文件日志增加max_files参数配置 自动清理多余日志文件

This commit is contained in:
thinkphp
2018-03-02 14:42:43 +08:00
parent 47f79024fc
commit 728134a0d5

View File

@@ -25,6 +25,7 @@ class File
'file_size' => 2097152, 'file_size' => 2097152,
'path' => LOG_PATH, 'path' => LOG_PATH,
'apart_level' => [], 'apart_level' => [],
'max_files' => 0,
]; ];
protected $writed = []; protected $writed = [];
@@ -48,8 +49,20 @@ class File
if ($this->config['single']) { if ($this->config['single']) {
$destination = $this->config['path'] . 'single.log'; $destination = $this->config['path'] . 'single.log';
} else { } else {
$cli = IS_CLI ? '_cli' : ''; $cli = IS_CLI ? '_cli' : '';
$destination = $this->config['path'] . date('Ym') . DS . date('d') . $cli . '.log';
if ($this->config['max_files']) {
$filename = date('Ymd') . $cli . '.log';
$files = glob($this->config['path'] . '*.log');
if (count($files) > $this->config['max_files']) {
unlink($files[0]);
}
} else {
$filename = date('Ym') . '/' . date('d') . $cli . '.log';
}
$destination = $this->config['path'] . $filename;
} }
$path = dirname($destination); $path = dirname($destination);