Files
cunkebao_v3/Server/thinkphp/library/think/Log.php

238 lines
6.1 KiB
PHP
Raw Normal View History

2025-04-02 16:38:48 +08:00
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think;
2025-07-07 11:31:25 +08:00
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
2025-04-02 16:38:48 +08:00
{
2025-07-07 11:31:25 +08:00
const LOG = 'log';
const ERROR = 'error';
const INFO = 'info';
const SQL = 'sql';
const NOTICE = 'notice';
const ALERT = 'alert';
const DEBUG = 'debug';
2025-04-02 16:38:48 +08:00
/**
2025-07-07 11:31:25 +08:00
* @var array 日志信息
2025-04-02 16:38:48 +08:00
*/
2025-07-07 11:31:25 +08:00
protected static $log = [];
2025-04-02 16:38:48 +08:00
/**
2025-07-07 11:31:25 +08:00
* @var array 配置参数
2025-04-02 16:38:48 +08:00
*/
2025-07-07 11:31:25 +08:00
protected static $config = [];
2025-04-02 16:38:48 +08:00
/**
2025-07-07 11:31:25 +08:00
* @var array 日志类型
2025-04-02 16:38:48 +08:00
*/
2025-07-07 11:31:25 +08:00
protected static $type = ['log', 'error', 'info', 'sql', 'notice', 'alert', 'debug'];
2025-04-02 16:38:48 +08:00
/**
2025-07-07 11:31:25 +08:00
* @var log\driver\File|log\driver\Test|log\driver\Socket 日志写入驱动
2025-04-02 16:38:48 +08:00
*/
2025-07-07 11:31:25 +08:00
protected static $driver;
2025-04-02 16:38:48 +08:00
/**
2025-07-07 11:31:25 +08:00
* @var string 当前日志授权 key
2025-04-02 16:38:48 +08:00
*/
2025-07-07 11:31:25 +08:00
protected static $key;
2025-04-02 16:38:48 +08:00
/**
* 日志初始化
* @access public
2025-07-07 11:31:25 +08:00
* @param array $config 配置参数
* @return void
2025-04-02 16:38:48 +08:00
*/
2025-07-07 11:31:25 +08:00
public static function init($config = [])
2025-04-02 16:38:48 +08:00
{
2025-07-07 11:31:25 +08:00
$type = isset($config['type']) ? $config['type'] : 'File';
$class = false !== strpos($type, '\\') ? $type : '\\think\\log\\driver\\' . ucwords($type);
2025-04-02 16:38:48 +08:00
2025-07-07 11:31:25 +08:00
self::$config = $config;
2025-04-02 16:38:48 +08:00
unset($config['type']);
2025-07-07 11:31:25 +08:00
if (class_exists($class)) {
self::$driver = new $class($config);
} else {
throw new ClassNotFoundException('class not exists:' . $class, $class);
2025-04-02 16:38:48 +08:00
}
2025-07-07 11:31:25 +08:00
// 记录初始化信息
App::$debug && Log::record('[ LOG ] INIT ' . $type, 'info');
2025-04-02 16:38:48 +08:00
}
/**
* 获取日志信息
* @access public
* @param string $type 信息类型
2025-07-07 11:31:25 +08:00
* @return array|string
2025-04-02 16:38:48 +08:00
*/
2025-07-07 11:31:25 +08:00
public static function getLog($type = '')
2025-04-02 16:38:48 +08:00
{
2025-07-07 11:31:25 +08:00
return $type ? self::$log[$type] : self::$log;
2025-04-02 16:38:48 +08:00
}
/**
2025-07-07 11:31:25 +08:00
* 记录调试信息
2025-04-02 16:38:48 +08:00
* @access public
2025-07-07 11:31:25 +08:00
* @param mixed $msg 调试信息
* @param string $type 信息类型
* @return void
2025-04-02 16:38:48 +08:00
*/
2025-07-07 11:31:25 +08:00
public static function record($msg, $type = 'log')
2025-04-02 16:38:48 +08:00
{
2025-07-07 11:31:25 +08:00
self::$log[$type][] = $msg;
2025-04-02 16:38:48 +08:00
2025-07-07 11:31:25 +08:00
// 命令行下面日志写入改进
IS_CLI && self::save();
2025-04-02 16:38:48 +08:00
}
/**
* 清空日志信息
* @access public
2025-07-07 11:31:25 +08:00
* @return void
2025-04-02 16:38:48 +08:00
*/
2025-07-07 11:31:25 +08:00
public static function clear()
2025-04-02 16:38:48 +08:00
{
2025-07-07 11:31:25 +08:00
self::$log = [];
2025-04-02 16:38:48 +08:00
}
/**
2025-07-07 11:31:25 +08:00
* 设置当前日志记录的授权 key
2025-04-02 16:38:48 +08:00
* @access public
2025-07-07 11:31:25 +08:00
* @param string $key 授权 key
* @return void
2025-04-02 16:38:48 +08:00
*/
2025-07-07 11:31:25 +08:00
public static function key($key)
2025-04-02 16:38:48 +08:00
{
2025-07-07 11:31:25 +08:00
self::$key = $key;
2025-04-02 16:38:48 +08:00
}
/**
* 检查日志写入权限
* @access public
2025-07-07 11:31:25 +08:00
* @param array $config 当前日志配置参数
2025-04-02 16:38:48 +08:00
* @return bool
*/
2025-07-07 11:31:25 +08:00
public static function check($config)
2025-04-02 16:38:48 +08:00
{
2025-07-07 11:31:25 +08:00
return !self::$key || empty($config['allow_key']) || in_array(self::$key, $config['allow_key']);
2025-04-02 16:38:48 +08:00
}
/**
* 保存调试信息
* @access public
* @return bool
*/
2025-07-07 11:31:25 +08:00
public static function save()
2025-04-02 16:38:48 +08:00
{
2025-07-07 11:31:25 +08:00
// 没有需要保存的记录则直接返回
if (empty(self::$log)) {
2025-04-02 16:38:48 +08:00
return true;
}
2025-07-07 11:31:25 +08:00
is_null(self::$driver) && self::init(Config::get('log'));
// 检测日志写入权限
if (!self::check(self::$config)) {
2025-04-02 16:38:48 +08:00
return false;
}
2025-07-07 11:31:25 +08:00
if (empty(self::$config['level'])) {
// 获取全部日志
$log = self::$log;
if (!App::$debug && isset($log['debug'])) {
unset($log['debug']);
2025-04-02 16:38:48 +08:00
}
2025-07-07 11:31:25 +08:00
} else {
// 记录允许级别
$log = [];
foreach (self::$config['level'] as $level) {
if (isset(self::$log[$level])) {
$log[$level] = self::$log[$level];
}
2025-04-02 16:38:48 +08:00
}
}
2025-07-07 11:31:25 +08:00
if ($result = self::$driver->save($log, true)) {
self::$log = [];
2025-04-02 16:38:48 +08:00
}
2025-07-07 11:31:25 +08:00
Hook::listen('log_write_done', $log);
2025-04-02 16:38:48 +08:00
return $result;
}
/**
* 实时写入日志信息 并支持行为
* @access public
* @param mixed $msg 调试信息
2025-07-07 11:31:25 +08:00
* @param string $type 信息类型
2025-04-02 16:38:48 +08:00
* @param bool $force 是否强制写入
* @return bool
*/
2025-07-07 11:31:25 +08:00
public static function write($msg, $type = 'log', $force = false)
2025-04-02 16:38:48 +08:00
{
2025-07-07 11:31:25 +08:00
$log = self::$log;
2025-04-02 16:38:48 +08:00
2025-07-07 11:31:25 +08:00
// 如果不是强制写入,而且信息类型不在可记录的类别中则直接返回 false 不做记录
if (true !== $force && !empty(self::$config['level']) && !in_array($type, self::$config['level'])) {
2025-04-02 16:38:48 +08:00
return false;
}
2025-07-07 11:31:25 +08:00
// 封装日志信息
$log[$type][] = $msg;
2025-04-02 16:38:48 +08:00
2025-07-07 11:31:25 +08:00
// 监听 log_write
Hook::listen('log_write', $log);
2025-04-02 16:38:48 +08:00
2025-07-07 11:31:25 +08:00
is_null(self::$driver) && self::init(Config::get('log'));
2025-04-02 16:38:48 +08:00
2025-07-07 11:31:25 +08:00
// 写入日志
if ($result = self::$driver->save($log, false)) {
self::$log = [];
}
2025-04-02 16:38:48 +08:00
2025-07-07 11:31:25 +08:00
return $result;
2025-04-02 16:38:48 +08:00
}
/**
2025-07-07 11:31:25 +08:00
* 静态方法调用
2025-04-02 16:38:48 +08:00
* @access public
2025-07-07 11:31:25 +08:00
* @param string $method 调用方法
* @param mixed $args 参数
2025-04-02 16:38:48 +08:00
* @return void
*/
2025-07-07 11:31:25 +08:00
public static function __callStatic($method, $args)
2025-04-02 16:38:48 +08:00
{
2025-07-07 11:31:25 +08:00
if (in_array($method, self::$type)) {
array_push($args, $method);
2025-04-02 16:38:48 +08:00
2025-07-07 11:31:25 +08:00
call_user_func_array('\\think\\Log::record', $args);
}
2025-04-02 16:38:48 +08:00
}
}