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

Laravel changes the default log file name and location

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

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:

<?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 = &#39;configure&#39;.ucfirst($app[&#39;config&#39;][&#39;app.log&#39;]).&#39;Handler&#39;;
 $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(&#39;config&#39;);
 $filename = $config->get(&#39;app.log_path&#39;, &#39;/var/log/nginx/app/system&#39;) . &#39;/&#39; . $config->get(&#39;app.log_name&#39;, &#39;laravel&#39;) . &#39;.log&#39;;
 $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(&#39;config&#39;);
 $filename = $config->get(&#39;app.log_path&#39;, &#39;/var/log/nginx/app/system&#39;) . &#39;/&#39; . $config->get(&#39;app.log_name&#39;, &#39;laravel&#39;) . &#39;.log&#39;;
 $log->useDailyFiles(
 $filename,
 $app->make(&#39;config&#39;)->get(&#39;app.log_max_files&#39;, 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(&#39;config&#39;)->get(&#39;app.log_name&#39;, &#39;laravel&#39;));
 }
 /**
 * 设置应用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

/**
 * 应用程序名称
 */
&#39;name&#39; => env(&#39;APP_NAME&#39;, &#39;laravel&#39;),
/**
 * 日志位置
 */
&#39;log_path&#39; => &#39;/var/log/nginx/app/&#39; . env(&#39;APP_NAME&#39;, &#39;laravel&#39;),
/**
 * 日志文件名称
 */
&#39;log_name&#39; => env(&#39;APP_NAME&#39;, &#39;laravel&#39;),
/**
 * 日志文件最大数
 */
&#39;log_max_files&#39; => &#39;30&#39;,

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