Skip to content

Commit a613c7a

Browse files
committed
feature #5137 Added a note about the rotating_file monolog handler (javiereguiluz)
This PR was merged into the 2.3 branch. Discussion ---------- Added a note about the rotating_file monolog handler | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes | Applies to | all | Fixed tickets | #1161 Commits ------- db1e798 Created a new section for rotating log files and explained the max_files configuration option 7345c17 Added XML and PHP configuration samples 31fd0db Added a note about the rotating_file monolog handler
2 parents a7cff0d + db1e798 commit a613c7a

File tree

1 file changed

+63
-7
lines changed

1 file changed

+63
-7
lines changed

cookbook/logging/monolog.rst

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,20 +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-
239295
A processor is simply a callable receiving the record as its first argument.
240296
Processors are configured using the ``monolog.processor`` DIC tag. See the
241297
:ref:`reference about it <dic_tags-monolog-processor>`.

0 commit comments

Comments
 (0)