Skip to content

Commit

Permalink
Add ignore_error to config and update code style
Browse files Browse the repository at this point in the history
  • Loading branch information
hedii committed Mar 16, 2022
1 parent 327a32b commit 0554b59
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 102 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ return [
// This optional option determines the prefix for 'extra' fields
// from the Monolog record. Default is null (no extra prefix)
'extra_prefix' => null,

// This optional option determines whether errors thrown during
// logging should be ignored or not. Default is true.
'ignore_error' => true,

],
],
Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
},
"require": {
"php": "^8.0",
"illuminate/log": "^9.0",
"graylog2/gelf-php": "^1.7"
"graylog2/gelf-php": "^1.7",
"illuminate/log": "^9.0"
},
"require-dev": {
"orchestra/testbench": "^7.0"
Expand All @@ -43,6 +43,9 @@
"Hedii\\LaravelGelfLogger\\Tests\\": "tests/"
}
},
"config": {
"sort-packages": true
},
"scripts": {
"test": "vendor/bin/phpunit"
}
Expand Down
63 changes: 17 additions & 46 deletions src/GelfLoggerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,35 @@
use Gelf\Transport\TcpTransport;
use Gelf\Transport\UdpTransport;
use Illuminate\Contracts\Container\Container;
use InvalidArgumentException;
use Illuminate\Log\ParsesLogConfiguration;
use Monolog\Formatter\GelfMessageFormatter;
use Monolog\Handler\GelfHandler;
use Monolog\Logger;

class GelfLoggerFactory
{
protected $app;

protected $levels = [
'debug' => Logger::DEBUG,
'info' => Logger::INFO,
'notice' => Logger::NOTICE,
'warning' => Logger::WARNING,
'error' => Logger::ERROR,
'critical' => Logger::CRITICAL,
'alert' => Logger::ALERT,
'emergency' => Logger::EMERGENCY,
];

public function __construct(Container $app)
use ParsesLogConfiguration;

public function __construct(protected Container $app)
{
$this->app = $app;
}

public function __invoke(array $config): Logger
{
$transport = new IgnoreErrorTransportWrapper(
$this->getTransport(
$config['transport'] ?? 'udp',
$config['host'] ?? '127.0.0.1',
$config['port'] ?? 12201,
$config['path'] ?? null,
$this->enableSsl($config) ? $this->sslOptions($config['ssl_options'] ?? null) : null
)
$ignoreError = $config['ignore_error'] ?? true;

$transport = $this->getTransport(
$config['transport'] ?? 'udp',
$config['host'] ?? '127.0.0.1',
$config['port'] ?? 12201,
$config['path'] ?? null,
$this->enableSsl($config) ? $this->sslOptions($config['ssl_options'] ?? null) : null
);

if ($ignoreError) {
$transport = new IgnoreErrorTransportWrapper($transport);
}

$handler = new GelfHandler(new Publisher($transport), $this->level($config));

$handler->setFormatter(
Expand All @@ -72,7 +64,7 @@ protected function getTransport(
?string $path = null,
?SslOptions $sslOptions = null
): AbstractTransport {
return match(strtolower($transport)) {
return match (strtolower($transport)) {
'tcp' => new TcpTransport($host, $port, $sslOptions),
'http' => new HttpTransport($host, $port, $path ?? HttpTransport::DEFAULT_PATH, $sslOptions),
default => new UdpTransport($host, $port),
Expand Down Expand Up @@ -104,18 +96,6 @@ protected function sslOptions(?array $sslConfig = null): SslOptions
return $sslOptions;
}

/** @throws \InvalidArgumentException */
protected function level(array $config): int
{
$level = $config['level'] ?? 'debug';

if (isset($this->levels[$level])) {
return $this->levels[$level];
}

throw new InvalidArgumentException('Invalid log level.');
}

protected function parseProcessors(array $config): array
{
$processors = [];
Expand All @@ -129,15 +109,6 @@ protected function parseProcessors(array $config): array
return $processors;
}

protected function parseChannel(array $config): string
{
if (! isset($config['name'])) {
return $this->getFallbackChannelName();
}

return $config['name'];
}

protected function getFallbackChannelName(): string
{
return $this->app->bound('env') ? $this->app->environment() : 'production';
Expand Down
Loading

0 comments on commit 0554b59

Please sign in to comment.