Content Table

JSON 日志显示为表格

日志系统输出的是 JSON 格式的日志,每行一个 JSON 对象,例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{"func":"newdtagent/service.(*WatchdogService).UpdateAgent():63","level":"info","msg":"升级 Agent","time":"2023-02-17 14:03:42","version":"v1.0"}
{"func":"newdtagent/service.(*WatchdogService).doUpdateAgent():60","level":"info","msg":"下载 Agent","time":"2023-02-17 14:03:42","version":"v1.0"}
{"autoServiceAddr":"10.10.10.36:8290","func":"newdtagent/service.(*WatchdogService).downloadAgent():289","level":"warning","msg":"[成功] 下载 Agent 成功","time":"2023-02-17 14:03:53","url":"http://10.10.10.36:8290/api/agents/versions/v1.0/downloadInPartition"}
{"count":1,"error":"不能升级,正在执行任务: RunningJobsCount: 1, UploadingFileCount: 0","func":"newdtagent/service.(*WatchdogService).doUpdateAgent.func3():124","level":"warning","msg":"尝试升级 Agent...","time":"2023-02-17 14:03:53"}
{"count":2,"error":"不能升级,正在执行任务: RunningJobsCount: 1, UploadingFileCount: 0","func":"newdtagent/service.(*WatchdogService).doUpdateAgent.func3():124","level":"warning","msg":"尝试升级 Agent...","time":"2023-02-17 14:03:58"}
{"count":3,"error":"不能升级,正在执行任务: RunningJobsCount: 1, UploadingFileCount: 0","func":"newdtagent/service.(*WatchdogService).doUpdateAgent.func3():124","level":"warning","msg":"尝试升级 Agent...","time":"2023-02-17 14:04:03"}
{"count":4,"error":"不能升级,正在执行任务: RunningJobsCount: 1, UploadingFileCount: 0","func":"newdtagent/service.(*WatchdogService).doUpdateAgent.func3():124","level":"warning","msg":"尝试升级 Agent...","time":"2023-02-17 14:04:08"}
{"count":5,"error":"不能升级,正在执行任务: RunningJobsCount: 1, UploadingFileCount: 0","func":"newdtagent/service.(*WatchdogService).doUpdateAgent.func3():124","level":"warning","msg":"尝试升级 Agent...","time":"2023-02-17 14:04:13"}
{"count":6,"error":"不能升级,正在执行任务: RunningJobsCount: 1, UploadingFileCount: 0","func":"newdtagent/service.(*WatchdogService).doUpdateAgent.func3():124","level":"warning","msg":"尝试升级 Agent...","time":"2023-02-17 14:04:18"}
{"count":7,"error":"不能升级,正在执行任务: RunningJobsCount: 1, UploadingFileCount: 0","func":"newdtagent/service.(*WatchdogService).doUpdateAgent.func3():124","level":"warning","msg":"尝试升级 Agent...","time":"2023-02-17 14:04:23"}
{"error":"Get \"http://127.0.0.1:12301/api/stats\": dial tcp 127.0.0.1:12301: connect: connection refused","func":"newdtagent/service.(*WatchdogService).doGetAgentStats():34","level":"warning","msg":"[错误] 获取 Agent 状态失败","time":"2023-02-17 14:04:28"}
{"func":"newdtagent/service.(*WatchdogService).doStopAgent.func1():170","level":"info","msg":"[成功] 退出 Agent 成功","time":"2023-02-17 14:04:28"}
{"error":"Get \"http://127.0.0.1:12301/api/stats\": dial tcp 127.0.0.1:12301: connect: connection refused","func":"newdtagent/service.(*WatchdogService).doGetAgentStats():34","level":"warning","msg":"[错误] 获取 Agent 状态失败","time":"2023-02-17 14:04:28"}
{"func":"newdtagent/service.(*WatchdogService).doStartAgent.func1():239","level":"info","msg":"[成功] 启动 Agent 成功","time":"2023-02-17 14:04:30"}

阅读起来不方便,可以把 JSON 格式的日志文件放到 ES、MongoDB 等中,可视化为表格方便查看、搜索等。

下面介绍使用 mongoimport (安装 MongoDB 后自带) 把 JSON 格式的日志文件内容导入到 MongoDB 中,命令格式为:

1
mongoimport --db <db_name> --collection <collection_name> --file <json_log_file_path>

例如:

1
/opt/homebrew/Cellar/mongodb-community@4.2/4.2.22/bin/mongoimport --db agent_logs --collection log --file logs.json

上面的 JSON 日志导入 MongoDB 后显示效果为: