log4j.properties basic examples

  • 09 February 2017
  • ADM


log4j.properties basic examples - images/logos/java.jpg


This tutorial will present minimum log4j.properties configuration file to output the log to console or to file. The settings are for log4j version 1.2.x.

To File

There are a few details that are important in the log4j.properties file:

rootLogger: defines the level of logging. For production usually should be used ERROR only. Using a lower level it might affect the performance. For more details on levels check the Log4j Logging Levels tutorial.

appender location: you can setup the file location.

ConversionPattern: defines the layout of the output.

MaxFileSize: defines the max log file size.

MaxBackupIndex: defines the max number of log files to be kept.

# Root logger option - if you need more info go with DEBUG
log4j.rootLogger=INFO, file

# Direct log messages to a log file

log4j.appender.file.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] [%-5p] %c{1}:%L - %m%n

To Console

# Root logger option
log4j.rootLogger=INFO, stdout

# Direct log messages to stdout
log4j.appender.stdout.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] [%-5p] %c{1}:%L - %m%n

You can add both file and console output in the same log4j.properties file. The difference will be on the rootLogger

log4j.rootLogger=INFO, file, stdout


The following table explains the characters used in the ConversionPattern and all other characters that you can use in your custom pattern:

Conversion Character Meaning
c Used to output the category of the logging event. For example, for the category name "a.b.c" the pattern %c{2} will output "b.c".
C Used to output the fully qualified class name of the caller issuing the logging request. For example, for the class name "org.apache.xyz.SomeClass", the pattern %C{1} will output "SomeClass".
d Used to output the date of the logging event. For example, %d{HH:mm:ss,SSS} or %d{dd MMM yyyy HH:mm:ss,SSS}.
F Used to output the file name where the logging request was issued.
l Used to output location information of the caller which generated the logging event.
L Used to output the line number from where the logging request was issued.
m Used to output the application supplied message associated with the logging event.
M Used to output the method name where the logging request was issued.
n Outputs the platform dependent line separator character or characters.
p Used to output the priority of the logging event.
r Used to output the number of milliseconds elapsed from the construction of the layout until the creation of the logging event.
t Used to output the name of the thread that generated the logging event.
x Used to output the NDC (nested diagnostic context) associated with the thread that generated the logging event.
X The X conversion character is followed by the key for the MDC. For example, X{clientIP} will print the information stored in the MDC against the key clientIP.
% The literal percent sign. %% will print a % sign.