![postgres 日志工作流:根据特定工作时间过滤日志事件](https://linux22.com/image/728377/postgres%20%E6%97%A5%E5%BF%97%E5%B7%A5%E4%BD%9C%E6%B5%81%EF%BC%9A%E6%A0%B9%E6%8D%AE%E7%89%B9%E5%AE%9A%E5%B7%A5%E4%BD%9C%E6%97%B6%E9%97%B4%E8%BF%87%E6%BB%A4%E6%97%A5%E5%BF%97%E4%BA%8B%E4%BB%B6.png)
要分析 postgres 日志,我们的工作流程是:
- postgres 通过 syslog 记录
- rsyslog 写入一个文本文件
- 午夜时分我们停止了 postgres
- pgbadger 读取并解析每日日志文件(大约 2 GB)
我们希望排除一些异常值,例如当应用程序启动时,因为对我们来说它们是无用的数据并且只会污染日志文件。
为了实现这一点:如何/在哪里指定工作时间(从..到)过滤器?
理想的解决方案是在 postgres 中设置过滤器。
不太理想,在 rsyslog 处过滤。
最后的选择是在 pgbadger 进行过滤:html 输出将被修剪,但我们的服务器会继续写入大日志文件。
当前 rsyslog 配置:
:msg, contains, "connection authorized: user=root database=root" ~
:msg, contains, "FATAL: database \"root\" does not exist" ~
local0.* -/var/log/postgresql.log
答案1
rsyslog.conf
您可以使用时间指定过滤器系统属性保存当前时间,如hour
和minute
。例如,
if $$hour >= '10' and $$hour <='17' then /var/log/output
据我了解,小时和分钟的值是带有前导零的 2 个字符串。
您可以将其与设施测试(local0 等)结合起来,例如:
if ( $syslogfacility-text=='local0' ) and ( $$hour <= '10' or $$hour>='20' ) then stop
(哪里stop
有更新的说法~
),或者如果你愿意
if ( $syslogfacility-text=='local0' ) and ( $$hour > '10' and $$hour<'20' ) then /var/log/output
请注意,这些过滤器必须位于从第一列开始的同一行上。