修改 bootstrap/app.php 文件
$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))
);
});
添加以后寫(xiě)入日志文件為:
-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 的錯(cuò)誤和日志記錄
或者
創(chuàng)建app\Providers\LogServiceProvider.php 文件
修改 config\app.php providers 增加
App\Providers\LogServiceProvider::class
然后 App\Providers\LogServiceProvider.php 內(nèi)容如下
?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/laravel.log',
$this->logLevel()
);
}
protected function configureDailyHandler(Writer $log)
{
$log->useDailyFiles(
'/var/logs/app/laravel.log', $this->maxFiles(),
$this->logLevel()
);
}
}
增加請(qǐng)求ID 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();
}
/**
* 生成 request_id
* @return void
*/
protected function load_request_id()
{
define( 'REQUEST_ID' , config('app.log_prefix').Carbon::now()->timestamp );
}
/**
* 注冊(cè) 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-設(shè)置
*/
$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 )."/laravel.log";
}
return $this->log_file;
}
}
優(yōu)化以后
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();
/**
* 根據(jù)日期來(lái)分割日志
*/
$this->useDailyFiles();
}
protected function loadRequestId()
{
define( 'REQUEST_ID' , config('app.log_prefix').Carbon::now()->timestamp );
}
/**
* 根據(jù)日期來(lái)分割日志
*/
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 );
});
}
/**
* 設(shè)置 日志 行格式
* @return LineFormatter
*/
protected function getDefaultFormatter()
{
$format = "[%datetime%] [".REQUEST_ID."] %channel%.%level_name%: %message% %context% %extra%\n";
return new LineFormatter( $format , null, true, true);
}
/**
* 根據(jù)日志區(qū)分
* @return \Monolog\Handler\RotatingFileHandler
*/
protected function getDailyHandler( $level = Logger::DEBUG)
{
return new RotatingFileHandler(
$this->logPath().$this->logName( $level ) ,
$this->maxFiles() ,
$level
);
}
/**
* 日志文件最多個(gè)數(shù)
* @return int
*/
protected function maxFiles()
{
if ($this->app->bound('config')) {
return $this->app->make('config')->get('app.log_max_files', 30);
}
return 0;
}
/**
* 日志文件名稱(chēng)
* @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";
}
/**
* 獲取項(xiàng)目app
* @return mixed
*/
protected function getAppName()
{
return $this->app->make('config')->get('app.name');
}
/**
* 獲取log錯(cuò)誤級(jí)別名稱(chēng)
* @param $level
* @return mixed
*/
protected function getLevelName( $level )
{
$r = new ReflectionClass( Logger::class );
$constants = array_flip( $r->getConstants() );
return $constants[$level];
}
}
以上這篇自定義Laravel (monolog)日志位置,并增加請(qǐng)求ID的實(shí)現(xiàn)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:- laravel日志優(yōu)化實(shí)例講解
- 解決laravel中日志權(quán)限莫名變成了root的問(wèn)題
- 關(guān)于laravel 日志寫(xiě)入失敗問(wèn)題匯總
- Laravel 5.5 異常處理 & 錯(cuò)誤日志的解決
- Laravel 重寫(xiě)日志,讓日志更優(yōu)雅