Log4j Common Conversion Patterns Example
- Details
- Written by Nam Ha Minh
- Last Updated on 01 July 2019   |   Print Email
Where:
- The percent sign (%) is a prefix for any conversion characters.
- Conversion characters are: p, d, c, m and n. These characters are explained in the table below.
- -5p is the format modifier for the conversion character p (priority), which left justifies the priority name with minimum 5 characters.
- Other characters are literals: [, ], - and spaces
That pattern will format log messages something like this:
[DEBUG] 2012-11-02 21:57:53,661 MyClass - this is a debug log message [INFO ] 2012-11-02 21:57:53,662 MyClass - this is a information log message [WARN ] 2012-11-02 21:57:53,662 MyClass - this is a warning log messageThe following table briefly summarizes the conversion characters defined by log4j:
What to print | Conversion character | Performance |
Category name (or logger name) | c | Fast |
Fully qualified class name | C | Slow |
Date and time | d d{format} | Slow if using JDK’s formatters. Fast if using log4j’s formatters. |
File name of Java class | F | Extremely slow |
Location (class, method and line number) | l | Extremely slow |
Line number only | L | Extremely slow |
Log message | m | Fast |
Method name | M | Extremely slow |
Priority (level) | p | Fast |
New line separator | n | Fast |
Thread name | t | Fast |
Time elapsed (milliseconds) | r | Fast |
Thread’s nested diagnostic context | x | Fast |
Thread’s mapped diagnostic context | X | Fast |
Percent sign | %% | Fast |
import java.io.File; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; public class MyClass { static Logger logger = Logger.getLogger(MyClass.class); void foo() { logger.info("this is a log message"); } }
1. Simple log4j conversion pattern
Perhaps the simplest pattern contains only the log message, but that is not informative for debugging. So a simple pattern would contain priority, category, method name, and message.Pattern: [%p] %c %M - %m%n[INFO] MyClass - foo - this is a log message
NOTE: the %n should be used to add new line character at the end of every message.2. Standard log4j conversion pattern
A standard (and probably most used) pattern would contain the following information: priority, date and time, category, method, and message.Pattern: [%p] %d %c %M - %m%nExample output:[INFO] 2012-11-02 22:46:43,896 MyClass foo - this is a log message3. Log4j Conversion patterns for date and time
Sometimes it is critical to measure time in the log output. The %d conversion character outputs date and time in ISO8601 format by default. However we can use a date format specifier to format the date and time explicitly, for example:%d{MM-dd-yyyy HH:mm:ss,SSS}
If you need time granularity only to second:- Pattern: [%p] %d{MM-dd-yyyy HH:mm:ss} %c %M - %m%n
- Example output:
[INFO] 11-02-2012 23:08:10 MyClass foo - this is a log message
If you need time granularity to milliseconds:- Pattern: [%p] %d{MM-dd-yyyy HH:mm:ss,SSS} %c %M - %m%n
- Example output:
[INFO] 11-02-2012 23:09:38,304 MyClass foo - this is a log message
Using ISO8601 date format specifier:
- Pattern: [%p] %d{ISO8601} %c %M - %m%n
- Example output:
[INFO] 2012-11-02 23:13:58,300 MyClass foo - this is a log message
Using ABSOLUTE date format specifier:- Pattern: [%p] %d{ABSOLUTE} %c %M - %m%n
- Example output:
[INFO] 23:17:07,097 MyClass foo - this is a log message
Using DATE date format specifier:- Pattern: [%p] %d{DATE} %c %M - %m%n
- Example output:
[INFO] 02 Nov 2012 23:18:19,547 MyClass foo - this is a log message
4. Log4j Conversion patterns for thread
For debugging multi-threaded applications, log4j provides the following conversion characters:- %t: thread name
- %x: thread’s nested diagnostic context.
- %X: thread’s mapped diagnostic context.
Pattern: [%p] %d [%t] %x %c %M - %m%nExample output:[INFO] 2012-11-02 23:28:26,178 [main] MyClass foo - this is a log messageRelated Java log4j Tutorials:
- How to configure log4j as logging mechanism in Java
- How to configure log4j for creating daily rolling log files
- How to initialize log4j for Java web application
- How to use log4j in Spring MVC
- How to use log4j in Struts2
Other Java Coding Tutorials:
- 10 Common Mistakes Every Beginner Java Programmer Makes
- 10 Java Core Best Practices Every Java Programmer Should Know
- How to become a good programmer? 13 tasks you should practice now
Comments
It seems log4j's pattern is taken care of, but can you please at least list some examples of JDK's date pattern?
Thank you! :)
I have one suggestion. It would be nice if you could list the JDK's and log4j's date format patterns so we can see which ones to avoid if we want to keep the performance cost minimal.
Again, super helpful! :D
Cheers