Skip to content

Commit 3c19461

Browse files
committed
Updated the doc for monolog
This describes the changes done in symfony/symfony#1556.
1 parent 883fde2 commit 3c19461

File tree

3 files changed

+117
-69
lines changed

3 files changed

+117
-69
lines changed

cookbook/logging/monolog.rst

Lines changed: 7 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -163,65 +163,15 @@ only for a specific handler.
163163

164164
A processor is simply a callable receiving the record as it's first argument.
165165

166-
This example configuration shows you how to define a processor as a service,
167-
as a callback, or how you can simply use one of the core processors, which
168-
are called ``monolog.processor.<processor_name>``. At the moment, the ``web``
169-
and ``introspection`` processors are available in core.
170-
171-
.. configuration-block::
172-
173-
.. code-block:: yaml
174-
175-
services:
176-
my_processor:
177-
class: Acme\MyBundle\MyFirstProcessor
178-
monolog:
179-
handlers:
180-
file:
181-
type: stream
182-
level: debug
183-
processors:
184-
- Acme\MyBundle\MySecondProcessor::process
185-
processors:
186-
- @my_processor
187-
- @monolog.processor.web
188-
189-
.. code-block:: xml
190-
191-
<container xmlns="http://symfony.com/schema/dic/services"
192-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
193-
xmlns:monolog="http://symfony.com/schema/dic/monolog"
194-
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
195-
http://symfony.com/schema/dic/monolog http://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
196-
197-
<services>
198-
<service id="my_processor" class="Acme\MyBundle\MyFirstProcessor" />
199-
</services>
200-
<monolog:config>
201-
<monolog:handler
202-
name="file"
203-
type="stream"
204-
level="debug"
205-
formatter="my_formatter"
206-
>
207-
<monolog:processor callback="Acme\MyBundle\MySecondProcessor::process" />
208-
</monolog:handler />
209-
<monolog:processor callback="@my_processor" />
210-
<monolog:processor callback="@monolog.processor.web" />
211-
</monolog:config>
212-
</container>
213-
214-
.. tip::
215-
216-
If you need some dependencies in your processor you can define a
217-
service and implement the ``__invoke`` method on the class to make
218-
it callable. You can then add it in the processor stack.
166+
Processors are configured using the ``monolog.processor`` DIC tag. See the
167+
:ref:`reference about it<dic_tags-monolog-processor>`.
219168

220169
Adding a Session/Request Token
221170
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
222171

223172
Sometimes it is hard to tell which entries in the log belong to which session
224-
and/or request. The following example will add a unique token for each request.
173+
and/or request. The following example will add a unique token for each request
174+
using a processor.
225175

226176
.. code-block:: php
227177
@@ -239,7 +189,7 @@ and/or request. The following example will add a unique token for each request.
239189
$this->session = $session;
240190
}
241191
242-
public function __invoke(array $record)
192+
public function processRecord(array $record)
243193
{
244194
if (null === $this->token) {
245195
try {
@@ -268,6 +218,8 @@ and/or request. The following example will add a unique token for each request.
268218
monolog.processor.session_request:
269219
class: Acme\MyBundle\SessionRequestProcessor
270220
arguments: [ @session ]
221+
tags:
222+
- { name: monolog.processor, method: processRecord }
271223
272224
monolog:
273225
handlers:
@@ -276,7 +228,6 @@ and/or request. The following example will add a unique token for each request.
276228
path: %kernel.logs_dir%/%kernel.environment%.log
277229
level: debug
278230
formatter: monolog.formatter.session_request
279-
processors: [ @monolog.processor.session_request ]
280231
281232
.. note::
282233

reference/configuration/monolog.rst

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ Configuration Reference
1818
level: ERROR
1919
bubble: false
2020
formatter: my_formatter
21-
processors:
22-
- some_callable
2321
main:
2422
type: fingerscrossed
2523
action_level: WARNING
@@ -48,13 +46,10 @@ Configuration Reference
4846
from_email: ~
4947
to_email: ~
5048
subject: ~
51-
email_prototype: ~
49+
email_prototype:
50+
id: ~ # Required (when the email_prototype is used)
51+
method: ~
5252
formatter: ~
53-
processors: []
54-
processors:
55-
56-
# Example:
57-
- @my_processor
5853
5954
.. code-block:: xml
6055
@@ -72,9 +67,7 @@ Configuration Reference
7267
level="error"
7368
bubble="false"
7469
formatter="my_formatter"
75-
>
76-
<monolog:processor callback="some_callable" />
77-
</monolog:handler>
70+
/>
7871
<monolog:handler
7972
name="main"
8073
type="fingerscrossed"
@@ -86,7 +79,6 @@ Configuration Reference
8679
type="service"
8780
id="my_handler"
8881
/>
89-
<monolog:processor callback="@my_processor" />
9082
</monolog:config>
9183
</container>
9284

reference/dic_tags.rst

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@ The Dependency Injection Tags
44
Tags:
55

66
* ``data_collector``
7+
* ``form.type``
8+
* ``form.type_extension``
9+
* ``form.type_guesser``
10+
* ``kernel.cache_warmer``
711
* ``kernel.event_listener``
12+
* ``monolog.logger``
13+
* ``monolog.processor``
814
* ``templating.helper``
9-
* ``templating.renderer``
1015
* ``routing.loader``
16+
* ``translation.loader``
1117
* ``twig.extension``
18+
* ``validator.initializer``
1219

1320
Enabling Custom PHP Template Helpers
1421
------------------------------------
@@ -204,3 +211,101 @@ channel when injecting the logger in a service.
204211

205212
This works only when the logger service is a constructor argument,
206213
not when it is injected through a setter.
214+
215+
.. _dic_tags-monolog-processor:
216+
217+
Adding a processor for Monolog
218+
------------------------------
219+
220+
Monolog allows to add processors in the logger or in the handlers to add
221+
extra data in the records. A processor receives the record as argument and
222+
must return it after adding eventually some extra data in the ``extra``
223+
attribute of the record.
224+
225+
Let's see how you can use the built-in IntrospectionProcessor to add the file,
226+
the line, the class and the method where the logger was triggered.
227+
228+
You can add a processor globally:
229+
230+
.. configuration-block::
231+
232+
.. code-block:: yaml
233+
234+
services:
235+
my_service:
236+
class: Monolog\Processor\IntrospectionProcessor
237+
tags:
238+
- { name: monolog.processor }
239+
240+
.. code-block:: xml
241+
242+
<service id="my_service" class="Monolog\Processor\IntrospectionProcessor">
243+
<tag name="monolog.processor" />
244+
</service>
245+
246+
.. code-block:: php
247+
248+
$definition = new Definition('Monolog\Processor\IntrospectionProcessor');
249+
$definition->addTag('monolog.processor');
250+
$container->register('my_service', $definition);
251+
252+
.. tip::
253+
254+
If your service is not a callable (using ``__invoke``) you can add the
255+
``method`` attribute in the tag to use a specific method.
256+
257+
You can add also a processor for a specific handler by using the ``handler``
258+
attribute:
259+
260+
.. configuration-block::
261+
262+
.. code-block:: yaml
263+
264+
services:
265+
my_service:
266+
class: Monolog\Processor\IntrospectionProcessor
267+
tags:
268+
- { name: monolog.processor, handler: firephp }
269+
270+
.. code-block:: xml
271+
272+
<service id="my_service" class="Monolog\Processor\IntrospectionProcessor">
273+
<tag name="monolog.processor" handler="firephp" />
274+
</service>
275+
276+
.. code-block:: php
277+
278+
$definition = new Definition('Monolog\Processor\IntrospectionProcessor');
279+
$definition->addTag('monolog.processor', array('handler' => 'firephp');
280+
$container->register('my_service', $definition);
281+
282+
You can add also a processor for a specific logging by using the ``channel``
283+
attribute. This will register the processor only for the ``security`` logging
284+
channel used in the Security component:
285+
286+
.. configuration-block::
287+
288+
.. code-block:: yaml
289+
290+
services:
291+
my_service:
292+
class: Monolog\Processor\IntrospectionProcessor
293+
tags:
294+
- { name: monolog.processor, channel: security }
295+
296+
.. code-block:: xml
297+
298+
<service id="my_service" class="Monolog\Processor\IntrospectionProcessor">
299+
<tag name="monolog.processor" channel="security" />
300+
</service>
301+
302+
.. code-block:: php
303+
304+
$definition = new Definition('Monolog\Processor\IntrospectionProcessor');
305+
$definition->addTag('monolog.processor', array('channel' => 'security');
306+
$container->register('my_service', $definition);
307+
308+
.. note::
309+
310+
You cannot use both the ``handler`` and ``channel`` attributes for the
311+
same tag as handlers are shared between all channels.

0 commit comments

Comments
 (0)