Skip to content

Commit

Permalink
This commit is a lie
Browse files Browse the repository at this point in the history
  • Loading branch information
Deamon committed Mar 4, 2017
0 parents commit c7bdc7b
Show file tree
Hide file tree
Showing 14 changed files with 507 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor
9 changes: 9 additions & 0 deletions DeamonLoggerExtraBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Deamon\LoggerExtraBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class DeamonLoggerExtraBundle extends Bundle
{
}
62 changes: 62 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Deamon\LoggerExtraBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* This is the class that validates and merges configuration from your app/config files
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritDoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('deamon_logger_extra');

$rootNode->children()
->arrayNode('application')->isRequired()
->children()
->scalarNode('name')->defaultNull()->cannotBeEmpty()->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()->cannotBeEmpty()->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('user_id')->defaultTrue()->end()
->booleanNode('user_email')->defaultTrue()->end()
->booleanNode('user_name')->defaultTrue()->end()
->booleanNode('global_channel')->defaultTrue()->end()
->end()
->end()
->end()
->end()
;

return $treeBuilder;
}
}
41 changes: 41 additions & 0 deletions DependencyInjection/DeamonLoggerExtraExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Deamon\LoggerExtraBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class DeamonLoggerExtraExtension extends Extension
{
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
$loader->load('processors.xml');

$definition = $container->getDefinition('deamon.logger_extra.context');
$definition->replaceArgument(1, $config['application']['name']);

$definition = $container->getDefinition('deamon.logger_extra.processors.web_processor');
$definition->replaceArgument(1, $config['config']);

$definition->clearTag('monolog.processor');
foreach ($config['handlers'] as $handler) {
$definition->addTag('monolog.processor', ['handler'=>$handler]);
}

}
}
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2017-2020 Bastien DURAND

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
136 changes: 136 additions & 0 deletions Processors/Monolog/DeamonLoggerExtraWebProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php
namespace Deamon\LoggerExtraBundle\Processors\Monolog;

use Symfony\Bridge\Monolog\Processor\WebProcessor as BaseWebProcessor;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

class DeamonLoggerExtraWebProcessor extends BaseWebProcessor
{
/**
* @var Container
*/
private $container = null;

/**
* @var array|null
*/
private $displayConfig;
/**
* @var string
*/
private $channelPrefix;

public function __construct($container = null, array $config = null)
{
parent::__construct();
$this->container = $container;
$this->channelPrefix = $config['channel_prefix'];
$this->displayConfig = $config['display'];
}

/**
* @param array $record
*
* @return array
*/
public function __invoke(array $record)
{
$record = parent::__invoke($record);

if($this->container === null){
return $record;
}

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

return $record;
}

public function setContainer($container)
{
$this->container = $container;
}

private function addContextInfo(&$record)
{
if ($this->configShowExtraInfo('env')) {
$env = $this->container->get('kernel')->getEnvironment();
$record['extra']['env'] = $env;
}

$context = $this->container->get('deamon.logger_extra.context');

if ($this->configShowExtraInfo('locale')) {
$record['extra']['locale'] = $context->getLocale();
}
if ($this->configShowExtraInfo('application_name')) {
$record['extra']['application'] = $context->getApplicationName();
}
}

private function addRequestInfo(&$record)
{
if (null !== $request = $this->container->get('request_stack')->getCurrentRequest()
) {
if($this->configShowExtraInfo('url')) {
$record['extra']['url'] = $request->getRequestUri();
}
if ($this->configShowExtraInfo('route')) {
$record['extra']['route'] = $request->get('_route');
}
if ($this->configShowExtraInfo('user_agent')) {
$record['extra']['user_agent'] = $request->server->get('HTTP_USER_AGENT');
}
if ($this->configShowExtraInfo('accept_encoding')) {
$record['extra']['accept_encoding'] = $request->headers->get('Accept-Encoding');
}
if ($this->configShowExtraInfo('client_ip')) {
$record['extra']['client_ip'] = $request->getClientIp();
}
}
}

private function addUserInfo(&$record)
{
if ($this->configShowExtraInfo('user')) {
$token = $this->container->get('security.token_storage')->getToken();
if (($token instanceof TokenInterface) && ($token->getUser() instanceof UserInterface) && null !== $user = $token->getUser()) {
if ($this->configShowExtraInfo('user_id') && method_exists($user, 'getId')) {
$record['extra']['user_id'] = $user->getId();
}
if ($this->configShowExtraInfo('user_email') && method_exists($user, 'getEmail')) {
$record['extra']['user_email'] = $user->getEmail();
}
if ($this->configShowExtraInfo('user_name') && method_exists($user, 'getUsername')) {
$record['extra']['user_name'] = $user->getUsername();
}
}
}
}

private function addChannelInfo(&$record)
{
if ($this->configShowExtraInfo('global_channel')) {
$record['extra']['global_channel'] = $record['channel'];
}

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

/**
* @param $extraInfo
*
* @return bool
*/
private function configShowExtraInfo($extraInfo)
{
return isset($this->displayConfig[$extraInfo]) && $this->displayConfig[$extraInfo];
}
}
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#DeamonLoggerExtra Bundle

[![Build Status](https://travis-ci.org/Deamon/logger-extra-bundle.svg?branch=master)](https://travis-ci.org/Deamon/logger-extra-bundle)
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/2b9e464e-d291-487f-9169-8b445ee73d83/mini.png)](https://insight.sensiolabs.com/projects/2b9e464e-d291-487f-9169-8b445ee73d83)


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

Dans le projet qui importe le bundle

Ajouter dans config.yml les handlers dans le noeud monolog


## Example

Config sample for a project:

```
monolog:
handlers:
default_info:
type: gelf
publisher:
hostname: "%graylog_host%"
level: INFO
channels: [!request, !security, !app, !monitoring, !deprecation, !php]
default_notice:
type: gelf
publisher:
hostname: "%graylog_host%"
level: NOTICE
channels: [request, security, app, php]
```

With this example of monolog config, you can config the bundle to only add extra info on `default_info` handler.

```
deamon_logger_extra:
application:
name: "loc-deamonfront"
handlers: [default_info]
config:
channel_prefix: "v0.1"
```

## Config reference

```
deamon_logger_extra:
application:
name: "loc-deamonfront" #default to null
handlers: [default_info] #the only required field
config:
channel_prefix: "v0.1" #default to null
display:
env: boolean default to true
locale: boolean default to true
application_name: boolean default to true
url: boolean default to true
route: boolean default to true
user_agent: boolean default to true
accept_encoding: boolean default to true
client_ip: boolean default to true
user: boolean default to true
user_id: boolean default to true
user_email: boolean default to true
user_name: boolean default to true
global_channel: boolean default to true
```
19 changes: 19 additions & 0 deletions Resources/config/processors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="deamon.logger_extra.processors.web_processor.class">Deamon\LoggerExtraBundle\Processors\Monolog\DeamonLoggerExtraWebProcessor</parameter>
</parameters>
<services>
<service id="deamon.logger_extra.processors.web_processor" class="%deamon.logger_extra.processors.web_processor.class%">
<tag name="monolog.processor" handler="default_info"/>
<tag name="monolog.processor" handler="default_notice"/>
<argument />
<argument />
<call method="setContainer"><argument type="service" id="service_container"/></call>
</service>
</services>
</container>
16 changes: 16 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="deamon.logger_extra.context.class">Deamon\LoggerExtraBundle\Services\DeamonLoggerExtraContext</parameter>
</parameters>
<services>
<service id="deamon.logger_extra.context" class="%deamon.logger_extra.context.class%">
<argument type="string">%locale%</argument>
<argument />
</service>
</services>
</container>
Loading

0 comments on commit c7bdc7b

Please sign in to comment.