How can i get logs of laravel in docker behind php-fpm?

12,068

Add stderr/stdout to the logging stack in config/logging.php

This was discussed before here, and Taylor added an example to stderr output (php://stderr) in the config/logging.php shipped with laravel https://github.com/laravel/ideas/issues/126

or just change the .env LOG_CHANNEL example quoting original comment: https://github.com/laravel/ideas/issues/126#issuecomment-438548169

In recent versions (5.6+) the default config/logging.php appears to include a stderr config, so you can just inject a LOG_CHANNEL=stderr environment variable into the container.

This will redirect all error/log based on your logging level to docker logs

Share:
12,068
Jonyhy96
Author by

Jonyhy96

Developer github.com/jonyhy96 kubernetes member

Updated on June 05, 2022

Comments

  • Jonyhy96
    Jonyhy96 almost 2 years

    During developing we met some problems with getting the real error log of the code.

    Architecture

    nginx -> php-fpm with laravel

    Problem

    can't get the logs of laravel

    Enviroment

    • image php:7.2.8-fpm-alpine3.7
    • docker 18.06.1-ce
    • laravel 5.5

    www.conf

    [www]
    user = www-data
    group = www-data
    listen = 127.0.0.1:9000
    clear_env = no
    catch_workers_output = yes
    
    pm = dynamic
    pm.max_children = 200
    pm.start_servers = 80
    pm.min_spare_servers = 50
    pm.max_spare_servers = 80
    pm.max_requests = 250
    request_terminate_timeout = 60
    
    slowlog = /var/log/error.log
    php_flag[display_errors] = on
    php_admin_value[error_log] = /var/log/error.log
    php_admin_flag[log_errors] = on
    php_value[session.save_handler] = files
    php_value[session.save_path]    = /usr/local/lib/session
    php_value[soap.wsdl_cache_dir]  = /usr/local/lib/wsdlcache
    ;php_value[opcache.file_cache]  = /usr/local/lib/opcache
    
    ;monitoring
    pm.status_path = /phpfpm_status
    ping.path = /phpfpm_ping
    ping.response = pong
    

    php.ini

    error_log = "/var/log/error.log"
    error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE
    display_errors = On
    display_startup_errors = On
    ...
    

    php-fpm.conf

    include=/usr/local/etc/php-fpm.d/*.conf
    [global]
    error_log = "/var/log/error.log"
    log_level = notice
    events.mechanism = epoll
    

    i already add full competence to the file /var/log/error.log & access.log right now,i only get php-fpm log in access.log and error.log

    /var/log # cat error.log
    [20-Mar-2019 06:08:34] NOTICE: fpm is running, pid 9
    [20-Mar-2019 06:08:34] NOTICE: ready to handle connections
    /var/log # cat access.log
    172.28.0.5 -  20/Mar/2019:06:34:12 +0000 "GET /index.php" 200
    172.28.0.5 -  20/Mar/2019:06:34:18 +0000 "POST /index.php" 200
    /var/log # pwd
    /var/log
    

    looking for answers

    • nct
      nct about 5 years
      Did you enable laravel log ?
    • Jonyhy96
      Jonyhy96 about 5 years
      @nhancao yes i already add this into env file APP_LOG=single APP_LOG_LEVEL=debug and i don't think this works,i think the php-fpm will take over all routines.
    • apokryfos
      apokryfos about 5 years
      Laravel's logs are generated in the application folder under storage/logs.
    • Jonyhy96
      Jonyhy96 about 5 years
      @apokryfos yes,but when i use php-fpm,it will not out put the log to storage/logs,even if i rewrite the monolog with StreamHandler to php://stderr or file,it won't work,i think the php-fpm will ignore laravel life cycle
    • apokryfos
      apokryfos about 5 years
      It certainly does not do that when using php-fpm outside of docker I don't see why it would do that within docker. however from what you've shared I don't see anything that would cause Laravel to write anything to the log unless you're manually calling \Log::error or something like that
    • Jonyhy96
      Jonyhy96 about 5 years
      @apokryfos i manually cause exception in my code ,and i alse use \Log::info,\Log::error,\Log::warningin my code,doesn't work either.