Setting Up Log4j Quickstart Guide

Add comments

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.
  • %t Output the thread that generated the logging event.
  • %-5p Output the priority of the logging event left
    justified to a width of five characters.
  • %C Output the fully qualified class name of the caller
    issuing the logging request.
  • %m Output the application supplied message associated with
    the logging event.
  • %n Output the platform dependent line separator character or
    characters. e.g “\n” or “\n\r”

For more information refer to the Log4j documentation

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in