mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 07:12:47 +08:00
改进日志记录格式
This commit is contained in:
@@ -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();
|
||||||
}
|
}
|
||||||
@@ -101,7 +101,7 @@ class Log
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 当前日志记录的授权key
|
* 当前日志记录的授权key
|
||||||
* @param string $key 授权key
|
* @param string $key 授权key
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function key($key)
|
public static function key($key)
|
||||||
@@ -111,7 +111,7 @@ class Log
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查日志写入权限
|
* 检查日志写入权限
|
||||||
* @param array $config 当前日志配置参数
|
* @param array $config 当前日志配置参数
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public static function check($config)
|
public static function check($config)
|
||||||
@@ -166,13 +166,14 @@ class Log
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 实时写入日志信息 并支持行为
|
* 实时写入日志信息 并支持行为
|
||||||
* @param mixed $msg 调试信息
|
* @param mixed $msg 调试信息
|
||||||
* @param string $type 信息类型
|
* @param string $type 信息类型
|
||||||
* @param bool $force 是否强制写入
|
* @param bool $force 是否强制写入
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -25,6 +25,8 @@ class File
|
|||||||
'apart_level' => [],
|
'apart_level' => [],
|
||||||
];
|
];
|
||||||
|
|
||||||
|
protected $writed = [];
|
||||||
|
|
||||||
// 实例化并传入参数
|
// 实例化并传入参数
|
||||||
public function __construct($config = [])
|
public function __construct($config = [])
|
||||||
{
|
{
|
||||||
@@ -37,45 +39,17 @@ 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);
|
||||||
|
|
||||||
//检测日志文件大小,超过配置大小则备份日志文件重新生成
|
|
||||||
if (is_file($destination) && floor($this->config['file_size']) <= filesize($destination)) {
|
|
||||||
rename($destination, dirname($destination) . DS . $_SERVER['REQUEST_TIME'] . '-' . basename($destination));
|
|
||||||
}
|
|
||||||
|
|
||||||
$depr = $depr ? "---------------------------------------------------------------\r\n" : '';
|
|
||||||
$info = '';
|
$info = '';
|
||||||
if (App::$debug) {
|
|
||||||
// 获取基本信息
|
|
||||||
if (isset($_SERVER['HTTP_HOST'])) {
|
|
||||||
$current_uri = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
|
|
||||||
} else {
|
|
||||||
$current_uri = "cmd:" . implode(' ', $_SERVER['argv']);
|
|
||||||
}
|
|
||||||
|
|
||||||
$runtime = round(microtime(true) - THINK_START_TIME, 10);
|
|
||||||
$reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞';
|
|
||||||
$time_str = ' [运行时间:' . number_format($runtime, 6) . 's][吞吐率:' . $reqs . 'req/s]';
|
|
||||||
$memory_use = number_format((memory_get_usage() - THINK_START_MEM) / 1024, 2);
|
|
||||||
$memory_str = ' [内存消耗:' . $memory_use . 'kb]';
|
|
||||||
$file_load = ' [文件加载:' . count(get_included_files()) . ']';
|
|
||||||
|
|
||||||
$info = '[ log ] ' . $current_uri . $time_str . $memory_str . $file_load . "\r\n";
|
|
||||||
$server = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_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';
|
|
||||||
$uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
|
|
||||||
}
|
|
||||||
foreach ($log as $type => $val) {
|
foreach ($log as $type => $val) {
|
||||||
$level = '';
|
$level = '';
|
||||||
foreach ($val as $msg) {
|
foreach ($val as $msg) {
|
||||||
@@ -86,16 +60,60 @@ class File
|
|||||||
}
|
}
|
||||||
if (in_array($type, $this->config['apart_level'])) {
|
if (in_array($type, $this->config['apart_level'])) {
|
||||||
// 独立记录的日志级别
|
// 独立记录的日志级别
|
||||||
$filename = $path . DS . date('d') . '_' . $type . '.log';
|
$filename = $path . DS . date('d') . '_' . $type . $cli . '.log';
|
||||||
error_log("[{$now}] {$level}\r\n{$depr}", 3, $filename);
|
$this->write($level, $filename, true);
|
||||||
} else {
|
} else {
|
||||||
$info .= $level;
|
$info .= $level;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (App::$debug) {
|
if ($info) {
|
||||||
$info = "{$server} {$remote} {$method} {$uri}\r\n" . $info;
|
return $this->write($info, $destination);
|
||||||
}
|
}
|
||||||
return error_log("[{$now}] {$info}\r\n{$depr}", 3, $destination);
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function write($message, $destination, $apart = false)
|
||||||
|
{
|
||||||
|
//检测日志文件大小,超过配置大小则备份日志文件重新生成
|
||||||
|
if (is_file($destination) && floor($this->config['file_size']) <= filesize($destination)) {
|
||||||
|
rename($destination, dirname($destination) . DS . $_SERVER['REQUEST_TIME'] . '-' . basename($destination));
|
||||||
|
$this->writed[$destination] = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($this->writed[$destination]) && !IS_CLI) {
|
||||||
|
if (App::$debug && !$apart) {
|
||||||
|
// 获取基本信息
|
||||||
|
if (isset($_SERVER['HTTP_HOST'])) {
|
||||||
|
$current_uri = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
|
||||||
|
} else {
|
||||||
|
$current_uri = "cmd:" . implode(' ', $_SERVER['argv']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$runtime = round(microtime(true) - THINK_START_TIME, 10);
|
||||||
|
$reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞';
|
||||||
|
$time_str = ' [运行时间:' . number_format($runtime, 6) . 's][吞吐率:' . $reqs . 'req/s]';
|
||||||
|
$memory_use = number_format((memory_get_usage() - THINK_START_MEM) / 1024, 2);
|
||||||
|
$memory_str = ' [内存消耗:' . $memory_use . 'kb]';
|
||||||
|
$file_load = ' [文件加载:' . count(get_included_files()) . ']';
|
||||||
|
|
||||||
|
$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';
|
||||||
|
$remote = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0';
|
||||||
|
$method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'CLI';
|
||||||
|
$uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
|
||||||
|
$message = "---------------------------------------------------------------\r\n[{$now}] {$server} {$remote} {$method} {$uri}\r\n" . $message;
|
||||||
|
|
||||||
|
$this->writed[$destination] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IS_CLI) {
|
||||||
|
$now = date($this->config['time_format']);
|
||||||
|
$message = "[{$now}]" . $message;
|
||||||
|
}
|
||||||
|
|
||||||
|
return error_log($message, 3, $destination);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user