Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

php - 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" ]
question from:https://stackoverflow.com/questions/65944449/saving-command-logger-output-to-log-file-and-console

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...