Home  >  Article  >  Backend Development  >  Laravel changes the default log file name and location

Laravel changes the default log file name and location

不言
不言Original
2018-07-05 14:42:531810browse

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

Add log processing class

#First we create a new file, app /Foundation/Bootstrap/ConfigureLogging.php, the code is as follows:

{$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();
 }
}

Configure log storage path

#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',

to php and apply our new processing class

#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.logbingo.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn