0

I want to format the timestamp in my structured logs. Currently I defined the logback.xml like:

<configuration>
<appender name="json" class="ch.qos.logback.core.ConsoleAppender">
    <encoder class="net.logstash.logback.encoder.LogstashEncoder">
        <fieldNames>
            <timestamp>timestamp</timestamp>
            <logger>[ignore]</logger>
            <version>[ignore]</version>
            <levelValue>[ignore]</levelValue>
            <stackTrace>exception</stackTrace>
        </fieldNames>
    </encoder>
</appender>

<root name="jsonLogger" level="DEBUG">
    <appender-ref ref="json"/>
</root>

</configuration>

Using <fieldNames>I am able to change the name of the timestamp field.

How can I change the pattern of the timestamp via the configuration in logback.xml?

xtra
  • 1,957
  • 4
  • 22
  • 40
  • https://dzone.com/articles/logback-configuration-using-xml https://www.mkyong.com/logging/logback-xml-example/ – pvpkiran Oct 02 '19 at 08:53

1 Answers1

5

If figured out that I can add <timeZone> and <timestampPattern> to the encoder to format my timestamp.

The complete configuration then becomes:

<configuration>
<appender name="json" class="ch.qos.logback.core.ConsoleAppender">
    <encoder class="net.logstash.logback.encoder.LogstashEncoder">
        <timeZone>UTC</timeZone>
        <timestampPattern>yyyy-MM-dd'T'HH:mm:ss.SSS</timestampPattern>
        <fieldNames>
            <timestamp>timestamp</timestamp>
            <logger>[ignore]</logger>
            <version>[ignore]</version>
            <levelValue>[ignore]</levelValue>
            <stackTrace>exception</stackTrace>
        </fieldNames>
    </encoder>
</appender>

<root name="jsonLogger" level="DEBUG">
    <appender-ref ref="json"/>
</root>

</configuration>
xtra
  • 1,957
  • 4
  • 22
  • 40