Log4j可以使用syslog appender(documentation)将日志发送到Papertrail:
log4j.appender.syslog=org.apache.log4j.net.SyslogAppender
log4j.appender.syslog.Facility=LOCAL7
log4j.appender.syslog.FacilityPrinting=false
log4j.appender.syslog.Header=true
log4j.appender.syslog.SyslogHost=<host>.papertrailapp.com:XXXXX
log4j.appender.syslog.layout=org.apache.log4j.PatternLayout
log4j.appender.syslog.layout.ConversionPattern=%p: (%F:%L) %x %m %n
注意方便的ConversionPattern
属性。
我已经设法为log4j2获得了类似的东西:
Syslog:
name: Syslog
id: [my-id]
appName: [my-app]
protocol: UDP
format: RFC5424
facility: local7
host: <host>.papertrailapp.com
port: XXXXX
includeMDC: true
mdcId: mdc
它使用默认的RFC5424格式工作和输出:
May 14 14:31:36 [server] [app]: My log message
我真正想要的是一条包含优先级信息和完整堆栈跟踪的消息:
May 14 14:31:36 [server] [app]: ERROR My log message MyException
at package1.MyClass.method1(MyClass.java:20)
at ...
看起来LoggerFields应该这样做,但我无法让它工作:
loggerFields:
LoggerFields:
KeyValuePair:
key: priority
value: "%p"
KeyValuePair:
key: exception
value: "%ex"
如何使用所需格式将Log4j2输出到syslog?