Log4j

 
 
Log4j is a package to output log statements to a variety of output targets.

The log statements can remain in your shipped code without incurring a heavy performance cost. The logging behavior can be controlled by editing a configuration file (log4j.properties), without modifying the application.

One of the distinctive features of log4j is the notion of inheritance in loggers. By using a logger hierarchy it is possible to control which log statements are output at arbitrarily fine granularity. This helps reduce the volume of logged output and minimize the cost of logging.

The target of the log output can be a file, an OutputStream, a java.io.Writer, a remote log4j server, a remote Unix Syslog daemon, or even a NT Event logger among many other output targets.

Log4j has three main components (loggers, appenders and layouts) which works together to enable developers to log messages according to message type and level, and to control at runtime how these messages are formatted and where they are reported.

Log4j is distributed at no charge for commercial or non-commercial use. For more information read the LICENSE.txt file.

More information about log4j can be found at http://logging.apache.org/log4j/docs/index.html

The latest Log4j version can be downloaded from http://logging.apache.org/log4j/docs/download.html

rss feed






Using appenders.



Information
Log4j allows logging requests to print to multiple output destinations, also known as an appender. There are several output destinations:
  • Console:
    • org.apache.log4j.ConsoleAppender
      Appends log events to System.out or System.err.

  • Files:
  • GUI components.

  • Remote socket servers:
    • org.apache.log4j.net.SocketAppender


  • JMS:
    • org.apache.log4j.net.JMSAppender
      Sends messages using Java Messaging Service.

  • NT Event Loggers:
    • org.apache.log4j.nt.NTEventLogAppender
      Append to the NT event log system.

  • Remote UNIX Syslog daemons:
    • org.apache.log4j.net.SyslogAppender
It is also possible to log asynchronously.
The appenders are defined in the log4j.properties configuration file.

Operating system used
Windows XP Home Edition Version 5.1 SP 2

Software prerequisites
Log4j 1.2.9


Procedure
  1. Create the following directories:

    c:\demo
    c:\demo\com
    c:\demo\com\mobilefish


  2. Create the file c:\demo\com\mobilefish\Log4jDemo2.java.

    package com.mobilefish;
    import org.apache.log4j.Logger;

    public class Log4jDemo2 {

          static Logger log = Logger.getLogger("com.mobilefish.Log4jDemo2");

          public static void main(String args[]) {

                for(int i=1 ; i<50000; i++) {
                   System.out.println("Counter = " + i);
                   log.debug("This is my debug message. Counter = " + i);
                   log.info("This is my info message. Counter = " + i);
                   log.warn("This is my warn message. Counter = " + i);
                   log.error("This is my error message. Counter = " + i);
                   log.fatal("This is my fatal message.Counter = " + i);
                }
          }
    }


  3. Open a dos window and type:

    cd c:\demo

  4. Compile the java code, type:

    javac ./com/mobilefish/Log4jDemo2.java

  5. Create file c:\demo\log4j.properties and define several appenders:

    log4j.rootLogger=ERROR, A2

    ########## Appender A1
    log4j.appender.A1=org.apache.log4j.ConsoleAppender
    log4j.appender.A1.layout=org.apache.log4j.PatternLayout
    log4j.appender.A1.layout.ConversionPattern=[%5p] %d{mm:ss} (%F:%M:%L)%n%m%n%n
    ########## Appender A2
    log4j.appender.A2=org.apache.log4j.FileAppender
    log4j.appender.A2.File=c:/demo/app_a2.log
    # Append to the end of the file or overwrites the file at start.
    log4j.appender.A2.Append=false
    log4j.appender.A2.layout=org.apache.log4j.PatternLayout
    log4j.appender.A2.layout.ConversionPattern=[%5p] %d{mm:ss} (%F:%M:%L)%n%m%n%n
    ########## Appender A3
    log4j.appender.A3=org.apache.log4j.RollingFileAppender
    log4j.appender.A3.File=c:/demo/app_a3.log
    # Set the maximum log file size (use KB, MB or GB)
    log4j.appender.A3.MaxFileSize=3000KB
    # Set the number of log files (0 means no backup files at all)
    log4j.appender.A3.MaxBackupIndex=5
    # Append to the end of the file or overwrites the file at start.
    log4j.appender.A3.Append=false
    log4j.appender.A3.layout=org.apache.log4j.PatternLayout
    log4j.appender.A3.layout.ConversionPattern=[%5p] %d{mm:ss} (%F:%M:%L)%n%m%n%n
    ########## Appender A4
    log4j.appender.A4=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.A4.File=c:/demo/app_a4.log
    # Roll the log file at a certain time
    log4j.appender.A4.DatePattern='.'yyyy-MM-dd-HH-mm
    # Append to the end of the file or overwrites the file at start.
    log4j.appender.A4.Append=false
    log4j.appender.A4.layout=org.apache.log4j.PatternLayout
    log4j.appender.A4.layout.ConversionPattern=[%5p] %d{mm:ss} (%F:%M:%L)%n%m%n%n


    Note: Use forward slashes in log4j.appender.A2.File=c:/demo/app_a2.log.



  6. Demonstrate FileAppender, run your application:

    java com.mobilefish.Log4jDemo2

    By using appender A2 no log messages are displayed on the console and all log messages a written to one large log file C:\demo\app_a2.log:

    [ERROR] 58:11 (Log4jDemo2.java:main:15)
    This is my error message. Counter = 1

    [FATAL] 58:11 (Log4jDemo2.java:main:16)
    This is my fatal message. Counter = 1
    :


  7. Demonstrate RollingFileAppender, change the following line in c:\demo\log4j.properties:

    log4j.rootLogger=ERROR, A3

  8. Run your application:

    java com.mobilefish.Log4jDemo2

    By using appender A3 the log file app_a3.log will be rolled over when it reaches 3000KB. When the roll-over occurs, the app_a3.log is automatically moved to app_a3.log.1. When app_a3.log again reaches 3000KB, app_a3.log.1 is moved to app_a3.log.2 and app_a3.log is moved to app_a3.log.1.

    The maximum number of backup log files is set to MaxBackupIndex=5, which means app_a3.log.5 is the last file created.

    [ERROR] 58:11 (Log4jDemo2.java:main:15)
    This is my error message. Counter = 1

    [FATAL] 58:11 (Log4jDemo2.java:main:16)
    This is my fatal message. Counter = 1
    :


  9. Demonstrate DailyRollingFileAppender, change the following line in c:\demo\log4j.properties:

    log4j.rootLogger=ERROR, A4

  10. Run your application:

    java com.mobilefish.Log4jDemo2

    By using appender A4 the log file app_a4.log will be rolled over depending on the date format (= Java SimpleDateFormat ) used.

    The suffix for the backup files can also be set. Literal text must be placed within a pair of single quotes.

    Here are a few examples:

    log4j.appender.A4.DatePattern= Description
    '_'yyyy-MM Roll log file on the first of each month
    Example: app_a4.log_2006-02
    '_'yyyy-ww Roll log file on the first of each week
    Example: app_a4.log_2006-08
    '_'yyyy-MM-dd Roll log file at midnight everyday
    Example: app_a4.log_2006-02-25
    '_'yyyy-MM-dd-a Roll log file at midnight and midday everyday
    Example: app_a4.log_2006-02-25-PM
    '_'yyyy-MM-dd-HH Roll log file at the start of every hour
    Example: app_a4.log_2006-02-25-15
    '_'yyyy-MM-dd-HH-mm Roll log file at the beginning of every minute
    Example: app_a4.log_2006-02-25-15-05
    '_'yyMMddHHmm Roll log file at the beginning of every minute
    Example: app_a4.log_0602251516
    '_'EEE-d-MMM-yyyy-HHmm Roll log file at the beginning of every minute
    Example: app_a4.log_za-25-feb-2006-1520
    '_'yyyy.MMMMM.dd.HHmm Roll log file at the beginning of every minute
    Example: app_a4.log_2006.februari.25.1527
    '_Date_'yyyy.MM.dd'_Time_'HHmm Roll log file at the beginning of every minute
    Example: app_a4.log_Date_2006.02.25_Time_1531