Open cesargb opened 5 years ago
I always thought for this advanced cases one should simply use a 'custom
' driver, you can provide a dedicate class which gets invoked for the setup within which you can do what you want, including adding processors (that's how I'm doing it):
'application' => [
'driver' => 'custom',
'level' => LogLevel::DEBUG,
'name' => env('APP_ENV', 'production'),
'path' => storage_path('logs/laravel.log'),
'via' => CreateApplicationLogger::class,
],
And CreateApplicationLogger
has this:
public function __invoke(array $config): Logger
{
// Handler
$formatter = new LineFormatter(null, AppServiceProvider::MONOLOG_TIMESTAMP, true, true);
$formatter->includeStacktraces();
$handler = new StreamHandler($config['path'], $config['level']);
$handler->setFormatter($formatter);
// Processors
$processors = [
// adds class/function/file/line
new IntrospectionProcessor($config['level'], [
// but skip Laravels namespace ; most of the time we should only
// get App\ classes here.
'Illuminate\\',
]),
$this->app->make(CustomProcessor::class),
];
return new Logger($config['name'], [$handler], $processors);
}
+1 i support this idea
Easier to create custom formatter and tap
it
[
'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
],
'tap' => [
\App\Exceptions\LogChannels\FileFormatter::class
],
]
class FileFormatter
{
public function __invoke(Logger $logger)
{
/** @var Logger|\Monolog\Logger $logger */
/** @var StreamHandler[] $handlers */
$handlers = $logger->getHandlers();
foreach ($handlers as $handler) {
$handler->setFormatter(new LogstashFormatter('log', basename($handler->getUrl())));
$handler->pushProcessor(new FieldsProcessor());
$handler->pushProcessor(new WebProcessor());
$handler->pushProcessor(new IntrospectionProcessor());
$handler->pushProcessor(new GuardProcessor());
$handler->pushProcessor(new QueryBindingsProcessor());
$handler->pushProcessor(new HeadersProcessor());
$handler->pushProcessor(new BadResponseGuzzleProcessor());
}
}
}
Monolog可以使用处理器在日志中写入额外的数据。例如,在日志中添加源IP地址,内存使用情况,...
我认为在Laravel框架中添加此功能并允许在配置文件中进行定义很有趣,例如:
config / logging.php
// ... 'channels' => [ // ... 'daily' => [ 'driver' => 'daily', // ... 'processors' => [ new Monolog \ Processor \ MemoryUsageProcessor, function($ record){ $ record [ 'extra' ] [ 'dummy' ] = 'Hello world!' ; 返回 $记录 ; }, ], ],
参考文献
+1
Easier to create custom formatter and
tap
it
But at this point this is "complexity-wise" already what you can do right now with the custom
driver => that's exactly what it was defined for.
👎 on the added special handling 😄
lumen 5.8 not use processors in logging.php
😭
Monolog can use the processors to write extra data in logs. For example, add in your logs the source Ip Address, memory usage, ...
I think that be interesting in add this feature in Laravel framework, and permit define in config file, example:
config/logging.php
References