部署模式下简化日志记录

This commit is contained in:
thinkphp
2016-12-01 18:32:28 +08:00
parent b62175c34d
commit 4a80099a51
3 changed files with 51 additions and 38 deletions

View File

@@ -184,7 +184,7 @@ class Log
self::init(Config::get('log')); self::init(Config::get('log'));
} }
// 写入日志 // 写入日志
return self::$driver->save($log); return self::$driver->save($log, false);
} }
/** /**

View File

@@ -11,6 +11,8 @@
namespace think\log\driver; namespace think\log\driver;
use think\App;
/** /**
* 本地化调试输出到文件 * 本地化调试输出到文件
*/ */
@@ -35,9 +37,10 @@ class File
* 日志写入接口 * 日志写入接口
* @access public * @access public
* @param array $log 日志信息 * @param array $log 日志信息
* @param bool $depr 是否写入分割线
* @return bool * @return bool
*/ */
public function save(array $log = []) public function save(array $log = [], $depr = true)
{ {
$now = date($this->config['time_format']); $now = date($this->config['time_format']);
$destination = $this->config['path'] . date('Ym') . DS . date('d') . '.log'; $destination = $this->config['path'] . date('Ym') . DS . date('d') . '.log';
@@ -50,6 +53,9 @@ class File
rename($destination, dirname($destination) . DS . $_SERVER['REQUEST_TIME'] . '-' . basename($destination)); rename($destination, dirname($destination) . DS . $_SERVER['REQUEST_TIME'] . '-' . basename($destination));
} }
$depr = $depr ? "---------------------------------------------------------------\r\n" : '';
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'];
@@ -69,6 +75,7 @@ class File
$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'] : '';
}
foreach ($log as $type => $val) { foreach ($log as $type => $val) {
$level = ''; $level = '';
foreach ($val as $msg) { foreach ($val as $msg) {
@@ -80,12 +87,15 @@ 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 . '.log';
error_log("[{$now}] {$server} {$remote} {$method} {$uri}\r\n{$level}\r\n---------------------------------------------------------------\r\n", 3, $filename); error_log("[{$now}] {$level}\r\n{$depr}", 3, $filename);
} else { } else {
$info .= $level; $info .= $level;
} }
} }
return error_log("[{$now}] {$server} {$remote} {$method} {$uri}\r\n{$info}\r\n---------------------------------------------------------------\r\n", 3, $destination); if (App::$debug) {
$info = "{$server} {$remote} {$method} {$uri}\r\n" . $info;
}
return error_log("[{$now}] {$info}\r\n{$depr}", 3, $destination);
} }
} }

View File

@@ -63,6 +63,8 @@ class Socket
if (!$this->check()) { if (!$this->check()) {
return false; return false;
} }
$trace = [];
if (App::$debug) {
$runtime = number_format(microtime(true) - THINK_START_TIME, 10); $runtime = number_format(microtime(true) - THINK_START_TIME, 10);
$reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞'; $reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞';
$time_str = ' [运行时间:' . number_format($runtime, 6) . 's][吞吐率:' . $reqs . 'req/s]'; $time_str = ' [运行时间:' . number_format($runtime, 6) . 's][吞吐率:' . $reqs . 'req/s]';
@@ -81,6 +83,7 @@ class Socket
'msg' => $current_uri . $time_str . $memory_str . $file_load, 'msg' => $current_uri . $time_str . $memory_str . $file_load,
'css' => $this->css['page'], 'css' => $this->css['page'],
]; ];
}
foreach ($log as $type => $val) { foreach ($log as $type => $val) {
$trace[] = [ $trace[] = [