Log.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think;
  12. use think\exception\ClassNotFoundException;
  13. /**
  14. * Class Log
  15. * @package think
  16. *
  17. * @method void log($msg) static 记录一般日志
  18. * @method void error($msg) static 记录错误日志
  19. * @method void info($msg) static 记录一般信息日志
  20. * @method void sql($msg) static 记录 SQL 查询日志
  21. * @method void notice($msg) static 记录提示日志
  22. * @method void alert($msg) static 记录报警日志
  23. * Modified by wuyonghong BEGIN 2018/12/19 ISSUES 添加日志类型
  24. * @method void queue($msg) static 记录队列日志
  25. * @method void wechat($msg) static 记录微信日志
  26. * END 2018/12/19 ISSUES
  27. */
  28. class Log
  29. {
  30. const LOG = 'log';
  31. const ERROR = 'error';
  32. const INFO = 'info';
  33. const SQL = 'sql';
  34. const NOTICE = 'notice';
  35. const ALERT = 'alert';
  36. const DEBUG = 'debug';
  37. /* Modified by wuyonghong BEGIN 2018/12/19 ISSUES 添加日志类型 */
  38. const QUEUE = 'queue';
  39. const WECHAT = 'wechat';
  40. /* END 2018/12/19 ISSUES: */
  41. /**
  42. * @var array 日志信息
  43. */
  44. protected static $log = [];
  45. /**
  46. * @var array 配置参数
  47. */
  48. protected static $config = [];
  49. /**
  50. * @var array 日志类型
  51. */
  52. /* Modified by wuyonghong BEGIN 2018/12/19 ISSUES 添加日志类型 */
  53. protected static $type = ['log', 'error', 'info', 'sql', 'notice', 'alert', 'debug', 'queue', 'wechat'];
  54. /* END 2018/12/19 ISSUES: */
  55. /**
  56. * @var log\driver\File|log\driver\Test|log\driver\Socket 日志写入驱动
  57. */
  58. protected static $driver;
  59. /**
  60. * @var string 当前日志授权 key
  61. */
  62. protected static $key;
  63. /**
  64. * 日志初始化
  65. * @access public
  66. * @param array $config 配置参数
  67. * @return void
  68. */
  69. public static function init($config = [])
  70. {
  71. $type = isset($config['type']) ? $config['type'] : 'File';
  72. $class = false !== strpos($type, '\\') ? $type : '\\think\\log\\driver\\' . ucwords($type);
  73. self::$config = $config;
  74. unset($config['type']);
  75. if (class_exists($class)) {
  76. self::$driver = new $class($config);
  77. } else {
  78. throw new ClassNotFoundException('class not exists:' . $class, $class);
  79. }
  80. // 记录初始化信息
  81. App::$debug && Log::record('[ LOG ] INIT ' . $type, 'info');
  82. }
  83. /**
  84. * 获取日志信息
  85. * @access public
  86. * @param string $type 信息类型
  87. * @return array|string
  88. */
  89. public static function getLog($type = '')
  90. {
  91. return $type ? self::$log[$type] : self::$log;
  92. }
  93. /**
  94. * 记录调试信息
  95. * @access public
  96. * @param mixed $msg 调试信息
  97. * @param string $type 信息类型
  98. * @return void
  99. */
  100. public static function record($msg, $type = 'log')
  101. {
  102. self::$log[$type][] = $msg;
  103. // 命令行下面日志写入改进
  104. IS_CLI && self::save();
  105. }
  106. /**
  107. * 清空日志信息
  108. * @access public
  109. * @return void
  110. */
  111. public static function clear()
  112. {
  113. self::$log = [];
  114. }
  115. /**
  116. * 设置当前日志记录的授权 key
  117. * @access public
  118. * @param string $key 授权 key
  119. * @return void
  120. */
  121. public static function key($key)
  122. {
  123. self::$key = $key;
  124. }
  125. /**
  126. * 检查日志写入权限
  127. * @access public
  128. * @param array $config 当前日志配置参数
  129. * @return bool
  130. */
  131. public static function check($config)
  132. {
  133. return !self::$key || empty($config['allow_key']) || in_array(self::$key, $config['allow_key']);
  134. }
  135. /**
  136. * 保存调试信息
  137. * @access public
  138. * @return bool
  139. */
  140. public static function save()
  141. {
  142. // 没有需要保存的记录则直接返回
  143. if (empty(self::$log)) {
  144. return true;
  145. }
  146. is_null(self::$driver) && self::init(Config::get('log'));
  147. // 检测日志写入权限
  148. if (!self::check(self::$config)) {
  149. return false;
  150. }
  151. if (empty(self::$config['level'])) {
  152. // 获取全部日志
  153. $log = self::$log;
  154. if (!App::$debug && isset($log['debug'])) {
  155. unset($log['debug']);
  156. }
  157. } else {
  158. // 记录允许级别
  159. $log = [];
  160. foreach (self::$config['level'] as $level) {
  161. if (isset(self::$log[$level])) {
  162. $log[$level] = self::$log[$level];
  163. }
  164. }
  165. }
  166. if ($result = self::$driver->save($log, true)) {
  167. self::$log = [];
  168. }
  169. Hook::listen('log_write_done', $log);
  170. return $result;
  171. }
  172. /**
  173. * 实时写入日志信息 并支持行为
  174. * @access public
  175. * @param mixed $msg 调试信息
  176. * @param string $type 信息类型
  177. * @param bool $force 是否强制写入
  178. * @return bool
  179. */
  180. public static function write($msg, $type = 'log', $force = false)
  181. {
  182. $log = self::$log;
  183. // 如果不是强制写入,而且信息类型不在可记录的类别中则直接返回 false 不做记录
  184. if (true !== $force && !empty(self::$config['level']) && !in_array($type, self::$config['level'])) {
  185. return false;
  186. }
  187. // 封装日志信息
  188. $log[$type][] = $msg;
  189. // 监听 log_write
  190. Hook::listen('log_write', $log);
  191. is_null(self::$driver) && self::init(Config::get('log'));
  192. // 写入日志
  193. if ($result = self::$driver->save($log, false)) {
  194. self::$log = [];
  195. }
  196. return $result;
  197. }
  198. /**
  199. * 静态方法调用
  200. * @access public
  201. * @param string $method 调用方法
  202. * @param mixed $args 参数
  203. * @return void
  204. */
  205. public static function __callStatic($method, $args)
  206. {
  207. if (in_array($method, self::$type)) {
  208. array_push($args, $method);
  209. call_user_func_array('\\think\\Log::record', $args);
  210. }
  211. }
  212. }