From c1342ae52fb3bf5f0f81956658bcd5a496430362 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Scho=CC=88ps?= Date: Wed, 15 Apr 2020 13:11:14 +0200 Subject: [PATCH] Inspired by Freek's Log Dumper let's get rid of the JSON viewer --- src/Loggy.php | 43 +++++++++++++++++++++++++++++++------------ src/helpers.php | 4 ++-- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/Loggy.php b/src/Loggy.php index 04ed37a..8eaf66e 100644 --- a/src/Loggy.php +++ b/src/Loggy.php @@ -3,14 +3,18 @@ namespace Nanuc\Loggy; use Nanuc\Loggy\Exceptions\LoggyException; +use Symfony\Component\VarDumper\Cloner\VarCloner; +use Symfony\Component\VarDumper\Dumper\HtmlDumper; class Loggy { protected $url; protected $key; - protected $measurements = []; + private $cloner; + private $dumper; + public function __construct($url, $key) { if(strlen($key) != config('loggy.key-length')) { @@ -18,21 +22,29 @@ public function __construct($url, $key) } $this->url = $url; $this->key = $key; + + $this->cloner = new VarCloner(); + $this->dumper = new HtmlDumper(); } /** - * @param $message + * @param $arguments */ - public function send($message) + public function send(...$arguments) { - $this->postWithoutWait($this->url . '/' . $this->key, [ - 'message' => $message, - 'runtime' => defined('LARAVEL_START') ? microtime(true) - LARAVEL_START : null, - 'app' => [ - 'name' => config('app.name'), - 'env' => config('app.env'), - ] - ]); + foreach ($arguments as $message) { + $pretty = $this->convertToString($message); + + $this->postWithoutWait($this->url . '/' . $this->key, [ + 'message' => $message, + 'pretty' => $pretty, + 'runtime' => defined('LARAVEL_START') ? microtime(true) - LARAVEL_START : null, + 'app' => [ + 'name' => config('app.name'), + 'env' => config('app.env'), + ] + ]); + } } /** @@ -54,7 +66,7 @@ public function stopMeasurement($name = 'Measurement') $this->send('[' . $name . '] ' . number_format($time - $this->measurements[$name], 4)); } else { - $this->send('[' . $name . '] Measurement was not started'); + $this->send('[' . $name . '] Measurement with this name has not been started.'); } } @@ -80,4 +92,11 @@ protected function postWithoutWait($url, $params) fwrite($fp, $out); fclose($fp); } + + protected function convertToString($argument): string + { + $clonedArgument = $this->cloner->cloneVar($argument); + + return $this->dumper->dump($clonedArgument, true); + } } \ No newline at end of file diff --git a/src/helpers.php b/src/helpers.php index d9b7b23..8b6ef04 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -6,9 +6,9 @@ /** * Send a loggy */ - function loggy($message) + function loggy(...$arguments) { - return Loggy::send($message); + return Loggy::send(...$arguments); } }