HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions
  • Dark Mode

Log4j2 – Useful Conversion Pattern Examples

By Lokesh Gupta | Filed Under: Log4j2

Below log4j2 conversion patterns are for reference only so that you and me don’t waste time to build these patterns everytime we are creating/editing log4j configuration files.

I am using 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.

%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 !!

%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 !!

%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 !!

%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 !!

%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 !!

%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 !!

%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 !!

TwitterFacebookLinkedinRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

2
Leave a Reply

This comment form is under antispam protection
1 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
This comment form is under antispam protection
  Subscribe  
newest oldest most voted
Notify of
Utsav

how can i Colorcode different log level like, DEBUG in Blue, Error in Red using log4j2

Vote Up0Vote Down  Reply
1 year ago
Lokesh Gupta

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}"
Vote Up0Vote Down  Reply
1 year ago

Search Tutorials

Log4j2 Tutorial

  • Log4j2 – Introduction
  • Log4j2 – JSON Config
  • Log4j2 – Properties Config
  • Log4j2 – XML Config
  • Log4j2 – RollingFileAppender
  • Log4j2 – Multiple appenders
  • Log4j2 – LevelRangeFilter
  • Log4j2 – HTMLLayout
  • Log4j2 – Fish Tagging
  • Log4j2 – Conversion Patterns
  • Log4j2 – JUnit

Log4j Tutorial

  • Log4j – Introduction
  • Log4j – Properties Config
  • Log4j – XML Config
  • Log4j – Maven Config
  • Log4j – Logging Levels
  • Log4j – ConsoleAppender
  • Log4j – RollingFileAppender
  • Log4j – SocketAppender
  • Log4j – JDBCAppender
  • Log4j – XMLLayout
  • Log4j – HTMLLayout
  • Log4j – Runtime Reload
  • Log4j vs. SLF4j
  • Log4j – RESTEasy + Tomcat 7

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Collections in Java
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Python Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap

wpDiscuz