Log4j2 – Useful Log Format Pattern Layouts

This Log4j2 tutorial lists some useful and ready-made log4j2 formatting patterns for reference so we don’t waste time building these patterns every time we are creating/editing log4j configuration.

Just to mention, we use the log format pattern layout as follows. Notice the pattern property.

<Configuration status="warn">
    	<!-- Console appender configuration -->
        <Console name="console" target="SYSTEM_OUT">
                pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
    	<!-- Root logger referring to console appender -->
        <Root level="info" additivity="false">
            <AppenderRef ref="console" />

For demo purposes, we are using the below log statements for generating the logs.

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

1. Simple Log Formatting

%d [%p] %c{1} - %m%n

Use it for simple logging i.e. date, level, logger, message. It will generate the below log messages.

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. Left Justified Log Level

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

Using [%-6p], the logging level should be left-justified to a width of six characters. Use it for simple logging with a pretty printed log level. It will generate the 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. Printing Package Information

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

Use %c{1} for printing the complete package level. It will generate the 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

%c{3} will print the package level upto two levels.

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. Custom Date Pattern

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

Use it for custom date format. It will generate the 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. Detailed File Name, Method Name and Line Number

%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. Fully Detailed Information

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

Leave a Reply

Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial