feat: 修改日志处理类以支持不同级别的日志文件
This commit is contained in:
parent
4b2c385b8b
commit
cf8c5e8f64
@ -16,11 +16,23 @@ return [
|
|||||||
'default' => [
|
'default' => [
|
||||||
'handlers' => [
|
'handlers' => [
|
||||||
[
|
[
|
||||||
'class' => Monolog\Handler\RotatingFileHandler::class,
|
// 'class' => Monolog\Handler\RotatingFileHandler::class,
|
||||||
|
// 'constructor' => [
|
||||||
|
// runtime_path() . '/logs/'.date('Ym').'/.log',
|
||||||
|
// 2048,
|
||||||
|
// Monolog\Logger::INFO,
|
||||||
|
// true,
|
||||||
|
// 0755
|
||||||
|
// ],
|
||||||
|
// 'formatter' => [
|
||||||
|
// 'class' => Monolog\Formatter\LineFormatter::class,
|
||||||
|
// 'constructor' => [null, 'Y-m-d H:i:s', true,true],
|
||||||
|
// ],
|
||||||
|
'class' => \support\log\MonologExtendHandler::class,
|
||||||
'constructor' => [
|
'constructor' => [
|
||||||
runtime_path() . '/logs/'.date('Ym').'/.log',
|
null,
|
||||||
2048,
|
2048,
|
||||||
Monolog\Logger::INFO,
|
Monolog\Logger::DEBUG,
|
||||||
true,
|
true,
|
||||||
0755
|
0755
|
||||||
],
|
],
|
||||||
|
@ -19,8 +19,10 @@ return [
|
|||||||
'class' => \support\log\MonologExtendHandler::class,
|
'class' => \support\log\MonologExtendHandler::class,
|
||||||
'constructor' => [
|
'constructor' => [
|
||||||
'redis-queue',
|
'redis-queue',
|
||||||
7, //$maxFiles
|
2048,
|
||||||
Monolog\Logger::NOTICE,
|
Monolog\Logger::DEBUG,
|
||||||
|
true,
|
||||||
|
0755
|
||||||
],
|
],
|
||||||
'formatter' => [
|
'formatter' => [
|
||||||
'class' => Monolog\Formatter\LineFormatter::class,
|
'class' => Monolog\Formatter\LineFormatter::class,
|
||||||
|
@ -1,11 +1,15 @@
|
|||||||
<?php
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: hjl
|
||||||
|
* Date: 2023/6/21
|
||||||
|
* Time: 07:25
|
||||||
|
*/
|
||||||
|
|
||||||
namespace support\log;
|
namespace support\log;
|
||||||
|
|
||||||
use Monolog\Handler\StreamHandler;
|
use Monolog\Handler\StreamHandler;
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
use Monolog\DateFormatter;
|
|
||||||
use DateTime;
|
|
||||||
|
|
||||||
class MonologExtendHandler extends StreamHandler
|
class MonologExtendHandler extends StreamHandler
|
||||||
{
|
{
|
||||||
@ -13,6 +17,7 @@ class MonologExtendHandler extends StreamHandler
|
|||||||
protected bool $mustRotate;
|
protected bool $mustRotate;
|
||||||
protected string $runtimeLogPath;
|
protected string $runtimeLogPath;
|
||||||
protected string|null $channelDirName;
|
protected string|null $channelDirName;
|
||||||
|
protected $level;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $channelDirName 日志通道路径
|
* @param string $channelDirName 日志通道路径
|
||||||
@ -28,6 +33,7 @@ class MonologExtendHandler extends StreamHandler
|
|||||||
{
|
{
|
||||||
$this->runtimeLogPath = runtime_path() . '/logs/';
|
$this->runtimeLogPath = runtime_path() . '/logs/';
|
||||||
$this->channelDirName = $channelDirName;
|
$this->channelDirName = $channelDirName;
|
||||||
|
$this->level=$level;
|
||||||
$dateDir = date('Ym').'/';
|
$dateDir = date('Ym').'/';
|
||||||
$filename = date('d') .'.log';
|
$filename = date('d') .'.log';
|
||||||
$fullFilePath = empty($channelDirName) ? $this->runtimeLogPath . $dateDir .$filename : $this->runtimeLogPath . $this->channelDirName . '/' . $dateDir . $filename;
|
$fullFilePath = empty($channelDirName) ? $this->runtimeLogPath . $dateDir .$filename : $this->runtimeLogPath . $this->channelDirName . '/' . $dateDir . $filename;
|
||||||
@ -97,12 +103,16 @@ class MonologExtendHandler extends StreamHandler
|
|||||||
}
|
}
|
||||||
$dateDir = date('Ym') . '/';
|
$dateDir = date('Ym') . '/';
|
||||||
$logBasePath = empty($this->channelDirName) ? $this->runtimeLogPath . $dateDir : $this->runtimeLogPath . $this->channelDirName . '/' . $dateDir;
|
$logBasePath = empty($this->channelDirName) ? $this->runtimeLogPath . $dateDir : $this->runtimeLogPath . $this->channelDirName . '/' . $dateDir;
|
||||||
$filename = date('d').'.log';
|
if($this->level==200){
|
||||||
|
$filename = date('d').'.log';
|
||||||
|
}else{
|
||||||
|
$filename = date('d').'_error.log';
|
||||||
|
}
|
||||||
$fullLogFilename = $logBasePath . $filename;
|
$fullLogFilename = $logBasePath . $filename;
|
||||||
// archive latest file
|
// archive latest file
|
||||||
clearstatcache(true, $fullLogFilename);
|
clearstatcache(true, $fullLogFilename);
|
||||||
if (file_exists($fullLogFilename)) {
|
if (file_exists($fullLogFilename)) {
|
||||||
$target = $logBasePath. date('Y-m') . '_' . $filename;
|
$target = $logBasePath. $filename;
|
||||||
rename($fullLogFilename, $target);
|
rename($fullLogFilename, $target);
|
||||||
}else{
|
}else{
|
||||||
if (!is_dir($logBasePath))
|
if (!is_dir($logBasePath))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user