bug修复
This commit is contained in:
1 parent
914208aa70
commit
88f7fc7141
8 files changed
+949
-456
No files matched your search
+102
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
/**
|
||||
* PHP日志管理类
|
||||
* 统一处理所有PHP文件的日志记录
|
||||
*/
|
||||
|
||||
class Logger {
|
||||
private static $logFile = __DIR__ . '/logs/php.log';
|
||||
private static $initialized = false;
|
||||
|
||||
/**
|
||||
* 初始化日志系统
|
||||
*/
|
||||
public static function init() {
|
||||
if (self::$initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 确保日志目录存在
|
||||
$logDir = dirname(self::$logFile);
|
||||
if (!is_dir($logDir)) {
|
||||
mkdir($logDir, 0755, true);
|
||||
}
|
||||
|
||||
// 创建日志文件(如果不存在)
|
||||
if (!file_exists(self::$logFile)) {
|
||||
touch(self::$logFile);
|
||||
chmod(self::$logFile, 0666);
|
||||
}
|
||||
|
||||
// 设置PHP错误日志路径
|
||||
ini_set('error_log', self::$logFile);
|
||||
ini_set('log_errors', 1);
|
||||
ini_set('display_errors', 0); // 生产环境不显示错误
|
||||
|
||||
self::$initialized = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录日志
|
||||
* @param string $message 日志消息
|
||||
* @param string $level 日志级别 (INFO, WARNING, ERROR, DEBUG)
|
||||
* @param string $source 来源文件/函数
|
||||
*/
|
||||
public static function log($message, $level = 'INFO', $source = '') {
|
||||
self::init();
|
||||
|
||||
$timestamp = date('Y-m-d H:i:s');
|
||||
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
|
||||
$caller = $source ?: (isset($backtrace[1]) ? basename($backtrace[1]['file']) . ':' . $backtrace[1]['line'] : 'unknown');
|
||||
|
||||
$logEntry = "[{$timestamp}] [{$level}] [{$caller}] {$message}" . PHP_EOL;
|
||||
|
||||
file_put_contents(self::$logFile, $logEntry, FILE_APPEND | LOCK_EX);
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录信息日志
|
||||
*/
|
||||
public static function info($message, $source = '') {
|
||||
self::log($message, 'INFO', $source);
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录警告日志
|
||||
*/
|
||||
public static function warning($message, $source = '') {
|
||||
self::log($message, 'WARNING', $source);
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录错误日志
|
||||
*/
|
||||
public static function error($message, $source = '') {
|
||||
self::log($message, 'ERROR', $source);
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录调试日志
|
||||
*/
|
||||
public static function debug($message, $source = '') {
|
||||
self::log($message, 'DEBUG', $source);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取日志文件路径
|
||||
*/
|
||||
public static function getLogFile() {
|
||||
return self::$logFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空日志文件
|
||||
*/
|
||||
public static function clear() {
|
||||
file_put_contents(self::$logFile, '');
|
||||
}
|
||||
}
|
||||
|
||||
// 自动初始化
|
||||
Logger::init();
|
||||
?>
|
||||
Reference in new issue
Block a user