日志Log类增加日志写入授权Key功能

This commit is contained in:
thinkphp
2016-06-06 17:32:18 +08:00
parent 37caffb6bc
commit 29c8faf118

View File

@@ -22,12 +22,16 @@ class Log
// 日志信息
protected static $log = [];
// 配置参数
protected static $config = [];
// 日志类型
protected static $type = ['log', 'error', 'info', 'sql', 'notice', 'alert'];
// 日志写入驱动
protected static $driver = null;
protected static $driver;
// 通知发送驱动
protected static $alarm = null;
protected static $alarm;
// 当前日志授权key
protected static $key;
/**
* 日志初始化
@@ -35,8 +39,9 @@ class Log
*/
public static function init($config = [])
{
$type = isset($config['type']) ? $config['type'] : 'File';
$class = (!empty($config['namespace']) ? $config['namespace'] : '\\think\\log\\driver\\') . ucwords($type);
$type = isset($config['type']) ? $config['type'] : 'File';
$class = (!empty($config['namespace']) ? $config['namespace'] : '\\think\\log\\driver\\') . ucwords($type);
self::$config = $config;
unset($config['type']);
self::$driver = new $class($config);
// 记录初始化信息
@@ -89,6 +94,30 @@ class Log
self::$log = [];
}
/**
* 当前日志记录的授权key
* @param string $key 授权key
* @return void
*/
public static function 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
@@ -100,6 +129,10 @@ class Log
self::init(Config::get('log'));
}
if (!self::check(self::$config)) {
// 检测日志写入权限
return false;
}
$result = self::$driver->save(self::$log);
if ($result) {