改进日志记录格式

This commit is contained in:
yunwuxin
2017-03-12 23:22:43 +08:00
parent af635db08f
commit 51f4e47dbe
2 changed files with 65 additions and 42 deletions

View File

@@ -84,7 +84,7 @@ class Log
public static function record($msg, $type = 'log') public static function record($msg, $type = 'log')
{ {
self::$log[$type][] = $msg; self::$log[$type][] = $msg;
if (IS_CLI && count(self::$log[$type]) > 100) { if (IS_CLI) {
// 命令行下面日志写入改进 // 命令行下面日志写入改进
self::save(); self::save();
} }
@@ -173,6 +173,7 @@ class Log
*/ */
public static function write($msg, $type = 'log', $force = false) public static function write($msg, $type = 'log', $force = false)
{ {
$log = self::$log;
// 封装日志信息 // 封装日志信息
if (true === $force || empty(self::$config['level'])) { if (true === $force || empty(self::$config['level'])) {
$log[$type][] = $msg; $log[$type][] = $msg;
@@ -188,7 +189,11 @@ class Log
self::init(Config::get('log')); self::init(Config::get('log'));
} }
// 写入日志 // 写入日志
return self::$driver->save($log, false); $result = self::$driver->save($log);
if ($result) {
self::$log = [];
}
return $result;
} }
/** /**

View File

@@ -25,6 +25,8 @@ class File
'apart_level' => [], 'apart_level' => [],
]; ];
protected $writed = [];
// 实例化并传入参数 // 实例化并传入参数
public function __construct($config = []) public function __construct($config = [])
{ {
@@ -37,25 +39,49 @@ class File
* 日志写入接口 * 日志写入接口
* @access public * @access public
* @param array $log 日志信息 * @param array $log 日志信息
* @param bool $depr 是否写入分割线
* @return bool * @return bool
*/ */
public function save(array $log = [], $depr = true) public function save(array $log = [])
{ {
$now = date($this->config['time_format']); $cli = IS_CLI ? '_cli' : '';
$destination = $this->config['path'] . date('Ym') . DS . date('d') . '.log'; $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);
$info = '';
foreach ($log as $type => $val) {
$level = '';
foreach ($val as $msg) {
if (!is_string($msg)) {
$msg = var_export($msg, true);
}
$level .= '[ ' . $type . ' ] ' . $msg . "\r\n";
}
if (in_array($type, $this->config['apart_level'])) {
// 独立记录的日志级别
$filename = $path . DS . date('d') . '_' . $type . $cli . '.log';
$this->write($level, $filename, true);
} else {
$info .= $level;
}
}
if ($info) {
return $this->write($info, $destination);
}
return true;
}
protected function write($message, $destination, $apart = false)
{
//检测日志文件大小,超过配置大小则备份日志文件重新生成 //检测日志文件大小,超过配置大小则备份日志文件重新生成
if (is_file($destination) && floor($this->config['file_size']) <= filesize($destination)) { if (is_file($destination) && floor($this->config['file_size']) <= filesize($destination)) {
rename($destination, dirname($destination) . DS . $_SERVER['REQUEST_TIME'] . '-' . basename($destination)); rename($destination, dirname($destination) . DS . $_SERVER['REQUEST_TIME'] . '-' . basename($destination));
$this->writed[$destination] = false;
} }
$depr = $depr ? "---------------------------------------------------------------\r\n" : ''; if (empty($this->writed[$destination]) && !IS_CLI) {
$info = ''; if (App::$debug && !$apart) {
if (App::$debug) {
// 获取基本信息 // 获取基本信息
if (isset($_SERVER['HTTP_HOST'])) { if (isset($_SERVER['HTTP_HOST'])) {
$current_uri = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $current_uri = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
@@ -70,32 +96,24 @@ class File
$memory_str = ' [内存消耗:' . $memory_use . 'kb]'; $memory_str = ' [内存消耗:' . $memory_use . 'kb]';
$file_load = ' [文件加载:' . count(get_included_files()) . ']'; $file_load = ' [文件加载:' . count(get_included_files()) . ']';
$info = '[ log ] ' . $current_uri . $time_str . $memory_str . $file_load . "\r\n"; $message = '[ info ] ' . $current_uri . $time_str . $memory_str . $file_load . "\r\n" . $message;
}
$now = date($this->config['time_format']);
$server = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : '0.0.0.0'; $server = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : '0.0.0.0';
$remote = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0'; $remote = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0';
$method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'CLI'; $method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'CLI';
$uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''; $uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
$message = "---------------------------------------------------------------\r\n[{$now}] {$server} {$remote} {$method} {$uri}\r\n" . $message;
$this->writed[$destination] = true;
} }
foreach ($log as $type => $val) {
$level = ''; if (IS_CLI) {
foreach ($val as $msg) { $now = date($this->config['time_format']);
if (!is_string($msg)) { $message = "[{$now}]" . $message;
$msg = var_export($msg, true);
} }
$level .= '[ ' . $type . ' ] ' . $msg . "\r\n";
} return error_log($message, 3, $destination);
if (in_array($type, $this->config['apart_level'])) {
// 独立记录的日志级别
$filename = $path . DS . date('d') . '_' . $type . '.log';
error_log("[{$now}] {$level}\r\n{$depr}", 3, $filename);
} else {
$info .= $level;
}
}
if (App::$debug) {
$info = "{$server} {$remote} {$method} {$uri}\r\n" . $info;
}
return error_log("[{$now}] {$info}\r\n{$depr}", 3, $destination);
} }
} }