Skip to content

Commit

Permalink
[Feature] Handle monolog 3 (#38)
Browse files Browse the repository at this point in the history
* handle monolog 3

* Add test u

Co-authored-by: Perriguey Thomas <[email protected]>
  • Loading branch information
thomasp1992 and Perriguey Thomas authored Aug 3, 2022
1 parent 52f4fde commit 745a735
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 158 deletions.
88 changes: 45 additions & 43 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,51 @@ public function getConfigTreeBuilder(): TreeBuilder
$treeBuilder = new TreeBuilder('deamon_logger_extra');

$treeBuilder->getRootNode()->children()
->arrayNode('application')->isRequired()
->children()
->scalarNode('name')->defaultNull()->cannotBeEmpty()->end()
->scalarNode('locale')->defaultNull()->end()
->end()
->end()
->arrayNode('handlers')->isRequired()
->beforeNormalization()
->ifString()
->then(function($v) {
return array($v);
})
->end()
->prototype('scalar')->isRequired()->cannotBeEmpty()->end()
->end()
->arrayNode('config')->isRequired()->addDefaultsIfNotSet()
->children()
->scalarNode('channel_prefix')->defaultNull()->end()
->scalarNode('user_class')->defaultValue(null)->end()
->arrayNode('user_methods')
->useAttributeAsKey('value')
->normalizeKeys(false)
->defaultValue(array(
'user_name' => 'getUsername',
))
->prototype('scalar')->end()
->end()
->arrayNode('display')->addDefaultsIfNotSet()
->children()
->booleanNode('env')->defaultTrue()->end()
->booleanNode('locale')->defaultTrue()->end()
->booleanNode('application_name')->defaultTrue()->end()
->booleanNode('url')->defaultTrue()->end()
->booleanNode('route')->defaultTrue()->end()
->booleanNode('user_agent')->defaultTrue()->end()
->booleanNode('accept_encoding')->defaultTrue()->end()
->booleanNode('client_ip')->defaultTrue()->end()
->booleanNode('user')->defaultTrue()->end()
->booleanNode('global_channel')->defaultTrue()->end()
->end()
->end()
->end()
->end();
->arrayNode('application')->isRequired()
->children()
->scalarNode('name')->defaultNull()->cannotBeEmpty()->end()
->scalarNode('locale')->defaultNull()->end()
->scalarNode('version')->defaultNull()->end()
->end()
->end()
->arrayNode('handlers')->isRequired()
->beforeNormalization()
->ifString()
->then(function($v) {
return array($v);
})
->end()
->prototype('scalar')->isRequired()->cannotBeEmpty()->end()
->end()
->arrayNode('config')->isRequired()->addDefaultsIfNotSet()
->children()
->scalarNode('channel_prefix')->defaultNull()->end()
->scalarNode('user_class')->defaultValue(null)->end()
->arrayNode('user_methods')
->useAttributeAsKey('value')
->normalizeKeys(false)
->defaultValue(array(
'user_name' => 'getUsername',
))
->prototype('scalar')->end()
->end()
->arrayNode('display')->addDefaultsIfNotSet()
->children()
->booleanNode('env')->defaultTrue()->end()
->booleanNode('locale')->defaultTrue()->end()
->booleanNode('application_name')->defaultTrue()->end()
->booleanNode('application_version')->defaultTrue()->end()
->booleanNode('url')->defaultTrue()->end()
->booleanNode('route')->defaultTrue()->end()
->booleanNode('user_agent')->defaultTrue()->end()
->booleanNode('accept_encoding')->defaultTrue()->end()
->booleanNode('client_ip')->defaultTrue()->end()
->booleanNode('user')->defaultTrue()->end()
->booleanNode('global_channel')->defaultFalse()->end()
->end()
->end()
->end()
->end();

return $treeBuilder;
}
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/DeamonLoggerExtraExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function load(array $configs, ContainerBuilder $container): void
$definition = $container->getDefinition('deamon.logger_extra.context');
$definition->addArgument($config['application']['name']);
$definition->addArgument($config['application']['locale']);
$definition->addArgument($config['application']['version']);

$definition = $container->getDefinition('deamon.logger_extra.processors.web_processor');
$definition->addArgument($config['config']);
Expand Down
36 changes: 12 additions & 24 deletions Processors/Monolog/DeamonLoggerExtraWebProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Deamon\LoggerExtraBundle\Processors\Monolog;

use Deamon\LoggerExtraBundle\Services\DeamonLoggerExtraContext;
use Monolog\LogRecord;
use Monolog\Processor\WebProcessor as BaseWebProcessor;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
Expand Down Expand Up @@ -31,27 +32,26 @@ class DeamonLoggerExtraWebProcessor extends BaseWebProcessor

private array $userMethods;

private array $record;
private LogRecord $record;

public function __construct(?array $config = null)
{
parent::__construct([]);

$this->channelPrefix = $config['channel_prefix'] ?? null;
$this->channelPrefix =$config['channel_prefix'] ?? null;

$this->displayConfig = $config['display'] ?? [];
$this->userClass = $config['user_class'] ?? null;
$this->userMethods = $config['user_methods'] ?? [];
$this->userMethods = $config['user_methods'] ?? [];
$this->displayConfig = $config['display'] ?? [];
$this->userClass = $config['user_class'] ?? null;
}

public function __invoke(array $record): array
public function __invoke(LogRecord $record): LogRecord
{
$this->record = parent::__invoke($record);

$this->addContextInfo();
$this->addRequestInfo();
$this->addUserInfo();
$this->addChannelInfo();

return $this->record;
}
Expand All @@ -68,8 +68,10 @@ private function addContextInfo(): void
if (null !== $this->loggerExtraContext) {
$this->addInfo('locale', $this->loggerExtraContext->getLocale());
if ($this->configShowExtraInfo('application_name')) {
$this->record['extra']['application'] = $this->loggerExtraContext->getApplicationName();
$this->record->extra['application'] = $this->loggerExtraContext->getApplicationName();
}
$applicationVersion = $this->loggerExtraContext->getApplicationVersion() ?? $this->channelPrefix;
$this->addInfo('application_version', $applicationVersion);
}
}

Expand Down Expand Up @@ -114,7 +116,7 @@ private function appendUserMethodInfo(UserInterface $user): void
{
foreach ($this->userMethods as $name => $method) {
if (method_exists($user, $method)) {
$this->record['extra'][$name] = $user->$method();
$this->record->extra[$name] = $user->$method();
}
}
}
Expand All @@ -127,27 +129,13 @@ private function isUserInstanceValid(?TokenInterface $token): bool
return $token instanceof TokenInterface && $token->getUser() instanceof $this->userClass;
}

/**
* Add channel info to ease the log interpretation.
*/
private function addChannelInfo(): void
{
if (!array_key_exists('global_channel', $this->record['extra'])) {
$this->addInfo('global_channel', $this->record['channel']);
}

if ($this->channelPrefix !== null && substr($this->record['channel'], 0, strlen($this->channelPrefix)) !== $this->channelPrefix) {
$this->record['channel'] = sprintf('%s.%s', $this->channelPrefix, $this->record['channel']);
}
}

/**
* Add the extra info if configured to.
*/
private function addInfo(string $key, mixed $value): void
{
if ($this->configShowExtraInfo($key) && $value !== null) {
$this->record['extra'][$key] = $value;
$this->record->extra[$key] = $value;
}
}

Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ DeamonLoggerExtra Bundle
![symfony version](https://img.shields.io/badge/symfony->=6.0-blue.svg)
![php version](https://img.shields.io/badge/php->=8.0-blue.svg)

This project is used to add extra context information in your logs.
This project is used to add extra context information in your logs.

If you need compatibility with previous Symfony versions, have a look at previous releases.
If you need compatibility with previous Symfony versions, have a look at previous releases.

Requirements
----------------
Expand Down Expand Up @@ -68,9 +68,8 @@ With this example of monolog config, you can configure this bundle to only add e
deamon_logger_extra:
application:
name: "loc-deamonfront"
version: "v0.1"
handlers: [main]
config:
channel_prefix: "v0.1"
```

## Config reference
Expand All @@ -81,6 +80,7 @@ deamon_logger_extra:
application:
name: "loc-deamonfront" # default to null
locale: "fr" # default to null
version: "v0.1" # default to null
handlers: [main] # the only required field
config:
channel_prefix: "v0.1" # default to null
Expand All @@ -105,6 +105,6 @@ deamon_logger_extra:
// config/packages/deamon_logger_extra.yaml
deamon_logger_extra:
application: null
handlers: 'main'
handlers: ['main']
config: null
```
14 changes: 12 additions & 2 deletions Services/DeamonLoggerExtraContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ class DeamonLoggerExtraContext
{
public function __construct(
private ?string $applicationName,
private ?string $locale = null)
{}
private ?string $locale = null,
private ?string $applicationVersion = null)
{
if(null !== $applicationVersion){
$this->applicationVersion = trim($this->applicationVersion);
}
}

public function getLocale(): ?string
{
Expand All @@ -18,4 +23,9 @@ public function getApplicationName(): ?string
{
return $this->applicationName;
}

public function getApplicationVersion(): ?string
{
return $this->applicationVersion;
}
}
11 changes: 8 additions & 3 deletions Tests/DependencyInjection/DeamonLoggerExtraExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ public function testLoad()

$definition1 = $this->container->getDefinition('deamon.logger_extra.context');
$this->assertEquals('foo', $definition1->getArgument(0));
$this->assertCount(2, $definition1->getArguments());
$this->assertCount(3, $definition1->getArguments());
$this->assertEquals('fr', $definition1->getArgument(1));
$this->assertEquals('barVersion', $definition1->getArgument(2));

$definition2 = $this->container->getDefinition('deamon.logger_extra.processors.web_processor');
$this->assertEquals($configs[0]['config'], $definition2->getArgument(0));
Expand All @@ -72,13 +73,14 @@ public function testDefaultValue()
'env' => true,
'locale' => true,
'application_name' => true,
'application_version' => true,
'url' => true,
'route' => true,
'user_agent' => true,
'accept_encoding' => true,
'client_ip' => true,
'user' => true,
'global_channel' => true,
'global_channel' => false,
],
];
$this->extension->load($configs, $this->container);
Expand All @@ -88,8 +90,9 @@ public function testDefaultValue()

$definition1 = $this->container->getDefinition('deamon.logger_extra.context');
$this->assertNull($definition1->getArgument(0));
$this->assertCount(2, $definition1->getArguments());
$this->assertCount(3, $definition1->getArguments());
$this->assertNull($definition1->getArgument(1));
$this->assertNull($definition1->getArgument(2));

$definition2 = $this->container->getDefinition('deamon.logger_extra.processors.web_processor');
$this->assertEquals($defaultConfigValues, $definition2->getArgument(0));
Expand Down Expand Up @@ -126,6 +129,7 @@ private function getValidConfigFull()
'application' => [
'name' => 'foo',
'locale' => 'fr',
'version' => 'barVersion'
],
'handlers' => ['bar'],
'config' => [
Expand All @@ -138,6 +142,7 @@ private function getValidConfigFull()
'env' => true,
'locale' => true,
'application_name' => true,
'application_version' => true,
'url' => true,
'route' => true,
'user_agent' => true,
Expand Down
Loading

0 comments on commit 745a735

Please sign in to comment.