我想能够根据其中发现的模式分割日志文件。
例如,处理所有日志并查找/(\w+)\s以匹配/myresource但排除/myresource/anythingelse ,将所有内容redirect到/var/log/extractedlog/myresource/access.log 。 我可以很容易地使用一些grep来编写脚本,但试图实时执行此操作会使问题变得更加困难。 例如,我想调用该程序两次,而不产生重复。
编辑
这里有一个完整的代码来获得像这样的工作与syslog-ng /etc/syslog-ng/syslog-ng.conf (学分得到接受的答案):
# no-parse let syslog load any source source s_unparsed_source { file("/var/log/myservice/access.log" flags(no-parse)); }; # Just protect the input and avoid syslog-ng header to be added in the final log template t_preserve_message { template("$MSG\n"); template_escape(no); }; # This will filter the message only matching the given expression filter f_match_pattern1 { match("\/pattern1"); }; destination d_target1 { file("/var/log/target/pattern1/access.log" template(t_preserve_message)); }; # The actual logging instruction which wraps everything log { source(s_unparsed_source); filter(f_math_pattern1); destination(d_target1); };
rsyslog和syslog-ng (在GNU / Linux中用来pipe理日志的两个常用程序)都有这样的手段。
使用syslog-ng ,您可以定义与正则expression式匹配的filter:
filter myfilter { not match("regex" value("\/usr\/sbin\/run-crons")) and not match("regex" value("vmware-checker")); }
你也可以使用模式数据库 ,它允许事件和动作触发相关。
还有logstash ,它具有高级过滤function 。 具体来说,它有一个grepfilter:
filter { grep { match => { "message" => "hello world" } } }
看看Logsurfer 。
我使用syslog-ng来“正常”login到文件中,另外我把所有东西都放到了logsurfer中,以便按程序进行分类,并发现新的/exception的消息。
从我的configuration一个片段,所以我看到所有openvpn警告:
'^.{29} .+ openvpn\[[0-9]+\]: (ERROR|WARN)' - - - 0 echo >>lines.openvpn $0 '^.{29} .+ openvpn\[[0-9]+\]: ' - - - 0 ignore