Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add processors to logging (placeholders) #46344

Merged
merged 3 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions src/Illuminate/Log/LogManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Monolog\Handler\SyslogHandler;
use Monolog\Handler\WhatFailureGroupHandler;
use Monolog\Logger as Monolog;
use Monolog\Processor\ProcessorInterface;
use Monolog\Processor\PsrLogMessageProcessor;
use Psr\Log\LoggerInterface;
use Throwable;

Expand Down Expand Up @@ -293,7 +295,7 @@ protected function createSingleDriver(array $config)
$config['bubble'] ?? true, $config['permission'] ?? null, $config['locking'] ?? false
), $config
),
]);
], $config['replace_placeholders'] ?? false ? [new PsrLogMessageProcessor()] : []);
}

/**
Expand All @@ -309,7 +311,7 @@ protected function createDailyDriver(array $config)
$config['path'], $config['days'] ?? 7, $this->level($config),
$config['bubble'] ?? true, $config['permission'] ?? null, $config['locking'] ?? false
), $config),
]);
], $config['replace_placeholders'] ?? false ? [new PsrLogMessageProcessor()] : []);
}

/**
Expand All @@ -333,7 +335,7 @@ protected function createSlackDriver(array $config)
$config['bubble'] ?? true,
$config['exclude_fields'] ?? []
), $config),
]);
], $config['replace_placeholders'] ?? false ? [new PsrLogMessageProcessor()] : []);
}

/**
Expand All @@ -349,7 +351,7 @@ protected function createSyslogDriver(array $config)
Str::snake($this->app['config']['app.name'], '-'),
$config['facility'] ?? LOG_USER, $this->level($config)
), $config),
]);
], $config['replace_placeholders'] ?? false ? [new PsrLogMessageProcessor()] : []);
}

/**
Expand All @@ -364,7 +366,7 @@ protected function createErrorlogDriver(array $config)
$this->prepareHandler(new ErrorLogHandler(
$config['type'] ?? ErrorLogHandler::OPERATING_SYSTEM, $this->level($config)
)),
]);
], $config['replace_placeholders'] ?? false ? [new PsrLogMessageProcessor()] : []);
}

/**
Expand All @@ -384,15 +386,33 @@ protected function createMonologDriver(array $config)
);
}

collect($config['processors'] ?? [])->each(function ($processor) {
$processor = $processor['processor'] ?? $processor;

if (! is_a($processor, ProcessorInterface::class, true)) {
throw new InvalidArgumentException(
$processor.' must be an instance of '.ProcessorInterface::class
);
}
});

$with = array_merge(
['level' => $this->level($config)],
$config['with'] ?? [],
$config['handler_with'] ?? []
);

return new Monolog($this->parseChannel($config), [$this->prepareHandler(
$this->app->make($config['handler'], $with), $config
)]);
return new Monolog(
$this->parseChannel($config),
[
$this->prepareHandler(
$this->app->make($config['handler'], $with), $config
)
],
collect($config['processors'] ?? [])
->map(fn ($processor) =>
$this->app->make($processor['processor'] ?? $processor, $processor['with'] ?? [])
)->toArray());
}

/**
Expand Down
49 changes: 49 additions & 0 deletions tests/Log/LogManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Monolog\Handler\SyslogHandler;
use Monolog\Level;
use Monolog\Logger as Monolog;
use Monolog\Processor\MemoryUsageProcessor;
use Monolog\Processor\PsrLogMessageProcessor;
use Monolog\Processor\UidProcessor;
use Orchestra\Testbench\TestCase;
use ReflectionProperty;
Expand Down Expand Up @@ -63,6 +65,7 @@ public function testStackChannel()
'stream' => 'php://stderr',
'bubble' => false,
],
'processors' => [PsrLogMessageProcessor::class],
]);

$config->set('logging.channels.stdout', [
Expand All @@ -84,6 +87,7 @@ public function testStackChannel()
$this->assertInstanceOf(Logger::class, $logger);
$this->assertCount(2, $handlers);
$this->assertInstanceOf(StreamHandler::class, $handlers[0]);
$this->assertInstanceOf(PsrLogMessageProcessor::class, $logger->getLogger()->getProcessors()[0]);
$this->assertInstanceOf(StreamHandler::class, $handlers[1]);
$this->assertEquals(Level::Notice, $handlers[0]->getLevel());
$this->assertEquals(Level::Info, $handlers[1]->getLevel());
Expand Down Expand Up @@ -211,6 +215,39 @@ public function testLogManagerCreatesMonologHandlerWithProperFormatter()
$this->assertInstanceOf(NullHandler::class, $handler);
}

public function testLogManagerCreatesMonologHandlerWithProcessors()
{
$config = $this->app->make('config');
$config->set('logging.channels.memory', [
'driver' => 'monolog',
'name' => 'memory',
'handler' => StreamHandler::class,
'with' => [
'stream' => 'php://stderr',
],
'processors' => [
MemoryUsageProcessor::class,
['processor' => PsrLogMessageProcessor::class, 'with' => ['removeUsedContextFields' => true]],
],
]);

$manager = new LogManager($this->app);

// create logger with handler specified from configuration
$logger = $manager->channel('memory');
$handler = $logger->getLogger()->getHandlers()[0];
$processors = $logger->getLogger()->getProcessors();

$this->assertInstanceOf(StreamHandler::class, $handler);
$this->assertInstanceOf(MemoryUsageProcessor::class, $processors[0]);
$this->assertInstanceOf(PsrLogMessageProcessor::class, $processors[1]);

$removeUsedContextFields = new ReflectionProperty(get_class($processors[1]), 'removeUsedContextFields');
$removeUsedContextFields->setAccessible(true);

$this->assertTrue($removeUsedContextFields->getValue($processors[1]));
}

public function testItUtilisesTheNullDriverDuringTestsWhenNullDriverUsed()
{
$config = $this->app->make('config');
Expand Down Expand Up @@ -253,6 +290,7 @@ public function testLogManagerCreateSingleDriverWithConfiguredFormatter()
'driver' => 'single',
'name' => 'ds',
'path' => storage_path('logs/laravel.log'),
'replace_placeholders' => true,
]);

$manager = new LogManager($this->app);
Expand All @@ -264,6 +302,7 @@ public function testLogManagerCreateSingleDriverWithConfiguredFormatter()

$this->assertInstanceOf(StreamHandler::class, $handler);
$this->assertInstanceOf(LineFormatter::class, $formatter);
$this->assertInstanceOf(PsrLogMessageProcessor::class, $logger->getLogger()->getProcessors()[0]);

$config->set('logging.channels.formattedsingle', [
'driver' => 'single',
Expand All @@ -273,6 +312,7 @@ public function testLogManagerCreateSingleDriverWithConfiguredFormatter()
'formatter_with' => [
'dateFormat' => 'Y/m/d--test',
],
'replace_placeholders' => false,
]);

$logger = $manager->channel('formattedsingle');
Expand All @@ -281,6 +321,7 @@ public function testLogManagerCreateSingleDriverWithConfiguredFormatter()

$this->assertInstanceOf(StreamHandler::class, $handler);
$this->assertInstanceOf(HtmlFormatter::class, $formatter);
$this->assertEmpty($logger->getLogger()->getProcessors());

$dateFormat = new ReflectionProperty(get_class($formatter), 'dateFormat');
$dateFormat->setAccessible(true);
Expand All @@ -295,6 +336,7 @@ public function testLogManagerCreateDailyDriverWithConfiguredFormatter()
'driver' => 'daily',
'name' => 'dd',
'path' => storage_path('logs/laravel.log'),
'replace_placeholders' => true,
]);

$manager = new LogManager($this->app);
Expand All @@ -306,6 +348,7 @@ public function testLogManagerCreateDailyDriverWithConfiguredFormatter()

$this->assertInstanceOf(StreamHandler::class, $handler);
$this->assertInstanceOf(LineFormatter::class, $formatter);
$this->assertInstanceOf(PsrLogMessageProcessor::class, $logger->getLogger()->getProcessors()[0]);

$config->set('logging.channels.formatteddaily', [
'driver' => 'daily',
Expand All @@ -315,6 +358,7 @@ public function testLogManagerCreateDailyDriverWithConfiguredFormatter()
'formatter_with' => [
'dateFormat' => 'Y/m/d--test',
],
'replace_placeholders' => false,
]);

$logger = $manager->channel('formatteddaily');
Expand All @@ -323,6 +367,7 @@ public function testLogManagerCreateDailyDriverWithConfiguredFormatter()

$this->assertInstanceOf(StreamHandler::class, $handler);
$this->assertInstanceOf(HtmlFormatter::class, $formatter);
$this->assertEmpty($logger->getLogger()->getProcessors());

$dateFormat = new ReflectionProperty(get_class($formatter), 'dateFormat');
$dateFormat->setAccessible(true);
Expand All @@ -336,6 +381,7 @@ public function testLogManagerCreateSyslogDriverWithConfiguredFormatter()
$config->set('logging.channels.defaultsyslog', [
'driver' => 'syslog',
'name' => 'ds',
'replace_placeholders' => true,
]);

$manager = new LogManager($this->app);
Expand All @@ -347,6 +393,7 @@ public function testLogManagerCreateSyslogDriverWithConfiguredFormatter()

$this->assertInstanceOf(SyslogHandler::class, $handler);
$this->assertInstanceOf(LineFormatter::class, $formatter);
$this->assertInstanceOf(PsrLogMessageProcessor::class, $logger->getLogger()->getProcessors()[0]);

$config->set('logging.channels.formattedsyslog', [
'driver' => 'syslog',
Expand All @@ -355,6 +402,7 @@ public function testLogManagerCreateSyslogDriverWithConfiguredFormatter()
'formatter_with' => [
'dateFormat' => 'Y/m/d--test',
],
'replace_placeholders' => false,
]);

$logger = $manager->channel('formattedsyslog');
Expand All @@ -363,6 +411,7 @@ public function testLogManagerCreateSyslogDriverWithConfiguredFormatter()

$this->assertInstanceOf(SyslogHandler::class, $handler);
$this->assertInstanceOf(HtmlFormatter::class, $formatter);
$this->assertEmpty($logger->getLogger()->getProcessors());

$dateFormat = new ReflectionProperty(get_class($formatter), 'dateFormat');
$dateFormat->setAccessible(true);
Expand Down