LogService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2018/10/23
  6. * Time: 上午10:28
  7. */
  8. namespace app\main\service;
  9. use think\Log;
  10. class LogService
  11. {
  12. /**
  13. * 细节级别
  14. * @param $msg
  15. */
  16. public static function debug($msg)
  17. {
  18. //启用debug才会打印此日志
  19. // if(Config::get('app_debug')){
  20. self::log($msg, 'debug');
  21. // }
  22. }
  23. /**
  24. * 了解级别
  25. * @param $msg
  26. */
  27. public static function info($msg)
  28. {
  29. self::log($msg, 'info');
  30. }
  31. /**
  32. * 通知级别
  33. * @param $msg
  34. */
  35. public static function notice($msg)
  36. {
  37. self::log($msg, 'notice');
  38. }
  39. /**
  40. * 错误级别
  41. * @param $msg
  42. */
  43. public static function error($msg)
  44. {
  45. self::log($msg, 'error');
  46. }
  47. /**
  48. * 告警级别
  49. * @param string $msg
  50. */
  51. public static function alert($msg)
  52. {
  53. self::log($msg, 'alert');
  54. }
  55. /**
  56. * 紧急级别
  57. * @param string $msg
  58. */
  59. public static function critical($msg)
  60. {
  61. self::log($msg, 'alert');
  62. }
  63. /**
  64. * @param string $msg
  65. * @param string $level
  66. */
  67. public static function log($msg, $level)
  68. {
  69. $msg = " [ pid:" . getmypid() . "] " . $msg;
  70. Log::record($msg, $level);
  71. }
  72. /**
  73. * @param \Exception $e
  74. * @return array
  75. */
  76. public static function exception(\Exception $e)
  77. {
  78. $msg = json_encode([
  79. $e->getMessage(),
  80. $e->getFile(),
  81. $e->getLine(),
  82. ], JSON_UNESCAPED_UNICODE);
  83. $stackList = array_slice($e->getTrace(), 0, 10);
  84. $stackList = self::getStackTree($stackList);
  85. $msg .= ' [ stack ] ' . json_encode($stackList);
  86. if (strstr($e->getMessage(), 'module not exists:')) {
  87. return self::toError($e->getCode(), $msg, 'info');
  88. } else {
  89. return self::toError($e->getCode(), $msg, 'error');
  90. }
  91. }
  92. /**
  93. * 堆栈参数处理
  94. * @param $list
  95. * @return mixed
  96. */
  97. public static function getStackTree($list)
  98. {
  99. foreach ($list as $index => $item) {
  100. if (array_key_exists('args', $item)) {
  101. foreach ($item['args'] as $arg_index => $arg_value) {
  102. if (is_array($arg_value)) {
  103. $arg_value = json_encode($arg_value, JSON_UNESCAPED_UNICODE);
  104. }
  105. if (is_string($arg_value) || is_numeric($arg_value)) {
  106. if (strlen($arg_value) > 200) {
  107. $item['args'][$arg_index] = 'length>200';
  108. }
  109. } else {
  110. $item['args'][$arg_index] = 'mix';
  111. }
  112. }
  113. $list[$index] = $item;
  114. }
  115. }
  116. return $list;
  117. }
  118. /**
  119. * 格式化错误返回值
  120. * @param $error_code
  121. * @param $error_msg
  122. * @param string $level
  123. * @return array
  124. */
  125. public static function toError($error_code, $error_msg, $level = 'error')
  126. {
  127. if (is_array($error_msg)) {
  128. $error_msg = json_encode($error_msg);
  129. }
  130. $return = ['error' => $error_code, 'msg' => $error_msg];
  131. //将进程id写入日志,方便定位
  132. self::log("[ errcode: $error_code ] [ errmsg: $error_msg ]", $level);
  133. return $return;
  134. }
  135. }