123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2018/10/23
- * Time: 上午10:28
- */
- namespace app\main\service;
- use think\Log;
- class LogService
- {
- /**
- * 细节级别
- * @param $msg
- */
- public static function debug($msg)
- {
- //启用debug才会打印此日志
- // if(Config::get('app_debug')){
- self::log($msg, 'debug');
- // }
- }
- /**
- * 了解级别
- * @param $msg
- */
- public static function info($msg)
- {
- self::log($msg, 'info');
- }
- /**
- * 通知级别
- * @param $msg
- */
- public static function notice($msg)
- {
- self::log($msg, 'notice');
- }
- /**
- * 错误级别
- * @param $msg
- */
- public static function error($msg)
- {
- self::log($msg, 'error');
- }
- /**
- * 告警级别
- * @param string $msg
- */
- public static function alert($msg)
- {
- self::log($msg, 'alert');
- }
- /**
- * 紧急级别
- * @param string $msg
- */
- public static function critical($msg)
- {
- self::log($msg, 'alert');
- }
- /**
- * @param string $msg
- * @param string $level
- */
- public static function log($msg, $level)
- {
- $msg = " [ pid:" . getmypid() . "] " . $msg;
- Log::record($msg, $level);
- }
- /**
- * @param \Exception $e
- * @return array
- */
- public static function exception(\Exception $e)
- {
- $msg = json_encode([
- $e->getMessage(),
- $e->getFile(),
- $e->getLine(),
- ], JSON_UNESCAPED_UNICODE);
- $stackList = array_slice($e->getTrace(), 0, 10);
- $stackList = self::getStackTree($stackList);
- $msg .= ' [ stack ] ' . json_encode($stackList);
- if (strstr($e->getMessage(), 'module not exists:')) {
- return self::toError($e->getCode(), $msg, 'info');
- } else {
- return self::toError($e->getCode(), $msg, 'error');
- }
- }
- /**
- * 堆栈参数处理
- * @param $list
- * @return mixed
- */
- public static function getStackTree($list)
- {
- foreach ($list as $index => $item) {
- if (array_key_exists('args', $item)) {
- foreach ($item['args'] as $arg_index => $arg_value) {
- if (is_array($arg_value)) {
- $arg_value = json_encode($arg_value, JSON_UNESCAPED_UNICODE);
- }
- if (is_string($arg_value) || is_numeric($arg_value)) {
- if (strlen($arg_value) > 200) {
- $item['args'][$arg_index] = 'length>200';
- }
- } else {
- $item['args'][$arg_index] = 'mix';
- }
- }
- $list[$index] = $item;
- }
- }
- return $list;
- }
- /**
- * 格式化错误返回值
- * @param $error_code
- * @param $error_msg
- * @param string $level
- * @return array
- */
- public static function toError($error_code, $error_msg, $level = 'error')
- {
- if (is_array($error_msg)) {
- $error_msg = json_encode($error_msg);
- }
- $return = ['error' => $error_code, 'msg' => $error_msg];
- //将进程id写入日志,方便定位
- self::log("[ errcode: $error_code ] [ errmsg: $error_msg ]", $level);
- return $return;
- }
- }
|