Skip to content

Commit

Permalink
Laravel add processors (#1)
Browse files Browse the repository at this point in the history
* Add custom processors

* Update documentation

* Up composer version

Co-authored-by: sergei.baikin <[email protected]>
  • Loading branch information
GDXbsv and GDXbsv authored Jul 14, 2022
1 parent 5e37fbc commit 2dd1626
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ add in `config/logging.php` in `channels` section:
'driver' => 'custom',
'via' => new Gotphoto\Logging\Laravel\LaravelLoggerCreating,
'app_name' => 'ServiceName',
'channel' => 'app'(security/reauest/order)
'channel' => 'app'(security/reauest/order),
'processors' => [new Monolog\Processor\ProcessorInterface()], //OPTIONAL
]
'security' => [
'driver' => 'custom',
'via' => new Gotphoto\Logging\Laravel\LaravelLoggerCreating,
'app_name' => 'ServiceName',
'channel' => 'security'
'channel' => 'security',
'processors' => [new Monolog\Processor\ProcessorInterface()], //OPTIONAL
],
```

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "fotografde/logging",
"description": "Integrate a common way for log handling.",
"type": "library",
"version": "1.0.3",
"version": "1.1.0",
"require": {
"php": "^7|^8",
"monolog/monolog": "^2.7|^3.1",
Expand Down
16 changes: 15 additions & 1 deletion src/Laravel/LaravelLoggerCreating.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

namespace Gotphoto\Logging\Laravel;

use Aws\Exception\AwsException;
use Gotphoto\Logging\ExceptionContext\AwsExceptionContext;
use Gotphoto\Logging\ExceptionContext\GuzzleRequestExceptionContext;
use Gotphoto\Logging\Formatter;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Support\Facades\App;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
Expand All @@ -16,18 +20,28 @@ public function __invoke(array $config)
{
assert(!empty($config['app_name']) && is_string($config['app_name']));
assert(!empty($config['channel']) && is_string($config['channel']));
assert(!isset($config['processors']) || is_array($config['processors']));
$appName = $config['app_name'];
$channel = $config['channel'];
/** @var \Monolog\Processor\ProcessorInterface[] $processors */
$processors = $config['processors'] ?? [];

$log = new Logger($channel);
$log->pushProcessor(new Processor());

foreach ($processors as $processor) {
$log->pushProcessor($processor);
}

$streamHandler = new StreamHandler('php://stderr');

$handler = $streamHandler;
$env = App::environment();
$handler->setFormatter(
new Formatter($appName, (is_string($env) ? $env : "undefined"), [])
new Formatter($appName, (is_string($env) ? $env : "undefined"), [
RequestException::class => [new GuzzleRequestExceptionContext()],
AwsException::class => [new AwsExceptionContext()],
])
);
$log->pushHandler($handler);

Expand Down

0 comments on commit 2dd1626

Please sign in to comment.