Modify bootstrap/ file
$app->configureMonologUsing(function($monolog) use ($app) { $monolog->pushHandler( (new Monolog\Handler\RotatingFileHandler( '/var/logs/app/laravel', $app->make('config')->get('app.log_max_files', 5) ))->setFormatter(new Monolog\Formatter\LineFormatter(null, null, true, true)) ); });
After adding, write to the log file is:
-rw-r--r-- 1 web web 93 Dec 18 15:52 laravel-2017-12-17 -rw-r--r-- 1 web web 279 Dec 18 16:10 laravel-2017-12-18
refer to:Laravel Errors and Logging
or
Create app\Providers\ file
Modify config\ providers Add
App\Providers\LogServiceProvider::class
Then the content of App\Providers\ is as follows
<?php namespace App\Providers; use Illuminate\Log\LogServiceProvider as SysServiceProvider; use Illuminate\Log\Writer; class LogServiceProvider extends SysServiceProvider { protected function configureSingleHandler(Writer $log) { $log->useFiles( '/var/logs/app/', $this->logLevel() ); } protected function configureDailyHandler(Writer $log) { $log->useDailyFiles( '/var/logs/app/', $this->maxFiles(), $this->logLevel() ); } }
Add request id
namespace App\Providers; use Illuminate\Support\ServiceProvider; use Carbon\Carbon; class LogServiceProvider extends ServiceProvider { protected $log_file; /** * Bootstrap any application services. * @return void */ public function boot() { // } /** * Register any application services. * @return void */ public function register() { $this->load_request_id(); $this->log_configure(); } /** * Generate request_id * @return void */ protected function load_request_id() { define( 'REQUEST_ID' , config('app.log_prefix').Carbon::now()->timestamp ); } /** * Register monolog pushHandler * @return void */ protected function log_configure() { $log_file = $this->getLogFile(); $log_max_files = $this->getLogMaxFiles(); /** * @doc /docs/5.4/errors#Customize-Monolog-Settings */ $this->app->configureMonologUsing(function($monolog) use ($log_file , $log_max_files) { $monolog->pushHandler( (new \Monolog\Handler\RotatingFileHandler( $log_file , $log_max_files ))->setFormatter(new \Monolog\Formatter\LineFormatter( "[%datetime%] [".REQUEST_ID."] %channel%.%level_name%: %message% %context% %extra%\n", null, true, true)) ); }); } protected function getLogMaxFiles() { return config('app.log_max_files' , 5); } /** * @return mixed */ protected function getLogFile() { if( is_null( $this->log_file) ) { $this->log_file = rtrim(config('app.log_path') , DIRECTORY_SEPARATOR )."/"; } return $this->log_file; } }
After optimization
namespace App\Providers; use Illuminate\Support\ServiceProvider; use Monolog\Formatter\LineFormatter; use Monolog\Handler\RotatingFileHandler; use Carbon\Carbon; use Monolog\Logger;; use ReflectionClass; class LogServiceProvider extends ServiceProvider { protected $log_file; /** * Bootstrap any application services. * * @return void */ public function boot() { // } /** * Register any application services. * * @return void */ public function register() { $this->loadRequestId(); /** * Split logs according to date */ $this->useDailyFiles(); } protected function loadRequestId() { define( 'REQUEST_ID' , config('app.log_prefix').Carbon::now()->timestamp ); } /** * Split logs according to date */ protected function useDailyFiles() { $handler = $this->getDailyHandler()->setFormatter( $this->getDefaultFormatter() ); $errorHandler = $this->getDailyHandler(Logger::ERROR)->setFormatter( $this->getDefaultFormatter() ); $this->app->configureMonologUsing( function( $monolog) use ( $handler , $errorHandler ) { $monolog->pushHandler( $handler ); $monolog->pushHandler( $errorHandler ); }); } /** * Set log line format * @return LineFormatter */ protected function getDefaultFormatter() { $format = "[%datetime%] [".REQUEST_ID."] %channel%.%level_name%: %message% %context% %extra%\n"; return new LineFormatter( $format , null, true, true); } /** *Distinguish according to log * @return \Monolog\Handler\RotatingFileHandler */ protected function getDailyHandler( $level = Logger::DEBUG) { return new RotatingFileHandler( $this->logPath().$this->logName( $level ) , $this->maxFiles() , $level ); } /** * Maximum number of log files * @return int */ protected function maxFiles() { if ($this->app->bound('config')) { return $this->app->make('config')->get('app.log_max_files', 30); } return 0; } /** * Log file name * @return mixed */ protected function logPath() { $logPath = $this->app->storagePath()."/logs/"; if( $this->app->bound('config')) { $logPath = $this->app->make('config')->get('app.log_path', $logPath ); } return $logPath; } /** * log full file name * @param int $level * @return string */ protected function logName( $level = Logger::DEBUG ) { return $this->getAppName().'-'.$this->getLevelName( $level ).".log"; } /** * Get the project app * @return mixed */ protected function getAppName() { return $this->app->make('config')->get(''); } /** * Get log error level name * @param $level * @return mixed */ protected function getLevelName( $level ) { $r = new ReflectionClass( Logger::class ); $constants = array_flip( $r->getConstants() ); return $constants[$level]; } }
The above article is the implementation of customizing the Laravel (monolog) log location and adding the request ID. It is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.