Skip to content

Commit

Permalink
add processors to logging (placeholders)
Browse files Browse the repository at this point in the history
  • Loading branch information
alanpoulain committed Mar 3, 2023
1 parent 4ae98ef commit de64d99
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
24 changes: 18 additions & 6 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['with_placeholders'] ?? true ? [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['with_placeholders'] ?? true ? [new PsrLogMessageProcessor()] : []);
}

/**
Expand All @@ -333,7 +335,7 @@ protected function createSlackDriver(array $config)
$config['bubble'] ?? true,
$config['exclude_fields'] ?? []
), $config),
]);
], $config['with_placeholders'] ?? true ? [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['with_placeholders'] ?? true ? [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['with_placeholders'] ?? true ? [new PsrLogMessageProcessor()] : []);
}

/**
Expand All @@ -383,6 +385,14 @@ protected function createMonologDriver(array $config)
$config['handler'].' must be an instance of '.HandlerInterface::class
);
}
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)],
Expand All @@ -392,7 +402,9 @@ protected function createMonologDriver(array $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
46 changes: 46 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 @@ -264,6 +301,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 +311,7 @@ public function testLogManagerCreateSingleDriverWithConfiguredFormatter()
'formatter_with' => [
'dateFormat' => 'Y/m/d--test',
],
'with_placeholders' => false,
]);

$logger = $manager->channel('formattedsingle');
Expand All @@ -281,6 +320,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 @@ -306,6 +346,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 +356,7 @@ public function testLogManagerCreateDailyDriverWithConfiguredFormatter()
'formatter_with' => [
'dateFormat' => 'Y/m/d--test',
],
'with_placeholders' => false,
]);

$logger = $manager->channel('formatteddaily');
Expand All @@ -323,6 +365,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 @@ -347,6 +390,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 +399,7 @@ public function testLogManagerCreateSyslogDriverWithConfiguredFormatter()
'formatter_with' => [
'dateFormat' => 'Y/m/d--test',
],
'with_placeholders' => false,
]);

$logger = $manager->channel('formattedsyslog');
Expand All @@ -363,6 +408,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

0 comments on commit de64d99

Please sign in to comment.