teemitop / tpglog
A ThinkPHP package for logging exceptions to database
1.0.1
2026-01-15 05:19 UTC
Requires
- php: >=7.1.0
- topthink/framework: ^6.0|^8.0
- topthink/think-migration: ^3.0
- topthink/think-orm: ^2.0|^3.0
Requires (Dev)
- phpunit/phpunit: ^7.0 || ^8.0 || ^9.0
This package is not auto-updated.
Last update: 2026-04-10 03:01:33 UTC
README
一个用于将ThinkPHP应用程序异常记录到数据库的Composer包。
功能特性
- 自动捕获并记录应用程序中的所有异常
- 将异常信息存储到数据库中便于分析
- 记录详细的请求和服务器上下文信息
- 提供异常日志查询和统计服务
- 支持按需清理旧的日志记录
安装
composer require teemitop/tpglog
注意:如果遇到安装问题,可以先添加 Gitee 的 Composer 镜像源:
composer config -g repo.packagist composer https://packagist.phpcomposer.com
配置
1. 运行数据库迁移
安装包后,需要运行以下命令创建异常日志表:
php think migrate:run --path=vendor/teemitop/tpglog/migrations
如果上述命令因路径问题无法执行,你也可以尝试复制迁移文件到你的项目迁移目录:
copy vendor\teemitop\tpglog\migrations\20241011000001_install_create_exception_log.php database\migrations\
然后执行:
php think migrate:run
注意: 如果您的项目中尚未安装
topthink/think-migration,请先安装:composer require topthink/think-migration
2. 配置异常处理器
在你的 app/ 下面新建provider.php,并绑定异常处理类:
<?php
// +----------------------------------------------------------------------
// | 服务提供者配置
// +----------------------------------------------------------------------
return [
// 绑定自定义异常处理handle类
'think\exception\Handle' => '\\Teemitop\\Tpglog\\ExceptionHandler',
];
3. 发布配置文件(可选)
将配置文件复制到应用目录:
cp vendor/teemitop/tpglog/config/exception_logger.php config/
使用方法
基础用法
安装并配置完成后,所有异常将自动记录到数据库的 exception_log 表中。
手动记录异常
use Teemitop\Tpglog\Services\ExceptionLogService;
try {
// 一些可能会抛出异常的代码
throw new \Exception('测试异常');
} catch (\Throwable $exception) {
// 手动记录异常
ExceptionLogService::logException($exception, [
'custom_field' => 'custom_value'
]);
}
查询异常日志
use Teemitop\Tpglog\Services\ExceptionLogService;
// 获取最近20条异常日志
$recentLogs = ExceptionLogService::getRecentLogs(20);
// 搜索特定异常
$searchResult = ExceptionLogService::searchLogs([
'class' => 'PDOException',
'date_from' => '2023-01-01',
'date_to' => '2023-12-31'
]);
// 获取异常统计
$stats = ExceptionLogService::getStatistics(7); // 最近7天的统计
清理旧日志
use Teemitop\Tpglog\Services\ExceptionLogService;
// 清理30天前的异常日志
$deletedCount = ExceptionLogService::cleanOldLogs(30);
配置选项
配置文件位于 config/exception_logger.php:
return [
// 是否启用异常日志记录
'enabled' => true,
// 日志保留天数
'retention_days' => 30,
// 是否记录特定类型的异常
'log_http_exceptions' => true,
'log_route_not_found' => false,
// 是否记录请求数据
'log_request_data' => true,
// 是否记录服务器数据
'log_server_data' => true,
// 敏感数据过滤 - 在记录时会被隐藏的字段
'sensitive_fields' => [
'password',
'pwd',
'secret',
'token',
'authorization',
'cookie',
],
// 日志级别
'level' => 'error',
// 是否同时记录到文件
'also_log_to_file' => true,
];
数据库表结构
创建的 exception_log 表包含以下字段:
id- 主键class- 异常类名code- 异常代码message- 异常消息file- 异常发生文件line- 异常发生行号trace- 堆栈跟踪request_data- 请求数据server_data- 服务器数据request_method- 请求方法request_uri- 请求URIuser_ip- 用户IPuser_agent- 用户代理created_at- 创建时间
API 服务
该包提供了 ExceptionLogService 类来处理常见的日志操作:
logException()- 记录单个异常batchLogExceptions()- 批量记录异常cleanOldLogs()- 清理旧日志getStatistics()- 获取统计信息getRecentLogs()- 获取最近日志searchLogs()- 按条件搜索日志
许可证
MIT License