Skip to content

Commit

Permalink
Inspired by Freek's Log Dumper let's get rid of the JSON viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Schöps committed Apr 15, 2020
1 parent fee54d8 commit c1342ae
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
43 changes: 31 additions & 12 deletions src/Loggy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,48 @@
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')) {
throw new LoggyException('Key must have a length of ' . config('loggy.key-length') . ' characters');
}
$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'),
]
]);
}
}

/**
Expand All @@ -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.');
}
}

Expand All @@ -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);
}
}
4 changes: 2 additions & 2 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
/**
* Send a loggy
*/
function loggy($message)
function loggy(...$arguments)
{
return Loggy::send($message);
return Loggy::send(...$arguments);
}
}

Expand Down

0 comments on commit c1342ae

Please sign in to comment.