Log4j Date Format Example
This article is a tutorial about date format patterns in Log4j. In this tutorial, we are going to configure log4j via property files.
1. Introduction
Log4J (Java) is a widely used logging framework for Java. It continues to grow continuously with the recent upgrade to Log4j2. Log4j supports logging via Logger, Appender and Layouts.
Logger is the interaction point for the application and carries out the logging activity. It is used to specify the logging mode and the name of the logger. It also delivers logs to the specified destination with the help of the appender. The Appender delivers the log to the logging destination i.e. console, file or database along with options to fine-tune the logging mechanism. Appenders generally have lifecycle configuration and filtering support. Filtering enables to filter the messages whose logging mode does not match the level configured. Log4j supports multiple predefined appenders and also helps create custom appenders.
Layout specifies the display format of the logs. The most commonly used layout for Log4j is PatternLayout. A sample pattern is %d [%t] %-5p (%F: %L) – %m%n
. The format strings for the pattern are as follows:
• Date – Full date until microseconds.
• Thread – JVM thread logging the output.
• Logging Mode – INFO/ERROR/DEBUG/WARN.
• Class – Java Class logging the output.
• Line number – Line number in a java class.
• Message – The message logged.
• Default line separator -/n unless specified otherwise.
In this tutorial, we are going to delve deeper into %d
date pattern.
2. SimpleDateFormat Pattern
We will design a simple logging class to illustrate our scenario with examples.
Example Class
package com.jcg.examples; import org.apache.log4j.Logger; public class LoggerMain { public static final Logger logger = Logger.getLogger(LoggerMain.class); public static void main(String[] args) { int loop = 0; while (loop < 1000) { logger.warn("This is a warn log"); loop++; try { Thread.sleep(20); } catch (InterruptedException e) { logger.error("Error occurred in sleep", e); Thread.currentThread().interrupt(); } } } }
• Line 4 configures a logger with the name as com.jcg.examples.LoggerMain
.
• Line 8 indicates the method used ie logging level for this message.
The next part is configuring the logger via XML file or properties. Below, we have used properties to provide a suitable configuration for the appender and the destination.
Configuration
log4j.rootLogger=DEBUG, console log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.Target=System.out log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p - %m%n
- Line 5 contains the date pattern. Here the pattern string corresponds to the class
java.text.SimpleDateFormat
in java.
The pattern string can be broken down into
- yyyy- year in 4 digits
- MM – Month in 2 digits
- dd – date in 2 digits
- HH – Hour
- mm -minutes
- ss – seconds
- SSS – milli seconds
This pattern is used by the SimpleDateFormat
class to render the date in logging file. Below is a screenshot of such a logging output.
Log4j supports three other customized date format helper classes which we will cover in the below sections.
3. ISO8601 formatter
The above output can be achieved by using the org.apache.log4j.helpers.ISO8601DateFormat
class. Below is the configuration to enable ISO8601DateFormat
class.
ISO8601 Configuration
log4j.rootLogger=DEBUG, console log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.Target=System.out log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.conversionPattern=%d{ISO8601} %-5p - %m%n
- In line 5, instead of a date pattern,
ISO8601
is specified to invokeISO8601DateFormat
class.
The output is exactly similar to the previous output configured by the pattern string yyyy-MM-dd HH:mm:ss,SSS
provided to SimpleDateFormat
class.
4. DateTime formatter
An alternative way of representing the complete time is to use the org.apache.log4j.helpers.DateTimeDateFormat
class. Below is the configuration to enable DateTimeDateFormat
class.
DATE Configuration
log4j.rootLogger=DEBUG, console log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.Target=System.out log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.conversionPattern=%d{DATE} %-5p - %m%n
- In line 5, instead of
ISO8601
pattern,DATE
is specified to invokeDateTimeDateFormat
class.
Below, we can see the output of running date formatter. There is only a subtle difference between the ISO8601
formatter and the DATE
formatter. The date is represented in the expanded form for better readability. It is equivalent to specifying the pattern dd MM YYYY HH:mm:ss,SSS
to SimpleDateFormat
class.
5. AbsoluteTime formatter
Absolute formatter displays only the time part of the log ignoring the day part. This involves using the org.apache.log4j.helpers.AbsoluteTimeDateFormat
class. Below is the configuration is to enable AbsoluteTimeDateFormat
class.
ABSOLUTE Configuration
log4j.rootLogger=DEBUG, console log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.Target=System.out log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.conversionPattern=%d{ABSOLUTE} %-5p - %m%n
- In line 5, instead of
DATE
pattern,ABSOLUTE
is specified to invokeAbsoluteTimeDateFormat
class.
Below, we can see the output of running Absolute formatter. It is equivalent to specifying the pattern HH:mm:ss,SSS
to SimpleDateFormat
class.
6. Execution Steps
- Import the example as a Maven project.
- Maven will import the dependencies automatically.
- Run the project by changing the configuration as mentioned above.
- Stop the running project in eclipse after 5 minutes.
7. Summary
In this section, we have covered SimpleDateFormatter
and log4j’s custom formatters. Log4j recommends its own custom formatters in the helpers
package over SimpleDateFormatter
for performance reasons.
8. Download the Source Code
You can download the full source code of this example here: Log4jDateFormatExample