Skip to content

Commit db1e798

Browse files
committed
Created a new section for rotating log files
and explained the max_files configuration option
1 parent 7345c17 commit db1e798

File tree

1 file changed

+63
-53
lines changed

1 file changed

+63
-53
lines changed

cookbook/logging/monolog.rst

+63-53
Original file line numberDiff line numberDiff line change
@@ -222,66 +222,76 @@ easily. Your formatter must implement
222222
),
223223
));
224224
225+
How to Rotate your Log Files
226+
----------------------------
227+
228+
Beware that log file sizes can grow very rapidly, leading to disk space exhaustion.
229+
This is specially true in the ``dev`` environment, where a simple request can
230+
generate hundreds of log lines. Consider using tools like the `logrotate`_
231+
Linux command to rotate log files before they become a problem.
232+
233+
In case you cannot use a dedicated tool for rotating log files, consider using
234+
the special ``rotating_file`` handler defined by Monolog. This handler creates
235+
a new log file every day and can also remove old files automatically. To use
236+
it, just set the ``type`` option of your handler to ``rotating_file``:
237+
238+
.. configuration-block::
239+
240+
.. code-block:: yaml
241+
242+
# app/config/config_dev.yml
243+
monolog:
244+
handlers:
245+
main:
246+
type: rotating_file
247+
path: %kernel.logs_dir%/%kernel.environment%.log
248+
level: debug
249+
# max number of log files to keep
250+
# defaults to zero, which means infinite files
251+
max_files: 10
252+
253+
.. code-block:: xml
254+
255+
<!-- app/config/config_dev.xml -->
256+
<?xml version="1.0" charset="UTF-8" ?>
257+
<container xmlns=''http://symfony.com/schema/dic/services"
258+
xmlns:monolog="http://symfony.com/schema/dic/monolog">
259+
260+
<monolog:config>
261+
<monolog:handler name="main"
262+
type="rotating_file"
263+
path="%kernel.logs_dir%/%kernel.environment%.log"
264+
level="debug"
265+
<!-- max number of log files to keep
266+
defaults to zero, which means infinite files -->
267+
max_files="10"
268+
/>
269+
</monolog:config>
270+
</container>
271+
272+
.. code-block:: php
273+
274+
// app/config/config_dev.php
275+
$container->loadFromExtension('monolog', array(
276+
'handlers' => array(
277+
'main' => array(
278+
'type' => 'rotating_file',
279+
'path' => '%kernel.logs_dir%/%kernel.environment%.log',
280+
'level' => 'debug',
281+
// max number of log files to keep
282+
// defaults to zero, which means infinite files
283+
'max_files' => 10,
284+
),
285+
),
286+
));
287+
225288
Adding some extra Data in the Log Messages
226289
------------------------------------------
227290
228291
Monolog allows you to process the record before logging it to add some
229292
extra data. A processor can be applied for the whole handler stack or
230293
only for a specific handler.
231294
232-
.. tip::
233-
234-
Beware that log file sizes can grow very rapidly, leading to disk space exhaustion.
235-
This is specially true in the ``dev`` environment, where a simple request can
236-
generate hundreds of log lines. Consider using tools like the `logrotate`_
237-
Linux command to rotate log files before they become a problem.
238-
239-
In case you cannot use a dedicated tool for rotating log files, consider using
240-
the special ``rotating_file`` handler defined by Monolog. This handler creates
241-
a new log file every day and can also remove old files automatically. To use
242-
it, just set the ``type`` option of your handler to ``rotating_file``:
243-
244-
.. configuration-block::
245-
246-
.. code-block:: yaml
247-
248-
# app/config/config_dev.yml
249-
monolog:
250-
handlers:
251-
main:
252-
type: rotating_file
253-
path: %kernel.logs_dir%/%kernel.environment%.log
254-
level: debug
255-
256-
.. code-block:: xml
257-
258-
<!-- app/config/config_dev.xml -->
259-
<?xml version="1.0" charset="UTF-8" ?>
260-
<container xmlns=''http://symfony.com/schema/dic/services"
261-
xmlns:monolog="http://symfony.com/schema/dic/monolog">
262-
263-
<monolog:config>
264-
<monolog:handler name="main"
265-
type="rotating_file"
266-
path="%kernel.logs_dir%/%kernel.environment%.log"
267-
level="debug"
268-
/>
269-
</monolog:config>
270-
</container>
271-
272-
.. code-block:: php
273-
274-
// app/config/config_dev.php
275-
$container->loadFromExtension('monolog', array(
276-
'handlers' => array(
277-
'main' => array(
278-
'type' => 'rotating_file',
279-
'path' => '%kernel.logs_dir%/%kernel.environment%.log',
280-
'level' => 'debug',
281-
),
282-
),
283-
));
284-
285295
A processor is simply a callable receiving the record as its first argument.
286296
Processors are configured using the ``monolog.processor`` DIC tag. See the
287297
:ref:`reference about it <dic_tags-monolog-processor>`.

0 commit comments

Comments
 (0)