mirror of
https://gitee.com/ulthon/ulthon_admin.git
synced 2026-07-01 23:42:48 +08:00
195 lines
6.7 KiB
PHP
195 lines
6.7 KiB
PHP
<?php
|
|
|
|
namespace base\common\command\tools\log;
|
|
|
|
use app\common\console\Command;
|
|
use think\console\Input;
|
|
use think\console\input\Argument;
|
|
use think\console\input\Option;
|
|
use think\console\Output;
|
|
use think\facade\Db;
|
|
|
|
/**
|
|
* tools:log:show 命令基类
|
|
*/
|
|
class ToolsLogShowBase extends Command
|
|
{
|
|
protected function configure()
|
|
{
|
|
parent::configure();
|
|
|
|
$this->setName('tools:log:show')
|
|
->setDescription('查看数据库日志')
|
|
->addOption('level', 'l', Option::VALUE_OPTIONAL, '过滤日志级别 (info, warning, error)')
|
|
->addOption('limit', null, Option::VALUE_OPTIONAL, '限制显示条数', 50)
|
|
->addOption('controller', 'c', Option::VALUE_OPTIONAL, '过滤控制器名称')
|
|
->addOption('action', 'a', Option::VALUE_OPTIONAL, '过滤操作名称')
|
|
->addOption('start', 's', Option::VALUE_OPTIONAL, '开始时间 (Y-m-d H:i:s)')
|
|
->addOption('end', 'e', Option::VALUE_OPTIONAL, '结束时间 (Y-m-d H:i:s)')
|
|
->addOption('help', 'h', Option::VALUE_NONE, '显示帮助信息');
|
|
}
|
|
|
|
protected function execute($input, $output)
|
|
{
|
|
if ($input->getOption('help')) {
|
|
$this->showHelp($output);
|
|
return;
|
|
}
|
|
|
|
$filters = $this->buildFilters($input);
|
|
$limit = (int)$input->getOption('limit') ?? 50;
|
|
|
|
try {
|
|
$query = Db::name('debug_log');
|
|
|
|
// 应用过滤条件
|
|
if (!empty($filters['level'])) {
|
|
$query->where('level', '=', $filters['level']);
|
|
}
|
|
if (!empty($filters['controller'])) {
|
|
$query->whereLike('controller_name', '%' . $filters['controller'] . '%');
|
|
}
|
|
if (!empty($filters['action'])) {
|
|
$query->whereLike('action_name', '%' . $filters['action'] . '%');
|
|
}
|
|
if (!empty($filters['start'])) {
|
|
$startTime = strtotime($filters['start']);
|
|
if ($startTime !== false) {
|
|
$query->where('create_time', '>=', $startTime);
|
|
}
|
|
}
|
|
if (!empty($filters['end'])) {
|
|
$endTime = strtotime($filters['end']);
|
|
if ($endTime !== false) {
|
|
$query->where('create_time', '<=', $endTime);
|
|
}
|
|
}
|
|
|
|
// 获取总数
|
|
$count = $query->count();
|
|
|
|
// 获取日志记录
|
|
$logs = $query->order('create_time', 'desc')
|
|
->limit($limit)
|
|
->select()
|
|
->toArray();
|
|
|
|
// 格式化时间戳
|
|
foreach ($logs as &$log) {
|
|
if (!empty($log['create_time'])) {
|
|
$log['create_time_formatted'] = date('Y-m-d H:i:s', $log['create_time']);
|
|
}
|
|
}
|
|
unset($log);
|
|
|
|
$this->outputText($logs, $count, $filters, $output);
|
|
} catch (\Exception $e) {
|
|
$output->error('查询失败:' . $e->getMessage());
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 构建过滤条件
|
|
*/
|
|
protected function buildFilters(Input $input): array
|
|
{
|
|
return [
|
|
'level' => $input->getOption('level'),
|
|
'controller' => $input->getOption('controller'),
|
|
'action' => $input->getOption('action'),
|
|
'start' => $input->getOption('start'),
|
|
'end' => $input->getOption('end')
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 文本格式输出
|
|
*/
|
|
protected function outputText(array $logs, int $count, array $filters, Output $output): void
|
|
{
|
|
$output->newLine();
|
|
$output->info('=== 数据库日志查询结果 ===');
|
|
$output->newLine();
|
|
|
|
// 显示过滤条件
|
|
$activeFilters = array_filter($filters, function($value) {
|
|
return !empty($value);
|
|
});
|
|
if (!empty($activeFilters)) {
|
|
$output->comment('过滤条件:');
|
|
foreach ($activeFilters as $key => $value) {
|
|
$output->writeln(' - ' . $key . ': ' . $value);
|
|
}
|
|
$output->newLine();
|
|
}
|
|
|
|
if (empty($logs)) {
|
|
$output->comment('没有找到符合条件的日志');
|
|
return;
|
|
}
|
|
|
|
// 显示日志列表
|
|
$output->info('日志列表(共 ' . $count . ' 条,显示 ' . count($logs) . ' 条):');
|
|
$output->newLine();
|
|
|
|
foreach ($logs as $log) {
|
|
$levelMethod = $this->getLevelMethod($log['level'] ?? '');
|
|
$output->$levelMethod('[' . $log['level'] . '] ' .
|
|
'[' . ($log['create_time_formatted'] ?? 'N/A') . '] ' .
|
|
'[' . ($log['controller_name'] ?? '') . '::' . ($log['action_name'] ?? '') . ']');
|
|
|
|
// 显示内容摘要
|
|
$content = $log['content'] ?? '';
|
|
if (strlen($content) > 200) {
|
|
$content = substr($content, 0, 200) . '...';
|
|
}
|
|
$output->writeln(' ' . $content);
|
|
$output->newLine();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取日志级别对应的方法名
|
|
*/
|
|
protected function getLevelMethod(string $level): string
|
|
{
|
|
$level = strtolower($level);
|
|
$methods = [
|
|
'error' => 'error',
|
|
'warning' => 'comment',
|
|
'info' => 'info',
|
|
'debug' => 'info'
|
|
];
|
|
|
|
return $methods[$level] ?? 'info';
|
|
}
|
|
|
|
/**
|
|
* 显示帮助信息
|
|
*/
|
|
protected function showHelp(Output $output): void
|
|
{
|
|
$output->newLine();
|
|
$output->info('命令名称:');
|
|
$output->writeln(' tools:log:show - 查看数据库日志');
|
|
$output->newLine();
|
|
$output->info('用法:');
|
|
$output->writeln(' php think tools:log:show [选项]');
|
|
$output->newLine();
|
|
$output->info('选项:');
|
|
$output->writeln(' -l, --level 过滤日志级别 (info, warning, error)');
|
|
$output->writeln(' --limit 限制显示条数 (默认: 50)');
|
|
$output->writeln(' -c, --controller 过滤控制器名称');
|
|
$output->writeln(' -a, --action 过滤操作名称');
|
|
$output->writeln(' -s, --start 开始时间 (Y-m-d H:i:s)');
|
|
$output->writeln(' -e, --end 结束时间 (Y-m-d H:i:s)');
|
|
$output->writeln(' -h, --help 显示帮助信息');
|
|
$output->newLine();
|
|
$output->info('示例:');
|
|
$output->writeln(' php think tools:log:show --level=error --limit=10');
|
|
$output->writeln(' php think tools:log:show --controller=Admin --limit=20');
|
|
$output->writeln(' php think tools:log:show --start="2024-01-01 00:00:00" --end="2024-12-31 23:59:59"');
|
|
$output->newLine();
|
|
}
|
|
} |