自定义Laravel (monolog)日志位置,并增加请求ID的实现

2020-10-20 14:27:34 浏览数 (1)

修改 bootstrap/app.php 文件

代码语言:javascript复制
$app- configureMonologUsing(function($monolog) use ($app) {
 $monolog- pushHandler(
  (new MonologHandlerRotatingFileHandler(
   '/var/logs/app/laravel',
   $app- make('config')- get('app.log_max_files', 5)
  ))- setFormatter(new MonologFormatterLineFormatter(null, null, true, true))
 );
});

添加以后写入日志文件为:

代码语言:javascript复制
-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

参考:Laravel 的错误和日志记录

或者

创建appProvidersLogServiceProvider.php 文件

修改 configapp.php providers 增加

代码语言:javascript复制
AppProvidersLogServiceProvider::class

然后 AppProvidersLogServiceProvider.php 内容如下

代码语言:javascript复制
<?php

namespace AppProviders;

use IlluminateLogLogServiceProvider as SysServiceProvider;
use IlluminateLogWriter;

class LogServiceProvider extends SysServiceProvider
{
 protected function configureSingleHandler(Writer $log)
 {
  $log- useFiles(
   '/var/logs/app/laravel.log',
   $this- logLevel()
  );
 }

 protected function configureDailyHandler(Writer $log)
 {
  $log- useDailyFiles(
   '/var/logs/app/laravel.log', $this- maxFiles(),
   $this- logLevel()
  );
 }
}

增加请求ID request id

代码语言:javascript复制
namespace AppProviders;

use IlluminateSupportServiceProvider;
use CarbonCarbon;

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

 /**
  * 生成 request_id
  * @return void
  */
 protected function load_request_id()
 {
  define( 'REQUEST_ID' , config('app.log_prefix').Carbon::now()- timestamp );
 }
 
 /**
  * 注册 monolog pushHandler
  * @return void
  */
 protected function log_configure()
 {
  $log_file = $this- getLogFile();
  $log_max_files = $this- getLogMaxFiles();

  /**
   * @doc https://d.laravel-china.org/docs/5.4/errors#自定义-Monolog-设置
   */
  $this- app- configureMonologUsing(function($monolog) use ($log_file , $log_max_files) {
   $monolog- pushHandler(
    (new MonologHandlerRotatingFileHandler(
     $log_file ,
     $log_max_files
    ))- setFormatter(new MonologFormatterLineFormatter( "[�tetime%] [".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 )."/laravel.log";
  }
  return $this- log_file;
 }
}

优化以后

代码语言:javascript复制
namespace AppProviders;
use IlluminateSupportServiceProvider;
use MonologFormatterLineFormatter;
use MonologHandlerRotatingFileHandler;
use CarbonCarbon;
use MonologLogger;;
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();
/**
* 根据日期来分割日志
*/
$this- useDailyFiles();
}
protected function loadRequestId()
{
define( 'REQUEST_ID' , config('app.log_prefix').Carbon::now()- timestamp );
}
/**
* 根据日期来分割日志
*/
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 );
});
}
/**
* 设置 日志 行格式
* @return LineFormatter
*/
protected function getDefaultFormatter()
{
$format = "[�tetime%] [".REQUEST_ID."] %channel%.%level_name%: %message% %context% %extra%n";
return new LineFormatter( $format , null, true, true);
}
/**
* 根据日志区分
* @return MonologHandlerRotatingFileHandler
*/
protected function getDailyHandler( $level = Logger::DEBUG)
{
return new RotatingFileHandler(
$this- logPath().$this- logName( $level ) ,
$this- maxFiles() ,
$level
);
}
/**
* 日志文件最多个数
* @return int
*/
protected function maxFiles()
{
if ($this- app- bound('config')) {
return $this- app- make('config')- get('app.log_max_files', 30);
}
return 0;
}
/**
* 日志文件名称
* @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 完整文件名
* @param int $level
* @return string
*/
protected function logName( $level = Logger::DEBUG )
{
return $this- getAppName().'-'.$this- getLevelName( $level ).".log";
}
/**
* 获取项目app
* @return mixed
*/
protected function getAppName()
{
return $this- app- make('config')- get('app.name');
}
/**
* 获取log错误级别名称
* @param $level
* @return mixed
*/
protected function getLevelName( $level )
{
$r = new ReflectionClass( Logger::class );
$constants = array_flip( $r- getConstants() );
return $constants[$level];
}
}

以上这篇自定义Laravel (monolog)日志位置,并增加请求ID的实现就是小编分享给大家的全部内容了,希望能给大家一个参考。

0 人点赞