1

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.

rst630
  • 59
  • 2
  • 14
  • Have a look at `Config::set` that some of the answers to [this question](https://stackoverflow.com/questions/39563042/laravel-dynamic-config-settings) mention. – D Malan Jan 20 '20 at 16:39
  • 1
    If you expect a proper answer for this question, please show use how you set, use etc $command->acc->login – mrhn Jan 30 '20 at 08:27
  • ok I added code of $command. acc is serialized instance of Accounts model in job payload – rst630 Feb 01 '20 at 04:30

2 Answers2

1

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' => [App\Logging\CustomFilenames::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 App\Logging;

use Monolog\Handler\RotatingFileHandler;

class CustomFilenames
{
    /**
    * Customize the given logger instance.
    *
    * @param  \Illuminate\Log\Logger  $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

  • What type $command is it?. All of your "tap" classes are resolved by the service container, so any constructor dependencies they require will automatically be injected. – Eduard Castro Feb 02 '20 at 00:13
0

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.

Christian
  • 848
  • 1
  • 11
  • 23
  • This mean that when 2 jobs running - 2nd set new config and if 1st job still working it will write to log which setted by 2nd job? – rst630 Jan 25 '20 at 03:01
  • Yes, thats why it works for when you have only one job and fails as soon as you have the second. – Christian Jan 25 '20 at 10:05
  • If you want to add additional informations in the log this may help you https://laravel-news.com/laravel-log-enhancer-package – Christian Jan 25 '20 at 12:53
  • No I need just separate logs for each job, but I don't want on each log write putting filename as argument. Still search solution for this case – rst630 Jan 25 '20 at 13:38