[TOC]
* * * * *
## 1 日志文件源代碼(thinkphp/library/think/Log.php)
~~~
class Log
{
const LOG = 'log';
const ERROR = 'error';
const INFO = 'info';
const SQL = 'sql';
const NOTICE = 'notice';
const ALERT = 'alert';
protected static $log = [];
protected static $type = ['log', 'error', 'info', 'sql', 'notice', 'alert'];
protected static $driver = null;
protected static $alarm = null;
public static function init($config = [])
{
$type = isset($config['type']) ? $config['type'] : 'File';
$class = (!empty($config['namespace']) ? $config['namespace'] : '\\think\\log\\driver\\') . ucwords($type);
unset($config['type']);
self::$driver = new $class($config);
APP_DEBUG && Log::record('[ LOG ] INIT ' . $type . ': ' . var_export($config, true), 'info');
}
public static function alarm($config = [])
{
$type = isset($config['type']) ? $config['type'] : 'Email';
$class = (!empty($config['namespace']) ? $config['namespace'] : '\\think\\log\\alarm\\') . ucwords($type);
unset($config['type']);
self::$alarm = new $class($config['alarm']);
APP_DEBUG && Log::record('[ CACHE ] ALARM ' . $type . ': ' . var_export($config, true), 'info');
}
public static function getLog()
{
return self::$log;
}
public static function record($msg, $type = 'log')
{
if (!is_string($msg)) {
$msg = var_export($msg, true);
}
self::$log[] = ['type' => $type, 'msg' => $msg];
}
public static function clear()
{
self::$log = [];
}
public static function save()
{
if (is_null(self::$driver)) {
self::init(Config::get('log'));
}
return self::$driver->save(self::$log);
}
public static function write($msg, $type = 'log')
{
if (!is_string($msg)) {
$msg = var_export($msg, true);
}
$log[] = ['type' => $type, 'msg' => $msg];
APP_HOOK && Hook::listen('log_write', $log);
if (is_null(self::$driver)) {
self::init(Config::get('log'));
}
return self::$driver->save($log);
}
public static function send($msg)
{
self::$alarm && self::$alarm->send($msg);
}
public static function __callStatic($method, $args)
{
if (in_array($method, self::$type)) {
array_push($args, $method);
return call_user_func_array('\\think\\Log::record', $args);
}
}
}
~~~
## 2 分析
### 成員變量
日志類型常量
const LOG = 'log';
const ERROR = 'error';
const INFO = 'info';
const SQL = 'sql';
const NOTICE = 'notice';
const ALERT = 'alert';
日志內容,日志類型,日志緩存驅動,日志警告驅動
protected static $log = [];
protected static $type = ['log', 'error', 'info', 'sql', 'notice', 'alert'];
protected static $driver = null;
protected static $alarm = null;
### 成員方法
>[info] init() 日志驅動初始化
`public static function init($config = [])`
> $config: 日志配置參數
* * * * *
>[info] alarm() 警告驅動初始化
`public static function alarm($config = [])`
> $config:日志配置參數
* * * * *
>[info] getLog() 獲取日志內容
`public static function getLog()`
* * * * *
>[info] recode() 記錄日志信息到$log
`public static function record($msg, $type = 'log')`
> $msg:日志內容
> $type:日志類型
* * * * *
>[info] log() 清空日志內容
`public static function clear()`
* * * * *
>[info] save() 保存日志內容
`public static function save()`
* * * * *
>[info] write() 實時保存日志內容
`public static function write($msg, $type = 'log')`
> $msg:日志內容
> $type:日志類型
可回調log_write標簽行為
* * * * *
>[info] send() 發送警告
`public static function send($msg)`
> $msg:調試信息
* * * * *
>[info] __callStatic() 靜態調用
`public static function __callStatic($method, $args)`
> $method:靜態方法名稱
> ['log', 'error', 'info', 'sql', 'notice', 'alert']其中一個
> $args:日志參數
例如:Log::Log(xx)
## 3 總結
Log.php 記錄調試信息到內存,
可以使用save() write()保存到文件
還可以使用send()發送預警信息
- 更新記錄
- 概述
- 文件索引
- 函數索引
- 章節格式
- 框架流程
- 前:章節說明
- 主:(index.php)入口
- 主:(start.php)框架引導
- 主:(App.php)應用啟動
- 主:(App.php)應用調度
- C:(Controller.php)應用控制器
- M:(Model.php)數據模型
- V:(View.php)視圖對象
- 附:(App.php)應用啟動
- 附:(base.php)全局變量
- 附:(common.php)模式配置
- 附:(convention.php)全局配置
- 附:(Loader.php)自動加載器
- 附:(Build.php)自動生成
- 附:(Hook.php)監聽回調
- 附:(Route.php)全局路由
- 附:(Response.php)數據輸出
- 附:(Log.php)日志記錄
- 附:(Exception.php)異常處理
- 框架工具
- 另:(helper.php)輔助函數
- 另:(Cache.php)數據緩存
- 另:(Cookie.php)cookie操作
- 另:(Console.php)控制臺
- 另:(Debug.php)開發調試
- 另:(Error.php)錯誤處理
- 另:(Url.php)Url操作文件
- 另:(Loader.php)加載器實例化
- 另:(Input.php)數據輸入
- 另:(Lang.php)語言包管理
- 另:(ORM.php)ORM基類
- 另:(Process.php)進程管理
- 另:(Session.php)session操作
- 另:(Template.php)模板解析
- 框架驅動
- D:(\config)配置解析
- D:(\controller)控制器擴展
- D:(\model)模型擴展
- D:(\db)數據庫驅動
- D:(\view)模板解析
- D:(\template)模板標簽庫
- D:(\session)session驅動
- D:(\cache)緩存驅動
- D:(\console)控制臺
- D:(\process)進程擴展
- T:(\traits)Trait目錄
- D:(\exception)異常實現
- D:(\log)日志驅動
- 使用范例
- 服務器與框架的安裝
- 控制器操作
- 數據模型操作
- 視圖渲染控制
- MVC開發初探
- 模塊開發
- 入口文件定義全局變量
- 運行模式開發
- 框架配置
- 自動生成應用
- 事件與插件注冊
- 路由規則注冊
- 輸出控制
- 多種應用組織
- 綜合應用
- tp框架整合后臺auto架構快速開發
- 基礎原理
- php默認全局變量
- php的魔術方法
- php命名空間
- php的自動加載
- php的composer
- php的反射
- php的trait機制
- php設計模式
- php的系統時區
- php的異常錯誤
- php的輸出控制
- php的正則表達式
- php的閉包函數
- php的會話控制
- php的接口
- php的PDO
- php的字符串操作
- php的curl
- 框架心得
- 心:整體結構
- 心:配置詳解
- 心:加載器詳解
- 心:輸入輸出詳解
- 心:url路由詳解
- 心:模板詳解
- 心:模型詳解
- 心:日志詳解
- 心:緩存詳解
- 心:控制臺詳解
- 框架更新
- 4.20(驗證類,助手函數)
- 4.27(新模型Model功能)
- 5.4(新數據庫驅動)
- 7.28(自動加載)