'Laravel change log path

I'm using the following change my log path:

\Log::useDailyFiles(...)

But I still get log entries in /storage/logs/. How can I use only my log path?



Solution 1:[1]

Laravel already registers an instance of the logger when bootstrapping the ConfigureLogging class. So when you use Log::useDailyFiles() you're just adding an additional log handler, that's why you also get log entries in the standard storage/logs/laravel.log.

To override the default log handler, Laravel offers the configureMonologUsing method available on the application instance. So in your bootstrap/app.php file just before the return $app; statement, add the following:

$app->configureMonologUsing(function($monolog) use ($app) {
    $monolog->pushHandler(
        (new Monolog\Handler\RotatingFileHandler(
            // Set the log path
            '/custom/path/to/custom.log',
            // Set the number of daily files you want to keep
            $app->make('config')->get('app.log_max_files', 5)
        ))->setFormatter(new Monolog\Formatter\LineFormatter(null, null, true, true))
    );
});

The second parameter passed to the RotatingFileHandler tries to get a configuration value for log_max_files from config/app.php to determine how many daily log files it should keep, and if it doesn't find one it defaults to 5. If you want to keep an unlimited number of daily log files just pass 0 instead.


You can read more about logging configuration in the Laravel Documentation.

Solution 2:[2]

Laravel 5 : bootstrap/app.php

CUSTOM DAILY LOG :

     $app->configureMonologUsing(function($monolog) use ($app) {
         $monolog->pushHandler(
             (new Monolog\Handler\RotatingFileHandler(
                 // Set the log path
                 $app->storagePath().'/logs/app_error.log',
                 // Set the number of daily files you want to keep
                 $app->make('config')->get('app.log_max_files', 30)
             ))->setFormatter(new Monolog\Formatter\LineFormatter(null, null, true, true))
         );
     });

SINGLE LOG :

    $app->configureMonologUsing(function($monolog) use ($app) {
        $handler = new Monolog\Handler\StreamHandler($app->storagePath().'/logs/app_error.log');
        $handler->setFormatter(new \Monolog\Formatter\LineFormatter(null, null, true, true));
        $monolog->pushHandler($handler);
    });

Solution 3:[3]

For those still coming across this post, I believe changing your log file location is now easier in newer versions of Laravel. I am currently using 8.x.

In your /config/logging.php, you can define the path for your single and daily logs. Then, update whichever one you are looking to change.

'single' => [
    'driver' => 'single',
    'path' => "/your/desired/log/path/file.log", // edit here
    'level' => env('LOG_LEVEL', 'debug'),
],

'daily' => [
    'driver' => 'daily',
    'path' => "/your/desired/log/path/file.log", // edit here
    'level' => env('LOG_LEVEL', 'debug'),
    'days' => 14,
]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bogdan
Solution 2 Rashedul Islam Sagor
Solution 3 Karl Hill