From b39b4ed15d63fed3cc2c1fce1b4d74cda17ade34 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 10 Dec 2015 19:25:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=81=E8=A3=85trace=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E8=BF=9C=E7=A8=8B=E8=B0=83=E8=AF=95=E8=BE=93?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base.php | 12 ++++++++++++ library/think/slog.php | 34 +++++++++++++++++----------------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/base.php b/base.php index bfc1840b..2748993e 100644 --- a/base.php +++ b/base.php @@ -305,3 +305,15 @@ function S($name, $value = '', $options = null) return $cache->set($name, $value, $expire); } } + +/** + * 添加Trace记录到SocketLog + * @param mixed $log log信息 支持字符串和数组 + * @param string $level 日志级别 + * @param string $css 样式 + * @return void|array + */ +function trace($log,$level='log',$css='') +{ + return think\Slog::record($level,$log,$css); +} \ No newline at end of file diff --git a/library/think/slog.php b/library/think/slog.php index eca180d2..094edaab 100644 --- a/library/think/slog.php +++ b/library/think/slog.php @@ -9,10 +9,10 @@ use think\Exception; class Slog { - public static $start_time = 0; - public static $start_memory = 0; - public static $port = 1116; //SocketLog 服务的http的端口号 - public static $log_types = ['log', 'info', 'error', 'warn', 'table', 'group', 'groupCollapsed', 'groupEnd', 'alert']; + public static $start_time = 0; + public static $start_memory = 0; + public static $port = 1116; //SocketLog 服务的http的端口号 + public static $log_types = ['log', 'info', 'error', 'warn', 'table', 'alert']; protected static $config = [ 'enable' => true, //是否记录日志的开关 @@ -30,16 +30,16 @@ class Slog protected static $logs = []; protected static $css = [ - 'sql' => 'color:#009bb4;', - 'sql_warn' => 'color:#009bb4;font-size:14px;', - 'error_handler' => 'color:#f4006b;font-size:14px;', - 'page' => 'color:#40e2ff;background:#171717;', - 'big' => 'font-size:20px;color:red;', + 'sql' => 'color:#009bb4;', + 'sql_warn' => 'color:#009bb4;font-size:14px;', + 'error_handler' => 'color:#f4006b;font-size:14px;', + 'page' => 'color:#40e2ff;background:#171717;', + 'big' => 'font-size:20px;color:red;', ]; public static function sql($sql, $pdo) { - + if (is_object($pdo)) { if (!self::check()) { return; @@ -59,7 +59,7 @@ class Slog } self::sqlwhere($sql, $css); self::trace($sql, 2, $css); - }else{ + } else { throw new Exception('SocketLog can not support this database link'); } @@ -70,7 +70,7 @@ class Slog if (!self::check()) { return; } - self::groupCollapsed($msg, $css); + self::record('groupCollapsed', $msg, $css); $traces = debug_backtrace(false); $traces = array_reverse($traces); $max = count($traces) - $trace_level; @@ -81,11 +81,11 @@ class Slog $line = isset($trace['line']) ? $trace['line'] : 'unknown line'; $trace_msg = '#' . $i . ' ' . $fun . ' called at [' . $file . ':' . $line . ']'; if (!empty($trace['args'])) { - self::record('groupCollapsed',$trace_msg); - self::record('log',$trace['args']); + self::record('groupCollapsed', $trace_msg); + self::record('log', $trace['args']); self::record('groupEnd'); } else { - self::record('log',$trace_msg); + self::record('log', $trace_msg); } } self::record('groupEnd'); @@ -183,7 +183,7 @@ class Slog // 保存日志记录 if ($e = error_get_last()) { self::error_handler($e['type'], $e['message'], $e['file'], $e['line']); - self::sendLog(); + self::sendLog(); } } @@ -375,7 +375,7 @@ class Slog { if (in_array($method, self::$log_types)) { array_unshift($args, $method); - return call_user_func_array(['\think\Slog','record'], $args); + return call_user_func_array(['\think\Slog', 'record'], $args); } }