Whi*_*ses 6 java log4j syslog rsyslog
我正在使用 rsyslog 将我的 var/log/message 提供给工具。但是,正如您所看到的,异常出现在多行(每行的时间戳不同)中,而不是记录为单行。我希望它看起来像 catalina.out 消息。有什么办法可以做到这一点。任何帮助将不胜感激。
catalina.out 看起来像这样:
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
Run Code Online (Sandbox Code Playgroud)
var/log/message 看起来像这样
2014-02-20T06:21:32.006782+00:00 something148-084-115 at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
2014-02-20T06:21:32.006782+00:00 something148-084-115 at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
2014-02-20T06:21:32.006784+00:00 something148-084-115 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
2014-02-20T06:21:32.006784+00:00 something148-084-115 at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
2014-02-20T06:21:32.006786+00:00 something148-084-115 at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
Run Code Online (Sandbox Code Playgroud)
log4j.xml -
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} abc: [component="XYZ" priority="%p" thread="%t"] %c.%M:%L - %m%n" />
</layout>
</appender>
<appender name="syslog" class="org.apache.log4j.net.SyslogAppender">
<param name="syslogHost" value="localhost" />
<param name="threshold" value="INFO" />
<param name="facility" value="LOCAL0" />
<param name="facilityPrinting" value="false" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="abc: [component="XYZ" priority="%p" thread="%t"] %c.%M:%L - %m%n" />
</layout>
</appender>
<root>
<priority value="info" />
<appender-ref ref="console" />
<appender-ref ref="syslog" />
</root>
<logger name="org.springframework">
<level value="warn" />
</logger>
<logger name="org.hibernate">
<level value="warn" />
</logger>
</log4j:configuration>
Run Code Online (Sandbox Code Playgroud)
谢谢
由于这个问题是 8 年前提出的,我不会费心回答这个具体问题,但会尽力让刚刚看到这篇文章的人更容易理解。
\n从rsyslog版本开始,他们添加了使用该模块处理文本文件中的多行消息的8.10功能。imfile该imfile模块使 rsyslog 能够将任何文本文件转换为系统日志消息流。您可以包含一个startmsg.regex参数来定义 rsyslog 将识别为新日志条目的开头的正则表达式模式。如果 rsyslog 检测到该模式,它会将所有后续日志条目聚合到同一事件中,直到找到另一个匹配行。
默认情况下,rsyslog 可以发送和接收最大8 KB的日志消息。多行消息可能比这大得多。为了确保 rsyslog 正确处理大型多行消息,您可以通过将以下内容添加到您的(非常)顶部,将最大消息大小增加到64 KBrsyslog.conf
$MaxMessageSize 64k\nRun Code Online (Sandbox Code Playgroud)\n要添加imfile模块,请附加以下内容:
module(load="imfile" mode="inotify")\nRun Code Online (Sandbox Code Playgroud)\n\n\n传统上,
\nimfile使用轮询模式,该模式比inotify模式消耗更多资源(并且速度更慢)。建议用户开启\xe2\x80\x9c轮询只有在 inotify 模式下遇到奇怪问题时才
之后,定义要从中导入日志消息的文件以及正则表达式。
\ninput(type="imfile" File="/var/log/local0.log" \n startmsg.regex="^[0-9]{4}-[0-9]{2}-[0-9]{2}")\nRun Code Online (Sandbox Code Playgroud)\n之后您可以根据需要重定向输入,例如
\n# Local \naction(type="omfile" file="/var/log/processed/local0.log")\n\n# Syslog over UDP\naction(type="omfwd" target="192.168.0.1" Port="514" Protocol="udp")\nRun Code Online (Sandbox Code Playgroud)\n如果您想对多个文件执行此操作,或者您想将输入聚合到一个文件,那么(可能)最好的方法是创建一个ruleset. 有关更多信息,请参阅rsyslog 文档。
注意: \n这个正则表达式适用于我的特定用例。这取决于template您正在使用的;如果您不提供模板并因此使用标准系统日志格式,这应该可行。
如果情况并非如此,请分析您的日志并创建适合您的用例的表达式。
\n