123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- <?php
- namespace think;
- use think\exception\ClassNotFoundException;
- class Log
- {
- const LOG = 'log';
- const ERROR = 'error';
- const INFO = 'info';
- const SQL = 'sql';
- const NOTICE = 'notice';
- const ALERT = 'alert';
- const DEBUG = 'debug';
-
- protected static $log = [];
-
- protected static $config = [];
-
- protected static $type = ['log', 'error', 'info', 'sql', 'notice', 'alert', 'debug'];
-
- protected static $driver;
-
- protected static $key;
-
- public static function init($config = [])
- {
- $type = isset($config['type']) ? $config['type'] : 'File';
- $class = false !== strpos($type, '\\') ? $type : '\\think\\log\\driver\\' . ucwords($type);
- self::$config = $config;
- unset($config['type']);
- if (class_exists($class)) {
- self::$driver = new $class($config);
- } else {
- throw new ClassNotFoundException('class not exists:' . $class, $class);
- }
-
- App::$debug && Log::record('[ LOG ] INIT ' . $type, 'info');
- }
-
- public static function getLog($type = '')
- {
- return $type ? self::$log[$type] : self::$log;
- }
-
- public static function record($msg, $type = 'log')
- {
- self::$log[$type][] = $msg;
-
- IS_CLI && self::save();
- }
-
- public static function clear()
- {
- self::$log = [];
- }
-
- public static function key($key)
- {
- self::$key = $key;
- }
-
- public static function check($config)
- {
- return !self::$key || empty($config['allow_key']) || in_array(self::$key, $config['allow_key']);
- }
-
- public static function save()
- {
-
- if (empty(self::$log)) {
- return true;
- }
- is_null(self::$driver) && self::init(Config::get('log'));
-
- if (!self::check(self::$config)) {
- return false;
- }
- if (empty(self::$config['level'])) {
-
- $log = self::$log;
- if (!App::$debug && isset($log['debug'])) {
- unset($log['debug']);
- }
- } else {
-
- $log = [];
- foreach (self::$config['level'] as $level) {
- if (isset(self::$log[$level])) {
- $log[$level] = self::$log[$level];
- }
- }
- }
- if ($result = self::$driver->save($log, true)) {
- self::$log = [];
- }
- Hook::listen('log_write_done', $log);
- return $result;
- }
-
- public static function write($msg, $type = 'log', $force = false)
- {
- $log = self::$log;
-
- if (true !== $force && !empty(self::$config['level']) && !in_array($type, self::$config['level'])) {
- return false;
- }
-
- $log[$type][] = $msg;
-
- Hook::listen('log_write', $log);
- is_null(self::$driver) && self::init(Config::get('log'));
-
- if ($result = self::$driver->save($log, false)) {
- self::$log = [];
- }
- return $result;
- }
-
- public static function __callStatic($method, $args)
- {
- if (in_array($method, self::$type)) {
- array_push($args, $method);
- call_user_func_array('\\think\\Log::record', $args);
- }
- }
- }
|