Jun 13
Log4j is an open source tool developed for putting log statements into your Java application. It’s allows log statements to remain in shipped code while giving the user the ability to enable logging at runtime without modifying any of the application binary.
The easiest way to setup Log4j is to create a log4j.properties file and put in root of your classes directory. A typical example of the log4j.properties file is as follows:
# Set root logger level to INFO and log to console, and log file appender.
log4j.rootLogger=INFO, Console, LogFile
# Set the logger level to DEBUG for all classes under the package com.mycompany.
log4j.logger.com.mycompany=DEBUG
# Define the settings for the console appender.
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{ISO8601} [%t] %-5p %C - %m%n
# Define the settings for the log file appender.
# Log file rotates every month.
log4j.appender.LogFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.LogFile.DatePattern='.'yyyy-MM
# Location to where the log file exists.
log4j.appender.LogFile.file=/path/to/logfile.log
log4j.appender.LogFile.layout=org.apache.log4j.PatternLayout
log4j.appender.LogFile.layout.ConversionPattern=%d{ISO8601} [%t] %-5p %C - %m%n
The ConversionPattern determines what the format of each line logged:
%d{ISO8601}Output the date in ISO8601 format.%tOutput the thread that generated the logging event.%-5pOutput the priority of the logging event left
justified to a width of five characters.%COutput the fully qualified class name of the caller
issuing the logging request.%mOutput the application supplied message associated with
the logging event.%nOutput the platform dependent line separator character or
characters. e.g “\n” or “\n\r”
For more information refer to the Log4j documentation