[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()发送预警信息