File日志驱动增加single参数配置是否记录单个文件日志

This commit is contained in:
thinkphp
2017-11-14 17:34:56 +08:00
parent f8c63f7167
commit be7a257e5f

View File

@@ -23,6 +23,7 @@ class File
'file_size' => 2097152, 'file_size' => 2097152,
'path' => LOG_PATH, 'path' => LOG_PATH,
'apart_level' => [], 'apart_level' => [],
'single' => false,
]; ];
protected $writed = []; protected $writed = [];
@@ -43,8 +44,13 @@ class File
*/ */
public function save(array $log = []) public function save(array $log = [])
{ {
$cli = IS_CLI ? '_cli' : ''; if ($this->config['single']) {
$destination = $this->config['path'] . date('Ym') . DS . date('d') . $cli . '.log'; $destination = $this->config['path'] . 'single.log';
} else {
$cli = IS_CLI ? '_cli' : '';
$destination = $this->config['path'] . date('Ym') . DS . date('d') . $cli . '.log';
}
$path = dirname($destination); $path = dirname($destination);
!is_dir($path) && mkdir($path, 0755, true); !is_dir($path) && mkdir($path, 0755, true);
@@ -60,7 +66,11 @@ class File
} }
if (in_array($type, $this->config['apart_level'])) { if (in_array($type, $this->config['apart_level'])) {
// 独立记录的日志级别 // 独立记录的日志级别
$filename = $path . DS . date('d') . '_' . $type . $cli . '.log'; if ($this->config['single']) {
$filename = $path . $type . '.log';
} else {
$filename = $path . DS . date('d') . '_' . $type . $cli . '.log';
}
$this->write($level, $filename, true); $this->write($level, $filename, true);
} else { } else {
$info .= $level; $info .= $level;
@@ -75,7 +85,7 @@ class File
protected function write($message, $destination, $apart = false) protected function write($message, $destination, $apart = false)
{ {
//检测日志文件大小,超过配置大小则备份日志文件重新生成 //检测日志文件大小,超过配置大小则备份日志文件重新生成
if (is_file($destination) && floor($this->config['file_size']) <= filesize($destination)) { if (!$this->config['single'] && is_file($destination) && floor($this->config['file_size']) <= filesize($destination)) {
rename($destination, dirname($destination) . DS . time() . '-' . basename($destination)); rename($destination, dirname($destination) . DS . time() . '-' . basename($destination));
$this->writed[$destination] = false; $this->writed[$destination] = false;
} }