skip to Main Content

Im using Queue::before in AppServiceProvider.php and set logging.channels.single.path value every time when job started:

config(['logging.channels.single.path' => storage_path('logs/accounts/'.$command->acc->login.'.log')]);

When I running 1 job all ok – logs in the right place.
When running 2 or more it writing logs to different files – one account can write to another accounts logfile. Why is it happening? It looks like it is caching the config variable.

Queue on horizon redis. One job after done dispatching another same job with the same $acc instance.

    Queue::before(function (JobProcessing $event) {
        $job = $event->job->payload();
        $command = unserialize($job['data']['command']);

Added ^^^ from where $command going.

2

Answers


  1. The configuration values work globaly for all sessions, like global variables (
    See the exampe here https://laravel.io/forum/how-can-i-set-global-dynamic-variables-in-laravel)

    You set the value in the config file always to the last login. Therefore all new logs go in the new named config file.

    Login or Signup to reply.
  2. Customization is now done through invoking a custom formatter for Monolog.

    This can be setup in config/logging.php, note the non-default tap parameter:

    'channels' => [
       'daily' => [
          'driver' => 'daily',
          'tap' => [AppLoggingCustomFilenames::class],
          'path' => storage_path('logs/accounts/laravel.log'),
          'level' => 'debug',
        ],
     ]
    

    In your custom formatter, you can manipulate the Monolog logger however you wish:

    <?php
    
    namespace AppLogging;
    
    use MonologHandlerRotatingFileHandler;
    
    class CustomFilenames
    {
        /**
        * Customize the given logger instance.
        *
        * @param  IlluminateLogLogger  $logger
        * @return void
        */
        public function __invoke($logger) {
           foreach ($logger->getHandlers() as $handler) {
              if ($handler instanceof RotatingFileHandler) {
                $login = $command->acc->login;
                $handler->setFilenameFormat("{filename}-$login-{date}", 'Y-m-d');
              }
           }
        }
     }
    

    See: https://laravel.com/docs/5.6/logging#advanced-monolog-channel-customization
    https://github.com/Seldaek/monolog/blob/master/src/Monolog/Handler/RotatingFileHandler.php

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search