Symfony logs to stdout inside Docker container

后端 未结 2 615
刺人心
刺人心 2020-12-13 00:37

I\'m building a docker image for a Symfony application. In this image, I want to stream the Symfony logs to stdout. So, similar to how nginx logs are configured, I added thi

相关标签:
2条回答
  • 2020-12-13 01:04

    With the help of Monolog, it is very easy to send logs to stdout/stderr. My examples are using stderr, but I think it's the same with stdout.

    Instead of defining a log file you just enter the preferred stream path

    path:  "php://stderr"
    

    BUT you are not done yet. You also have to configure PHP accordingly. The workers have to catch the output of their processes and log this output again to their stderr.

    PHP Configuration

    #/etc/php/7.0/fpm/php-fpm.conf
    error_log = /proc/self/fd/2
    
    #/etc/php/7.0/fpm/pool.d/www.conf
    catch_workers_output = yes
    

    Symfony Configuration

    # app/config/config_prod.yml
    monolog:
        handlers:
            main:
                type:         fingers_crossed
                action_level: error
                handler:      nested
            nested:
                type:  stream
                path:  "php://stderr"
                level: debug
            console:
                type:  console
    

    If you are using any process control system in a fat docker container you have to make sure that this system also logs to stdout (or stderr).

    Example with supervisor:

    [supervisord]
    nodaemon=true
    ;@see http://blog.turret.io/basic-supervisor-logging-with-docker/
    ;we need the output from the controlled processes
    ;but this is only possible with lowered loglevel
    loglevel=debug
    

    All in all make sure that:

    • The application logs to stdout/stderr
    • PHP catches workers output and logs to stderr
    • optional: any process control system has to forward output of managed processes to stdout/stderr
    0 讨论(0)
  • 2020-12-13 01:27

    My config to get all php infos / warnings / errors to docker log (I am using php:7.1-apache image):

    php.ini

    log_errors = On
    error_reporting = E_ALL | E_STRICT
    error_log = /dev/stderr
    

    Symfony config

    # app/config/config_prod.yml
    monolog:
    handlers:
        main:
            type: group
            members: [logfile, dockerlog]
        logfile:
            type: stream
            path: "%kernel.logs_dir%/%kernel.environment%.log"
            level: debug
            channels: ['!event', '!doctrine']
        dockerlog:
            type: stream
            path:  "php://stderr"
            level: debug
            channels: ['!event', '!doctrine']
    

    troubleshooting:

    • check symfony environment (prod/dev/test)
    • clear symfony cache maybe
    • rebuild container, if you copy php.ini in dockerfile
    0 讨论(0)
提交回复
热议问题