標籤:thinkphp
<?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK ]// +----------------------------------------------------------------------// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.// +----------------------------------------------------------------------// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )// +----------------------------------------------------------------------// | Author: liu21st <[email protected]>// +----------------------------------------------------------------------namespace think;use think\exception\ClassNotFoundException;// 使用異常 類未捕獲 異常/** * Class Log * @package think * * @method void log($msg) static 靜態 日誌 記錄 * @method void error($msg) static 靜態 錯誤 資訊 * @method void info($msg) static 靜態 資訊 * @method void sql($msg) static 靜態 sql 語句 * @method void notice($msg) static 靜態 通知 * @method void alert($msg) static 靜態 警告 */class Log{ const LOG = ‘log‘;// 常量定義 const ERROR = ‘error‘;// 錯誤定義 const INFO = ‘info‘; // 資訊定義 const SQL = ‘sql‘; // SQL定義 const NOTICE = ‘notice‘;// 通知定義 const ALERT = ‘alert‘;// 警告定義 // 日誌資訊 protected static $log = [];// 日誌 資訊 存放 // 配置參數 protected static $config = [];// 配置參數 資訊 存放 // 日誌類型 protected static $type = [‘log‘, ‘error‘, ‘info‘, ‘sql‘, ‘notice‘, ‘alert‘];// 日誌 類型 // 日誌寫入驅動 protected static $driver;// 日誌 寫入 驅動 // 當前日誌授權key protected static $key;// 日誌 授權 /** * 日誌初始化 * @param array $config */ 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‘);// 日誌記錄, App 下面的日誌記錄 // 調試 許可權 用的是 App 的 App 是個類,記錄日誌資訊 } /** * 擷取日誌資訊 * @param string $type 資訊類型 * @return array */ public static function getLog($type = ‘‘)// 擷取日誌資訊,不同的日誌資訊類型 info error 等 { return $type ? self::$log[$type] : self::$log; } /** * 記錄調試資訊 * @param mixed $msg 調試資訊 * @param string $type 資訊類型 * @return void */ public static function record($msg, $type = ‘log‘)// 預設寫入 日誌 資料 倉庫 而已 { self::$log[$type][] = $msg;// 並沒有寫入檔案 } /** * 清空日誌資訊 * @return void */ public static function clear()// 清空 日誌 存檔 { self::$log = []; } /** * 當前日誌記錄的授權key * @param string $key 授權key * @return void */ public static function key($key)// 授權key { self::$key = $key; } /** * 檢查日誌寫入許可權 * @param array $config 當前日誌配置參數 * @return bool */ public static function check($config)// 檢查日誌 寫入 許可權 { if (self::$key && !empty($config[‘allow_key‘]) && !in_array(self::$key, $config[‘allow_key‘])) { return false;// 日誌許可權 很 鬆散啊 } return true; } /** * 儲存調試資訊 * @return bool */ public static function save() {// 寫入記錄檔 if (!empty(self::$log)) {// 不為空白 if (is_null(self::$driver)) {// 初始化驅動 self::init(Config::get(‘log‘)); } if (!self::check(self::$config)) {// 檢測許可權 // 檢測日誌寫入許可權 return false; } if (empty(self::$config[‘level‘])) { // 擷取全部日誌 $log = self::$log; } else { // 記錄允許層級 $log = []; foreach (self::$config[‘level‘] as $level) { if (isset(self::$log[$level])) { $log[$level] = self::$log[$level]; } } } $result = self::$driver->save($log);// 調用 策略 存入 if ($result) { self::$log = []; } return $result; } return true; } /** * 即時寫入日誌資訊 並支援行為 * @param mixed $msg 調試資訊 * @param string $type 資訊類型 * @param bool $force 是否強制寫入 * @return bool */ public static function write($msg, $type = ‘log‘, $force = false) { // 封裝日誌資訊 if (true === $force || empty(self::$config[‘level‘])) {// 強制 寫入 $log[$type][] = $msg; } elseif (in_array($type, self::$config[‘level‘])) {// 許可類型 內 $log[$type][] = $msg; } else { return false; } // 監聽log_write Hook::listen(‘log_write‘, $log);// 監聽 if (is_null(self::$driver)) {// 初始化 self::init(Config::get(‘log‘)); } // 寫入日誌 return self::$driver->save($log);// 儲存日誌 } /** * 靜態調用 * @param $method * @param $args * @return mixed */ public static function __callStatic($method, $args)// 靜態調用其它 特殊的資料 { if (in_array($method, self::$type)) { array_push($args, $method);// 把函數 作為參數 存入 Tlog return call_user_func_array(‘\\think\\Log::record‘, $args); } }}
本文出自 “專註php 群號:414194301” 部落格,請務必保留此出處http://jingshanls.blog.51cto.com/3357095/1878757
[李景山php]每天TP5-20170108|thinkphp5-Log.php