Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Log.php 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. class Log implements LoggerInterface
  13. {
  14. const EMERGENCY = 'emergency';
  15. const ALERT = 'alert';
  16. const CRITICAL = 'critical';
  17. const ERROR = 'error';
  18. const WARNING = 'warning';
  19. const NOTICE = 'notice';
  20. const INFO = 'info';
  21. const DEBUG = 'debug';
  22. const SQL = 'sql';
  23. /**
  24. * 日志信息
  25. * @var array
  26. */
  27. protected $log = [];
  28. /**
  29. * 配置参数
  30. * @var array
  31. */
  32. protected $config = [];
  33. /**
  34. * 日志写入驱动
  35. * @var object
  36. */
  37. protected $driver;
  38. /**
  39. * 日志授权key
  40. * @var string
  41. */
  42. protected $key;
  43. /**
  44. * 是否允许日志写入
  45. * @var bool
  46. */
  47. protected $allowWrite = true;
  48. /**
  49. * 应用对象
  50. * @var App
  51. */
  52. protected $app;
  53. public function __construct(App $app)
  54. {
  55. $this->app = $app;
  56. }
  57. public static function __make(App $app, Config $config)
  58. {
  59. return (new static($app))->init($config->pull('log'));
  60. }
  61. /**
  62. * 日志初始化
  63. * @access public
  64. * @param array $config
  65. * @return $this
  66. */
  67. public function init($config = [])
  68. {
  69. $type = isset($config['type']) ? $config['type'] : 'File';
  70. $this->config = $config;
  71. unset($config['type']);
  72. if (!empty($config['close'])) {
  73. $this->allowWrite = false;
  74. }
  75. $this->driver = Loader::factory($type, '\\think\\log\\driver\\', $config);
  76. return $this;
  77. }
  78. /**
  79. * 获取日志信息
  80. * @access public
  81. * @param string $type 信息类型
  82. * @return array
  83. */
  84. public function getLog($type = '')
  85. {
  86. return $type ? $this->log[$type] : $this->log;
  87. }
  88. /**
  89. * 记录日志信息
  90. * @access public
  91. * @param mixed $msg 日志信息
  92. * @param string $type 日志级别
  93. * @param array $context 替换内容
  94. * @return $this
  95. */
  96. public function record($msg, $type = 'info', array $context = [])
  97. {
  98. if (!$this->allowWrite) {
  99. return;
  100. }
  101. if (is_string($msg) && !empty($context)) {
  102. $replace = [];
  103. foreach ($context as $key => $val) {
  104. $replace['{' . $key . '}'] = $val;
  105. }
  106. $msg = strtr($msg, $replace);
  107. }
  108. if (PHP_SAPI == 'cli') {
  109. if (empty($this->config['level']) || in_array($type, $this->config['level'])) {
  110. // 命令行日志实时写入
  111. $this->write($msg, $type, true);
  112. }
  113. } else {
  114. $this->log[$type][] = $msg;
  115. }
  116. return $this;
  117. }
  118. /**
  119. * 清空日志信息
  120. * @access public
  121. * @return $this
  122. */
  123. public function clear()
  124. {
  125. $this->log = [];
  126. return $this;
  127. }
  128. /**
  129. * 当前日志记录的授权key
  130. * @access public
  131. * @param string $key 授权key
  132. * @return $this
  133. */
  134. public function key($key)
  135. {
  136. $this->key = $key;
  137. return $this;
  138. }
  139. /**
  140. * 检查日志写入权限
  141. * @access public
  142. * @param array $config 当前日志配置参数
  143. * @return bool
  144. */
  145. public function check($config)
  146. {
  147. if ($this->key && !empty($config['allow_key']) && !in_array($this->key, $config['allow_key'])) {
  148. return false;
  149. }
  150. return true;
  151. }
  152. /**
  153. * 关闭本次请求日志写入
  154. * @access public
  155. * @return $this
  156. */
  157. public function close()
  158. {
  159. $this->allowWrite = false;
  160. $this->log = [];
  161. return $this;
  162. }
  163. /**
  164. * 保存调试信息
  165. * @access public
  166. * @return bool
  167. */
  168. public function save()
  169. {
  170. if (empty($this->log) || !$this->allowWrite) {
  171. return true;
  172. }
  173. if (!$this->check($this->config)) {
  174. // 检测日志写入权限
  175. return false;
  176. }
  177. $log = [];
  178. foreach ($this->log as $level => $info) {
  179. if (!$this->app->isDebug() && 'debug' == $level) {
  180. continue;
  181. }
  182. if (empty($this->config['level']) || in_array($level, $this->config['level'])) {
  183. $log[$level] = $info;
  184. $this->app['hook']->listen('log_level', [$level, $info]);
  185. }
  186. }
  187. $result = $this->driver->save($log, true);
  188. if ($result) {
  189. $this->log = [];
  190. }
  191. return $result;
  192. }
  193. /**
  194. * 实时写入日志信息 并支持行为
  195. * @access public
  196. * @param mixed $msg 调试信息
  197. * @param string $type 日志级别
  198. * @param bool $force 是否强制写入
  199. * @return bool
  200. */
  201. public function write($msg, $type = 'info', $force = false)
  202. {
  203. // 封装日志信息
  204. if (empty($this->config['level'])) {
  205. $force = true;
  206. }
  207. if (true === $force || in_array($type, $this->config['level'])) {
  208. $log[$type][] = $msg;
  209. } else {
  210. return false;
  211. }
  212. // 监听log_write
  213. $this->app['hook']->listen('log_write', $log);
  214. // 写入日志
  215. return $this->driver->save($log, false);
  216. }
  217. /**
  218. * 记录日志信息
  219. * @access public
  220. * @param string $level 日志级别
  221. * @param mixed $message 日志信息
  222. * @param array $context 替换内容
  223. * @return void
  224. */
  225. public function log($level, $message, array $context = [])
  226. {
  227. $this->record($message, $level, $context);
  228. }
  229. /**
  230. * 记录emergency信息
  231. * @access public
  232. * @param mixed $message 日志信息
  233. * @param array $context 替换内容
  234. * @return void
  235. */
  236. public function emergency($message, array $context = [])
  237. {
  238. $this->log(__FUNCTION__, $message, $context);
  239. }
  240. /**
  241. * 记录警报信息
  242. * @access public
  243. * @param mixed $message 日志信息
  244. * @param array $context 替换内容
  245. * @return void
  246. */
  247. public function alert($message, array $context = [])
  248. {
  249. $this->log(__FUNCTION__, $message, $context);
  250. }
  251. /**
  252. * 记录紧急情况
  253. * @access public
  254. * @param mixed $message 日志信息
  255. * @param array $context 替换内容
  256. * @return void
  257. */
  258. public function critical($message, array $context = [])
  259. {
  260. $this->log(__FUNCTION__, $message, $context);
  261. }
  262. /**
  263. * 记录错误信息
  264. * @access public
  265. * @param mixed $message 日志信息
  266. * @param array $context 替换内容
  267. * @return void
  268. */
  269. public function error($message, array $context = [])
  270. {
  271. $this->log(__FUNCTION__, $message, $context);
  272. }
  273. /**
  274. * 记录warning信息
  275. * @access public
  276. * @param mixed $message 日志信息
  277. * @param array $context 替换内容
  278. * @return void
  279. */
  280. public function warning($message, array $context = [])
  281. {
  282. $this->log(__FUNCTION__, $message, $context);
  283. }
  284. /**
  285. * 记录notice信息
  286. * @access public
  287. * @param mixed $message 日志信息
  288. * @param array $context 替换内容
  289. * @return void
  290. */
  291. public function notice($message, array $context = [])
  292. {
  293. $this->log(__FUNCTION__, $message, $context);
  294. }
  295. /**
  296. * 记录一般信息
  297. * @access public
  298. * @param mixed $message 日志信息
  299. * @param array $context 替换内容
  300. * @return void
  301. */
  302. public function info($message, array $context = [])
  303. {
  304. $this->log(__FUNCTION__, $message, $context);
  305. }
  306. /**
  307. * 记录调试信息
  308. * @access public
  309. * @param mixed $message 日志信息
  310. * @param array $context 替换内容
  311. * @return void
  312. */
  313. public function debug($message, array $context = [])
  314. {
  315. $this->log(__FUNCTION__, $message, $context);
  316. }
  317. /**
  318. * 记录sql信息
  319. * @access public
  320. * @param mixed $message 日志信息
  321. * @param array $context 替换内容
  322. * @return void
  323. */
  324. public function sql($message, array $context = [])
  325. {
  326. $this->log(__FUNCTION__, $message, $context);
  327. }
  328. public function __debugInfo()
  329. {
  330. $data = get_object_vars($this);
  331. unset($data['app']);
  332. return $data;
  333. }
  334. }