Skip to content
Advertisement

Saving command logger output to log file and console

I wrote a very simple test command which has LoggerInterface injected in its constructor.

How am I suppose to change the monolog.yaml configuration to save this logger output to both log file and to output it to console?

monolog:
    handlers:
        main:
            type: fingers_crossed
            action_level: error
            formatter: monolog.line.formatter
            handler: terminal
            excluded_http_codes: [404, 405]
            buffer_size: 50 # How many messages should be saved? Prevent memory leaks
        terminal:
            type: stream
            path: "php://stderr"
            level: debug
        console:
            type: console
            process_psr_3_messages: false
            channels: [ "!event", "!doctrine" ]

Advertisement

Answer

The commands will always stderr by default (if you specify the -vvv option)

If you need to write the logs in a file only on error (with the stack error) you can use the finger_crossed handler :

handlers:
    main:
        # fingers_crossed allow to log only if action_level defined is reach
        type: fingers_crossed
        # minimum level to activate the handler
        # available level (emergency|alert|critical|error|warning|notice|info|debug){1}
        action_level: error
        # wrapped handler's name
        handler: nested

    nested:
        # stream allow to write log in file
        type: stream
        # path to the log file
        path:  "%kernel.logs_dir%/%project_name%_%kernel.environment%.log"
        # available level (emergency|alert|critical|error|warning|notice|info|debug){1}
        level: debug

If you want to filter a bit the logs shown in the stderr you can use the default config for the console :

console:
    type:   console
    process_psr_3_messages: false
    channels: ['!event', '!doctrine', '!console']

I’ll allow you to have nicer console logs (and avoid too many “useless” logs such as event or doctrine which are very verbose)

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement