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");
}
} [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.%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
- %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 message
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.