This article mainly introduces how to modify the default log file name and location in Laravel. It has certain reference value. Now I share it with you. Friends in need can refer to it
Modify the default log location
In our usual development, we may always put laravel's log files in the default location without any impact, but if our project is fully deployed when it goes online, and each deployment is the latest code in git, then at this time Our logs will be cleared every time, showing that this is not what we expected. Fortunately, laravel provides us with a way to modify it.
Here we take the example of placing it in /var/log/nginx/app/phphub/phphub.log
#First we create a new file, app /Foundation/Bootstrap/ConfigureLogging.php, the code is as follows:
<?php namespace App\Foundation\Bootstrap; use Illuminate\Log\Writer; use Illuminate\Contracts\Foundation\Application; class ConfigureLogging { /** * 设置应用的Monolog处理程序 * * @param \Illuminate\Contracts\Foundation\Application $app * @param \Illuminate\Log\Writer $log * @return void */ public function configureHandlers(Application $app, Writer $log) { $method = 'configure'.ucfirst($app['config']['app.log']).'Handler'; $this->{$method}($app, $log); } /** * 设置应用single模式下的Monolog处理程序 * * @param \Illuminate\Contracts\Foundation\Application $app * @param \Illuminate\Log\Writer $log * @return void */ protected function configureSingleHandler(Application $app, Writer $log) { $config = $app->make('config'); $filename = $config->get('app.log_path', '/var/log/nginx/app/system') . '/' . $config->get('app.log_name', 'laravel') . '.log'; $log->useFiles($filename); } /** * 设置应用daily模式下的Monolog处理程序 * * @param \Illuminate\Contracts\Foundation\Application $app * @param \Illuminate\Log\Writer $log * @return void */ protected function configureDailyHandler(Application $app, Writer $log) { $config = $app->make('config'); $filename = $config->get('app.log_path', '/var/log/nginx/app/system') . '/' . $config->get('app.log_name', 'laravel') . '.log'; $log->useDailyFiles( $filename, $app->make('config')->get('app.log_max_files', 5) ); } /** * 设置应用syslog模式下的Monolog处理程序 * * @param \Illuminate\Contracts\Foundation\Application $app * @param \Illuminate\Log\Writer $log * @return void */ protected function configureSyslogHandler(Application $app, Writer $log) { $log->useSyslog($app->make('config')->get('app.log_name', 'laravel')); } /** * 设置应用errorlog模式下的Monolog处理程序 * * @param \Illuminate\Contracts\Foundation\Application $app * @param \Illuminate\Log\Writer $log * @return void */ protected function configureErrorlogHandler(Application $app, Writer $log) { $log->useErrorLog(); } }
#Add
APP_NAME=phphub
in .env in config/app. Add
/** * 应用程序名称 */ 'name' => env('APP_NAME', 'laravel'), /** * 日志位置 */ 'log_path' => '/var/log/nginx/app/' . env('APP_NAME', 'laravel'), /** * 日志文件名称 */ 'log_name' => env('APP_NAME', 'laravel'), /** * 日志文件最大数 */ 'log_max_files' => '30',
#Modify Bootstrap/app.php and add code before return $app
$app->configureMonologUsing(function($monolog) use ($app) { $configureLogging = new App\Foundation\Bootstrap\ConfigureLogging(); $configureLogging->configureHandlers($app, $app->log); });
At this time we You can use \Log::info('test log info');
to test it, and the log should be recorded in /var/log/nginx/app/phphub/phphub.log
bingo.
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
How to view logs in laravel terminal
The above is the detailed content of Laravel changes the default log file name and location. For more information, please follow other related articles on the PHP Chinese website!