Logging not working in laravel queue job

后端 未结 3 1851
情书的邮戳
情书的邮戳 2021-02-02 02:10

I have the below settings in my supervisor/conf.d/myconf.conf file :

[program:my-worker]    
process_name=%(program_name)s_%(process_num)02d    
command=php /var         


        
相关标签:
3条回答
  • 2021-02-02 02:20

    I guess you are using Daily logs.

    When we have single log file then we have one log file : "laravel.log" which we give full permission [777], so we are able to log and everything works as expected.

    But when we have daily file selected then each day the laravel app will create a new file whenever it needs to log something. Now this file's owner is the web server (daemon/www-root) because laravel is run by the user daemon/www-root.

    When the queue is getting processed the user in action is "cli", and it does not have permission to write to this file. so it throws an exception and the processing stops.

    What I would suggest to do if you are using daily logs is change monolog settings so that for different users different log files will be created.

    Add this code to bootstrap/app.php

    /**
     * Configure Monolog.
     */
    $app->configureMonologUsing( function( Monolog\Logger $monolog) {
        $processUser = posix_getpwuid( posix_geteuid() );
        $processName= $processUser[ 'name' ];
    
        $filename = storage_path( 'logs/laravel-' . php_sapi_name() . '-' . $processName . '.log' );
        $handler = new Monolog\Handler\RotatingFileHandler( $filename );
        $monolog->pushHandler( $handler );
    });
    

    Just before retrurning the app.

    Now you will have log files for cli too, and everything related to queue will be logged in that file.

    This also keeps log files clean. HTTP app logs will be in different file and the Queue processing logs will be in different file.

    Also please don't forget to run following commands.

    To make sure new config is loaded correctly

    php artisan config:clear
    

    As you are using supervisor daemon queue worker, so you will need to broadcast the queue restart signal.

    php artisan queue:restart
    

    Hope this helps.

    0 讨论(0)
  • 2021-02-02 02:33

    Based on @PrabhatRai answer, here is another configuration example that looks more like Laravel's default behavior.

    Add this code to bootstrap/app.php:

    $app->configureMonologUsing(function (Monolog\Logger $monolog) {
        $filename = storage_path('logs/' . php_sapi_name() . '-' . posix_getpwuid(posix_geteuid())['name'] . '.log');
        $monolog->pushHandler($handler = new Monolog\Handler\RotatingFileHandler($filename, 30));
        $handler->setFilenameFormat('laravel-{date}-{filename}', 'Y-m-d');
        $handler->setFormatter(new \Monolog\Formatter\LineFormatter(null, null, true, true));
    });
    
    • It will store files like this: laravel-2017-03-27-cli-raph.log and laravel-2017-03-27-fpm-cgi-raph.log which is more readable.
    • New lines are preserved (as of default Laravel behavior)
    • It works with Laravel Log Viewer
    0 讨论(0)
  • 2021-02-02 02:34

    For solving this problem I did the following :

    1. Set the group id of the storage directory and all its sub-directories. Every file created in this directory will have www-data as group.

      sudo chgrp -r www-data storage
      sudo chmod +R g+s storage
      
    2. Add the <user> running the script to the group www-data

      sudo usermod -a -G www-data user
      
    0 讨论(0)
提交回复
热议问题