Custom (dynamic) log file names with laravel5.6

Customisation is now done through invoking a custom formatter for Monolog.

Here is an example using daily rotating filenames (as I do).

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/laravel.log'),
        'level' => 'debug',
    ],

]

In your custom formatter, you can manipulate the Monolog logger however you wish, similar to configureMonologUsing():

app\Logging\CustomFilenames.php

<?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) {
                $sapi = php_sapi_name();
                $handler->setFilenameFormat("{filename}-$sapi-{date}", 'Y-m-d');
            }
        }
    }
}

One way to restore your original behaviour is to remove the {date} component from the handler's filenameFormat. A better way might be to manipulate the appropriate handler for the single driver.

See: https://laravel.com/docs/5.6/logging#advanced-monolog-channel-customization


Solution:

step1: create a channel inside the config/logging.php file

example :

'channels' => [
    'single' => [
    'driver' => 'single', 
    'path' => storage_path('logs/laravel.log'),
    'level' => 'debug',
],

'web' => [
      'driver' => 'single',
      'path' => storage_path('logs/web/web.log'),
   ],

]

Step2: Now set dyanamic path from controller like this

config(['logging.channels.web.path' => storage_path('logs/web/'.time().'.log')]);

Step3 : now generate your log

  Log::channel('web')->info("your message goes here");

Enjoy :)