Home >PHP Framework >Laravel >laravel log directory modification
Laravel is a popular PHP framework that greatly simplifies the process of web development. In a Laravel application, logging plays a very important role. Laravel uses a flexible log system and provides developers with a variety of log drivers, such as file storage logs, database storage logs, etc. In Laravel, logging can be implemented very easily and elegantly, but sometimes some of its configuration needs to be modified and customized, such as changing the log directory.
By default, Laravel's log files will be saved in the storage/logs
directory. In actual development, we may need to save logs in other directories, such as the system default /var/log
directory.
So how to change the log directory in Laravel?
First, we need to open the configuration file config/logging.php
. In this file, you can see that Laravel is configured with three log channels by default: stack, single, and daily. Among them, stack is a channel composed of multiple log drivers, single uses single file mode to save logs, and daily saves logs with date as the file name, generating a new log file every day.
Find the channels
array in the configuration file:
'channels' => [ 'stack' => [ 'driver' => 'stack', 'channels' => ['single'], ], 'single' => [ 'driver' => 'single', 'path' => storage_path('logs/laravel.log'), 'level' => 'debug', ], 'daily' => [ 'driver' => 'daily', 'path' => storage_path('logs/laravel.log'), 'level' => 'debug', 'days' => 7, ], ],
As shown in the above code snippet, there is a path## in both single and daily channels. #Attribute, which indicates the saving path of the log file. Modify the value of this attribute to change the logging directory.
path. For example, if you want to save the logging file to the
/var/log directory, set the
path property to
/var/log/laravel.log that is Can:
'single' => [ 'driver' => 'single', 'path' => '/var/log/laravel.log', 'level' => 'debug', ],If you want to customize the log file name and path in addition to modifying the directory where the log file is saved, you can use the
daily channel to achieve this.
daily The channel will generate a log file every day. You can set the directory to save the file through
path, and set the file name prefix through
filename.
'daily' => [ 'driver' => 'daily', 'path' => '/var/log', 'filename' => 'laravel.log', 'level' => 'debug', 'days' => 7, ],In the above code snippet, the
path attribute specifies the directory where the log file is saved, and the
filename attribute specifies the prefix name of the log file, such as setting
filename will generate a file name similar to
laravel-2019-08-08.log for
laravel.
config/logging.php configuration file.
The above is the detailed content of laravel log directory modification. For more information, please follow other related articles on the PHP Chinese website!