Skip to content

Fixed logger processor example #8226

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

Merged
merged 1 commit into from
Sep 3, 2017
Merged
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
62 changes: 31 additions & 31 deletions logging/processors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,31 @@ using a processor.

.. code-block:: php

namespace AppBundle;
namespace AppBundle\Logger;

use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

class SessionRequestProcessor
{
private $session;
private $token;
private $sessionId;

public function __construct(Session $session)
public function __construct(SessionInterface $session)
{
$this->session = $session;
}

public function processRecord(array $record)
{
if (null === $this->token) {
try {
$this->token = substr($this->session->getId(), 0, 8);
} catch (\RuntimeException $e) {
$this->token = '????????';
}
$this->token .= '-' . substr(uniqid(), -8);
if (!$this->session->isStarted()) {
return $record;
}
$record['extra']['token'] = $this->token;

if (!$this->sessionId) {
$this->sessionId = substr($this->session->getId(), 0, 8) ?: '????????';
}

$record['extra']['token'] = $this->sessionId.'-'.substr(uniqid('', true), -8);

return $record;
}
Expand All @@ -59,8 +59,8 @@ using a processor.
arguments:
- "[%%datetime%%] [%%extra.token%%] %%channel%%.%%level_name%%: %%message%% %%context%% %%extra%%\n"

monolog.processor.session_request:
class: AppBundle\SessionRequestProcessor
app.logger.session_request_processor:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be an FQCN from 3.3

class: AppBundle\Logger\SessionRequestProcessor
arguments: ['@session']
tags:
- { name: monolog.processor, method: processRecord }
Expand All @@ -71,7 +71,7 @@ using a processor.
type: stream
path: '%kernel.logs_dir%/%kernel.environment%.log'
level: debug
formatter: monolog.formatter.session_request
formatter: app.logger.session_request_processor

.. code-block:: xml

Expand All @@ -92,8 +92,8 @@ using a processor.
<argument>[%%datetime%%] [%%extra.token%%] %%channel%%.%%level_name%%: %%message%% %%context%% %%extra%%&#xA;</argument>
</service>

<service id="monolog.processor.session_request"
class="AppBundle\SessionRequestProcessor">
<service id="app.logger.session_request_processor"
class="AppBundle\Logger\SessionRequestProcessor">

<argument type="service" id="session" />
<tag name="monolog.processor" method="processRecord" />
Expand All @@ -106,23 +106,23 @@ using a processor.
type="stream"
path="%kernel.logs_dir%/%kernel.environment%.log"
level="debug"
formatter="monolog.formatter.session_request"
formatter="app.logger.session_request_processor"
/>
</monolog:config>
</container>

.. code-block:: php

// app/config/config.php
use AppBundle\SessionRequestProcessor;
use AppBundle\Logger\SessionRequestProcessor;
use Monolog\Formatter\LineFormatter;

$container
->register('monolog.formatter.session_request', LineFormatter::class)
->addArgument('[%%datetime%%] [%%extra.token%%] %%channel%%.%%level_name%%: %%message%% %%context%% %%extra%%\n');

$container
->register('monolog.processor.session_request', SessionRequestProcessor::class)
->register('app.logger.session_request_processor', SessionRequestProcessor::class)
->addArgument(new Reference('session'))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'session' should be SessionInterface::class from 3.3

->addTag('monolog.processor', array('method' => 'processRecord'));

Expand All @@ -132,7 +132,7 @@ using a processor.
'type' => 'stream',
'path' => '%kernel.logs_dir%/%kernel.environment%.log',
'level' => 'debug',
'formatter' => 'monolog.formatter.session_request',
'formatter' => 'app.logger.session_request_processor',
),
),
));
Expand All @@ -155,8 +155,8 @@ the ``monolog.processor`` tag:

# app/config/config.yml
services:
monolog.processor.session_request:
class: AppBundle\SessionRequestProcessor
app.logger.session_request_processor:
class: AppBundle\Logger\SessionRequestProcessor
arguments: ['@session']
tags:
- { name: monolog.processor, method: processRecord, handler: main }
Expand All @@ -174,8 +174,8 @@ the ``monolog.processor`` tag:
http://symfony.com/schema/dic/monolog/monolog-1.0.xsd">

<services>
<service id="monolog.processor.session_request"
class="AppBundle\SessionRequestProcessor">
<service id="app.logger.session_request_processor
class="AppBundle\Logger\SessionRequestProcessor">

<argument type="service" id="session" />
<tag name="monolog.processor" method="processRecord" handler="main" />
Expand All @@ -190,7 +190,7 @@ the ``monolog.processor`` tag:
// ...
$container
->register(
'monolog.processor.session_request',
'app.logger.session_request_processor',
SessionRequestProcessor::class
)
->addArgument(new Reference('session'))
Expand All @@ -208,8 +208,8 @@ the ``monolog.processor`` tag:

# app/config/config.yml
services:
monolog.processor.session_request:
class: AppBundle\SessionRequestProcessor
app.logger.session_request_processor:
class: AppBundle\Logger\SessionRequestProcessor
arguments: ['@session']
tags:
- { name: monolog.processor, method: processRecord, channel: main }
Expand All @@ -227,8 +227,8 @@ the ``monolog.processor`` tag:
http://symfony.com/schema/dic/monolog/monolog-1.0.xsd">

<services>
<service id="monolog.processor.session_request"
class="AppBundle\SessionRequestProcessor">
<service id="app.logger.session_request_processor"
class="AppBundle\Logger\SessionRequestProcessor">

<argument type="service" id="session" />
<tag name="monolog.processor" method="processRecord" channel="main" />
Expand All @@ -242,6 +242,6 @@ the ``monolog.processor`` tag:

// ...
$container
->register('monolog.processor.session_request', SessionRequestProcessor::class)
->register('app.logger.session_request_processor', SessionRequestProcessor::class)
->addArgument(new Reference('session'))
->addTag('monolog.processor', array('method' => 'processRecord', 'channel' => 'main'));