Log4j2 – Useful Conversion Patterns

Some useful and ready-made log4j2 conversion patterns are for reference so we don’t waste time building these patterns every time we are creating/editing log4j configuration files.

I am using the below log statements for generating the logs.

LOGGER.debug("Debug Message Logged !!");
LOGGER.info("Info Message Logged !!");

Now, I will list down different log patterns and their respective generated outputs.

1. %d [%p] %c{1} – %m%n

Use it for simple logging i.e. date, level, logger, message. It will generate below output:

2016-06-20 19:18:02,958 [DEBUG] Log4j2HelloWorldExample - Debug Message Logged !!
2016-06-20 19:18:02,959 [INFO] Log4j2HelloWorldExample - Info Message Logged !!

2. %d [%-6p] %c{1} – %m%n

Use it for simple logging with pretty print logger level. It will generate below output:

2016-06-20 19:21:05,271 [DEBUG ] Log4j2HelloWorldExample - Debug Message Logged !!
2016-06-20 19:21:05,272 [INFO  ] Log4j2HelloWorldExample - Info Message Logged !!

3. %d [%-6p] %c{1} – %m%n

Use it for printing the complete package level. It will generate below output:

2016-06-20 19:22:05,379 [DEBUG ] com.howtodoinjava.log4j2.examples.Log4j2HelloWorldExample - Debug Message Logged !!
2016-06-20 19:22:05,380 [INFO  ] com.howtodoinjava.log4j2.examples.Log4j2HelloWorldExample - Info Message Logged !!

4. %d [%-6p] %c{3} – %m%n

Use it for printing the package level upto two levels. It will generate below output:

2016-06-20 19:23:48,202 [DEBUG ] log4j2.examples.Log4j2HelloWorldExample - Debug Message Logged !!
2016-06-20 19:23:48,204 [INFO  ] log4j2.examples.Log4j2HelloWorldExample - Info Message Logged !!

5. %d{yyyy/MM/dd HH:mm:ss,SSS} [%-6p] %c{1} – %m%n

Use it for custom date format. It will generate below output:

2016/06/20 19:24:45,076 [DEBUG ] Log4j2HelloWorldExample - Debug Message Logged !!
2016/06/20 19:24:45,078 [INFO  ] Log4j2HelloWorldExample - Info Message Logged !!

6. %d [%-6p] %C{1}.%M(%F:%L) – %m%n

Use it for the caller class, method, source file and line number. It will generate below output:

2016-06-20 19:25:42,249 [DEBUG ] Log4j2HelloWorldExample.methodOne(Log4j2HelloWorldExample.java:14) - Debug Message Logged !!
2016-06-20 19:25:42,251 [INFO  ] Log4j2HelloWorldExample.methodOne(Log4j2HelloWorldExample.java:15) - Info Message Logged !!

7. %sn %d{yyyy/MM/dd HH:mm:ss,SSS} %r [%-6p] [%t] %c{3} %C{3}.%M(%F:%L) – %m%n

Use it to capture everything discussed above. It will generate below output:

1 2016/06/20 19:27:03,595 620 [DEBUG ] [main] log4j2.examples.Log4j2HelloWorldExample log4j2.examples.Log4j2HelloWorldExample.main(Log4j2HelloWorldExample.java:14) - Debug Message Logged !!
2 2016/06/20 19:27:03,597 622 [INFO  ] [main] log4j2.examples.Log4j2HelloWorldExample log4j2.examples.Log4j2HelloWorldExample.main(Log4j2HelloWorldExample.java:15) - Info Message Logged !!

Feel free to change and use any pattern as per your need.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

3 thoughts on “Log4j2 – Useful Conversion Patterns”

  1. Hi , I want to print log message only for my custom log appender created to log particular information to different log file .But though I am using conversion pattern “%m%n” , it is printing date ,thread,class information as well which I dont need . Is there any extra setting for custom appenders ?

    Reply
    • Checkout the highlight property of patterns. e.g.

      pattern="%highlight{%d{HH:mm:ss.SSS} %-5level %logger{36}.%M() @%L - 
      %msg%n}{FATAL=red blink, ERROR=red, WARN=yellow bold, INFO=black, DEBUG=green bold, TRACE=blue}"
      Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.