0

I am looking for to print-out a log in a custom manner.

For example,

currently we have following log structure in form of JSON,

{
  "timeMillis" : 1488791217953,
  "thread" : "restartedMain",
  "level" : "DEBUG",
  "loggerName" : "org.springframework.jdbc.datasource.DriverManagerDataSource",
  "message" : "hello world",
  "endOfBatch" : false,
  "loggerFqcn" : "org.apache.commons.logging.impl.SLF4JLocationAwareLog",
  "threadId" : 17,
  "threadPriority" : 5
}

Now I found that, there were couple of fields are still missing which are important for me to have in a log,

Expected JSON would be likewise :

{
  "timeMillis" : 1488791217953,
  "thread" : "restartedMain",
  "level" : "DEBUG",
  .................. 
  "file" : "p1.pck.HelloWorld.java",
  "line" : "190",
  "application-id" : "101",
  "logged in user id " : "199",
  "etc" : "etc"
  ..................
  "threadPriority" : 5
}

NOTE : the log configuration file log4j2.yml has following configuration

JsonLayout:
          propertiesAsList: true
flyx
  • 35,506
  • 7
  • 89
  • 126
Vishal Gajera
  • 4,137
  • 5
  • 28
  • 55

1 Answers1

0

You'll want to include the log4j-web artifact to get context data in your log -

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-web</artifactId>
    <version>2.8.2</version>
</dependency>

Also, you may find an artifact I created useful [extended-jsonlayout]. It allows you to add additional information to the json log message by implementing a simple interface, and including the jar on your classpath. You can check it out here -
https://github.com/savantly-net/log4j2-extended-jsonlayout

Jeremy
  • 2,970
  • 1
  • 26
  • 50