LoggerFields for Syslog (output log priority and s

2019-08-12 07:37发布

问题:

Log4j can send logs to Papertrail using a syslog appender (documentation):

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

Notice the handy ConversionPattern property.


I have managed to get something similar rolling for 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 

It works and outputs using the default RFC5424 format:

May 14 14:31:36 [server] [app]: My log message

What I really want is a message with priority information as well as full stack traces:

May 14 14:31:36 [server] [app]: ERROR My log message MyException
        at package1.MyClass.method1(MyClass.java:20)
        at ...

Looks like LoggerFields is supposed to do it, but I can't get it to work:

 loggerFields:
   LoggerFields:
     KeyValuePair:
       key: priority
       value: "%p"
     KeyValuePair:
       key: exception
       value: "%ex"

How can I get Log4j2 to output to syslog using the desired format?

回答1:

Please try setting RFC5424Layout's exceptionPattern attribute to %ex.