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

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.