使用CSV格式写日志
前提条件
- log_destination的值设置为csvlog。
- logging_collector的值设置为on。
csvlog定义
以“逗号分隔值” 即CSV(Comma Separated Value)的形式发出日志。
以下是简单的用来存储CSV形式日志输出的表定义:
1. CREATE TABLE gaussdb_log
2. (
3. log_time timestamp(3) with time zone,
4. node_name text,
5. user_name text,
6. database_name text,
7. process_id bigint,
8. connection_from text,
9. "session_id" text,
10. session_line_num bigint,
11. command_tag text,
12. session_start_time timestamp with time zone,
13. virtual_transaction_id text,
14. transaction_id bigint,
15. query_id bigint,
16. module text,
17. error_severity text,
18. sql_state_code text,
19. message text,
20. detail text,
21. hint text,
22. internal_query text,
23. internal_query_pos integer,
24. context text,
25. query text,
26. query_pos integer,
27. location text,
28. application_name text
29. );
详细说明请参见表1。
表 1 csvlog字段含义表
| 字段名 | 字段含义 | 字段名 | 字段含义 |
|---|---|---|---|
| log_time | 毫秒级的时间戳 | module | 日志所属模块 |
| node_name | 节点名称 | error_severity | ERRORSTATE代码 |
| user_name | 用户名 | sql_state_code | SQLSTATE代码 |
| database_name | 数据库名 | message | 错误消息 |
| process_id | 进程ID | detail | 详细错误消息 |
| connection_from | 客户主机:端口号 | hint | 提示 |
| session_id | 会话ID | internal_query | 内部查询(查询那些导致错误的信息,如果有的话) |
| session_line_num | 每个会话的行数 | internal_query_pos | 内部查询指针 |
| command_tag | 命令标签 | context | 环境 |
| session_start_time | 会话开始时间 | query | 错误发生位置的字符统计 |
| virtual_transaction_id | 常规事务 | query_pos | 错误发生位置指针 |
| transaction_id | 事务ID | location | 在openGauss源代码中错误的位置(如果log_error_verbosity的值设为verbose) |
| query_id | 查询ID | application_name | 应用名称 |
使用COPY FROM命令将日志文件导入这个表:
1. COPY gaussdb_log FROM '/opt/data/pg_log/logfile.csv' WITH csv;
说明: 此处的日志名“logfile.csv”要换成实际生成的日志的名称。
简化输入
简化输入到CSV日志文件,可以通过如下操作:
- 设置log_filename和log_rotation_age,为日志文件提供一个一致的、可预测的命名方案。通过日志文件名,预测一个独立的日志文件完成并进入准备导入状态的时间。
- 将log_rotation_size设为0来终止基于尺寸的日志回滚,因为基于尺寸的日志回滚让预测日志文件名变得非常的困难。
- 将log_truncate_on_rotation设为on以便区分在同一日志文件中旧的日志数据和新的日志数据。
说明:
此处的日志名“logfile.csv”要换成实际生成的日志的名称。