Narayana Tutorial

Online Java Tutorial Blog

Log4j common conversion patterns


You will find in this article some useful log4j common conversion patterns compiled for daily use of log4j as a preferred logging mechanism in Java development.

log4j common conversion patterns

The following table briefly summarizes the conversion characters defined by log4j:

Log4j common conversion patterns

log4j conversion pattern

 

Conversion Pattern Example

Here we are providing some conversion pattern examples with syntax, description and output.

The example output is based on this small program (assuming log4j is initialized elsewhere. See the article: How to configure log4j as logging mechanism in Java

Log4jSample.java

package com.narayanatutorial.log4j;

import org.apache.log4j.Logger;

public class Log4jSample {

    private static final Logger logger = Logger.getLogger(Log4jExample.class);

    public static void main(String[] args) {
        logger.info("This is a INFO Message ");
    }
}

The above program executing with log4j.rootLogger=TRACE,CONSOLE,

So Log4j level set to TRACE, it will display all types of logs.

See the article: Log4j Level Hierarchy

See the article: Log4j Level Example

Example Pattern [%-5p] %d %c – %m%n

log4j.appender.CONSOLE.layout.ConversionPattern=[%-5p] %d %c – %m%n

Where:

  • The percent sign (%) is a prefix for any conversion characters.
  • -5p is the format modifier for the conversion character p (priority), which left justifies the priority name with minimum 5 characters.
  • Conversion characters are: p, d, c, m and n. These characters are explained in the table below.
  • Other characters are literals: [, ], – and spaces

That pattern will format log messages something like this:

[INFO ] 2016-02-08 06:08:44,928 com.narayanatutorial.log4j.Log4jSample - This is a INFO Message 
[DEBUG] 2016-02-08 06:08:44,928 com.narayanatutorial.log4j.Log4jSample - This is a DEBUG Message 
[WARN ] 2016-02-08 06:08:44,944 com.narayanatutorial.log4j.Log4jSample - This is a WARN Message

Simple Pattern [%p] %c %M – %m%n

log4j.appender.CONSOLE.layout.ConversionPattern=[%p] %c %M – %m%n

That pattern will format log messages something like this:

[INFO] com.narayanatutorial.log4j.Log4jSample main - This is a INFO Message 
[DEBUG] com.narayanatutorial.log4j.Log4jSample main - This is a DEBUG Message 
[WARN] com.narayanatutorial.log4j.Log4jSample main - This is a WARN Message

NOTE: the %n should be used to add new line character at the end of every message.

Standard Pattern [%p] %d %c %M – %m%n

log4j.appender.CONSOLE.layout.ConversionPattern=[%p] %d %c %M – %m%n

That pattern will format log messages something like this:

[INFO] 2016-02-08 06:17:36,632 com.narayanatutorial.log4j.Log4jSample main - This is a INFO Message 
[DEBUG] 2016-02-08 06:17:36,642 com.narayanatutorial.log4j.Log4jSample main - This is a DEBUG Message 
[WARN] 2016-02-08 06:17:36,643 com.narayanatutorial.log4j.Log4jSample main - This is a WARN Message

Date & Time Pattern [%p] %d{MM-dd-yyyy HH:mm:ss} %c %M – %m%n

  • Date and Time( with Seconds) conversion pattern syntax is [%p] %d{MM-dd-yyyy HH:mm:ss} %c %M – %m%n

log4j.appender.CONSOLE.layout.ConversionPattern=[%p] %d{MM-dd-yyyy HH:mm:ss} %c %M – %m%n

That pattern will format log messages something like this:

[INFO] 02-08-2016 06:29:02 com.narayanatutorial.log4j.Log4jSample main - This is a INFO Message 
[DEBUG] 02-08-2016 06:29:02 com.narayanatutorial.log4j.Log4jSample main - This is a DEBUG Message 
[WARN] 02-08-2016 06:29:02 com.narayanatutorial.log4j.Log4jSample main - This is a WARN Message
  • Date and Time( with Milli-Seconds) conversion pattern syntax is [%p] %d{MM-dd-yyyy HH:mm:ss,SSS} %c %M – %m%n

log4j.appender.CONSOLE.layout.ConversionPattern=[%p] %d{MM-dd-yyyy HH:mm:ss,SSS} %c %M – %m%n

That pattern will format log messages something like this:

[INFO] 02-08-2016 06:30:39,079 com.narayanatutorial.log4j.Log4jSample main - This is a INFO Message 
[DEBUG] 02-08-2016 06:30:39,083 com.narayanatutorial.log4j.Log4jSample main - This is a DEBUG Message 
[WARN] 02-08-2016 06:30:39,084 com.narayanatutorial.log4j.Log4jSample main - This is a WARN Message

Thread Pattern [%p] %d [%t] %x %c %M – %m%n

log4j.appender.CONSOLE.layout.ConversionPattern=[[%p] %d [%t] %x %c %M – %m%n

That pattern will format log messages something like this:

[INFO] 2016-02-08 06:32:44,461 [main]  com.narayanatutorial.log4j.Log4jSample main - This is a INFO Message 
[DEBUG] 2016-02-08 06:32:44,466 [main]  com.narayanatutorial.log4j.Log4jSample main - This is a DEBUG Message 
[WARN] 2016-02-08 06:32:44,466 [main]  com.narayanatutorial.log4j.Log4jSample main - This is a WARN Message

 

References

 

Narayanaswamy

Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in IT industry more than 7 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting and Website Development.

I am a self learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.

Leave a Reply

Narayana Tutorial © 2018 Frontier Theme
Show Button
Hide Button