Log.php 5.6 KB

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