For a project written in Java and based on Spring Boot, I’m using Syslog for logging. Recently I started liking using YAML for configuration files. There aren’t many nontrivial examples online for Log4J 2.x configuration for Syslog written in YAML. Since it can be tricky to write the configuration properly, I’m sharing with you a working example.

You’ll need to define the host and port of your syslog server, the protocol used and facility for the messages.

In this project, I’m using Spring Cloud Sleuth to easily track correlated events and messages in log files.

In the LoggerFields section, you can specify which fields to include in the message and which format to use. And using the mdcExcludes property you can exclude some MDC keys.

Configuration:

  Properties:

  Appenders:

    Syslog:
      name: syslog
      host: 172.17.0.1
      port: 514
      protocol: UDP
      facility: local6
      connectTimeoutMillis: 10000
      reconnectionDelayMillis: 5000
      mdcId: mdc
      mdcExcludes: "X-B3-SpanId,X-B3-TraceId,X-Span-Export,spanExportable,spanId,traceId"
      messageId: mywebapp
      appName: mywebapp
      charset: UTF-8
      ignoreExceptions: true
      newLine: true
      format: RFC5424
      LoggerFields:
        KeyValuePair: !!pairs
          - key: priority
            value: "%p"
          - key: category
            value: "%c{0}"
          - key: pid
            value: "%pid"
          - key: traceSpanId
            value: "%X{traceId}/%X{spanId}"
          - key: exception
            value: "%ex"

  Loggers:

    Root:
      level: WARN
      AppenderRef:
        - ref: syslog

    Logger:
      - name: xyz.joshefin.app
        level: DEBUG
      - name: org.springframework
        level: WARN

And here is an example of a line from the log file:

2019-06-21T20:56:41.847Z myserver mywebapp info Just a message. [mdc@18060 category="MyFavouriteClass" exception="" pid="1234" priority="INFO" traceSpanId="f54b0ca63e9a3070/f54b0ca63e9a3070"]
2 comments
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like