How to log messages to different files with Monolog in Symfony2.2?
Asked Answered
C

2

1

I would like to use Monolog in symfony2 application for logging, but my question is how can I split the file every day instead of appending to the same file?

I would like my log file to be somthing like below:

"%kernel.logs_dir%/%kernel.environment%.%date%.log" Which %date% should be replaced with real date.

I read that logrotate but I don't understand how to use it ?

Candicandia answered 18/9, 2013 at 16:7 Comment(0)
H
3

Use multiple handlers

Example:

monolog:
    handlers:
        main:
            type:  stream
            path:  %kernel.logs_dir%/%kernel.environment%.log
            level: debug
        ex1:
            type:  stream
            path:  %kernel.logs_dir%/ex1.log
            level: info
        ex2:
            type:  stream
            path:  %kernel.logs_dir%/ex2.log
            level: error

It's explained in the Symfony2 Cookbook http://symfony.com/doc/current/cookbook/logging/monolog.html

And specific for you (channels): http://symfony.com/doc/current/cookbook/logging/channels_handlers.html

Halogenate answered 13/11, 2013 at 14:58 Comment(1)
you can add the bubble: false entry if just want to record one specific level per handler/file.Ardrey
T
3

This will create a new file for each day. You can also define a max number of files.

monolog:
    handlers:
        main:
            type:       rotating_file
            path:       "%kernel.logs_dir%/%kernel.environment%.log"
            level:      notice
            max_files:  10

It will create a date to the file name automatically, so you don't have to worry about that.

http://symfony.com/doc/current/cookbook/logging/monolog.html

Tube answered 4/3, 2016 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.