Log.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. */
  24. class Log
  25. {
  26. const LOG = 'log';
  27. const ERROR = 'error';
  28. const INFO = 'info';
  29. const SQL = 'sql';
  30. const NOTICE = 'notice';
  31. const ALERT = 'alert';
  32. const DEBUG = 'debug';
  33. /**
  34. * @var array 日志信息
  35. */
  36. protected static $log = [];
  37. /**
  38. * @var array 配置参数
  39. */
  40. protected static $config = [];
  41. /**
  42. * @var array 日志类型
  43. */
  44. protected static $type = ['log', 'error', 'info', 'sql', 'notice', 'alert', 'debug'];
  45. /**
  46. * @var log\driver\File|log\driver\Test|log\driver\Socket 日志写入驱动
  47. */
  48. protected static $driver;
  49. /**
  50. * @var string 当前日志授权 key
  51. */
  52. protected static $key;
  53. /**
  54. * 日志初始化
  55. * @access public
  56. * @param array $config 配置参数
  57. * @return void
  58. */
  59. public static function init($config = [])
  60. {
  61. $type = isset($config['type']) ? $config['type'] : 'File';
  62. $class = false !== strpos($type, '\\') ? $type : '\\think\\log\\driver\\' . ucwords($type);
  63. self::$config = $config;
  64. unset($config['type']);
  65. if (class_exists($class)) {
  66. self::$driver = new $class($config);
  67. } else {
  68. throw new ClassNotFoundException('class not exists:' . $class, $class);
  69. }
  70. // 记录初始化信息
  71. App::$debug && Log::record('[ LOG ] INIT ' . $type, 'info');
  72. }
  73. /**
  74. * 获取日志信息
  75. * @access public
  76. * @param string $type 信息类型
  77. * @return array|string
  78. */
  79. public static function getLog($type = '')
  80. {
  81. return $type ? self::$log[$type] : self::$log;
  82. }
  83. /**
  84. * 记录调试信息
  85. * @access public
  86. * @param mixed $msg 调试信息
  87. * @param string $type 信息类型
  88. * @return void
  89. */
  90. public static function record($msg, $type = 'log')
  91. {
  92. self::$log[$type][] = $msg;
  93. // 命令行下面日志写入改进
  94. IS_CLI && self::save();
  95. }
  96. /**
  97. * 清空日志信息
  98. * @access public
  99. * @return void
  100. */
  101. public static function clear()
  102. {
  103. self::$log = [];
  104. }
  105. /**
  106. * 设置当前日志记录的授权 key
  107. * @access public
  108. * @param string $key 授权 key
  109. * @return void
  110. */
  111. public static function key($key)
  112. {
  113. self::$key = $key;
  114. }
  115. /**
  116. * 检查日志写入权限
  117. * @access public
  118. * @param array $config 当前日志配置参数
  119. * @return bool
  120. */
  121. public static function check($config)
  122. {
  123. return !self::$key || empty($config['allow_key']) || in_array(self::$key, $config['allow_key']);
  124. }
  125. /**
  126. * 保存调试信息
  127. * @access public
  128. * @return bool
  129. */
  130. public static function save()
  131. {
  132. // 没有需要保存的记录则直接返回
  133. if (empty(self::$log)) {
  134. return true;
  135. }
  136. is_null(self::$driver) && self::init(Config::get('log'));
  137. // 检测日志写入权限
  138. if (!self::check(self::$config)) {
  139. return false;
  140. }
  141. if (empty(self::$config['level'])) {
  142. // 获取全部日志
  143. $log = self::$log;
  144. if (!App::$debug && isset($log['debug'])) {
  145. unset($log['debug']);
  146. }
  147. } else {
  148. // 记录允许级别
  149. $log = [];
  150. foreach (self::$config['level'] as $level) {
  151. if (isset(self::$log[$level])) {
  152. $log[$level] = self::$log[$level];
  153. }
  154. }
  155. }
  156. if ($result = self::$driver->save($log, true)) {
  157. self::$log = [];
  158. }
  159. Hook::listen('log_write_done', $log);
  160. return $result;
  161. }
  162. /**
  163. * 实时写入日志信息 并支持行为
  164. * @access public
  165. * @param mixed $msg 调试信息
  166. * @param string $type 信息类型
  167. * @param bool $force 是否强制写入
  168. * @return bool
  169. */
  170. public static function write($msg, $type = 'log', $force = false)
  171. {
  172. $log = self::$log;
  173. // 如果不是强制写入,而且信息类型不在可记录的类别中则直接返回 false 不做记录
  174. if (true !== $force && !empty(self::$config['level']) && !in_array($type, self::$config['level'])) {
  175. return false;
  176. }
  177. // 封装日志信息
  178. $log[$type][] = $msg;
  179. // 监听 log_write
  180. Hook::listen('log_write', $log);
  181. is_null(self::$driver) && self::init(Config::get('log'));
  182. // 写入日志
  183. if ($result = self::$driver->save($log, false)) {
  184. self::$log = [];
  185. }
  186. return $result;
  187. }
  188. /**
  189. * 静态方法调用
  190. * @access public
  191. * @param string $method 调用方法
  192. * @param mixed $args 参数
  193. * @return void
  194. */
  195. public static function __callStatic($method, $args)
  196. {
  197. if (in_array($method, self::$type)) {
  198. array_push($args, $method);
  199. call_user_func_array('\\think\\Log::record', $args);
  200. }
  201. }
  202. }