log4j

Posted On December 9, 2007

Filed under Uncategorized
Tags: ,

Comments Dropped leave a response

Log4jExample.java

package com.filler.l4j;

import org.apache.log4j.Logger;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.IOException;

public class Log4jExample
{
  private static Logger log = Logger.getLogger(Log4jExample.class);

  public static void main(String args[])
  {
    log.info("I am the one that jaded you!");
  }
}

log4j.properties

###the name needs to be log4j.properties
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%t] %C{1} - %m\n

###log4j.category.com.filler.l4j.Log4jExample=debug, stdout
###log4j.rootLogger=debug, stdout
log4j.rootCategory=debug, stdout

Compile and Run on Linux (output will be written on console)

javac -classpath log4j.jar:./ -d ./ Log4jExample.java
java -classpath log4j.jar:./ com.filler.l4j.Log4jExample

Compile and Run on Windows

javac -classpath log4j.jar;.\ -d .\ Log4jExample.java
java -classpath log4j.jar;.\ com.filler.l4j.Log4jExample

# Note the semicolon delimiter for the classpath in windows command, as opposed to colon in linux
# The order for the standards of logging is FATAL > ERROR > WARN > INFO > DEBUG > TRACE. So, if you set log4j.rootLogger as warn,stdout; only fatal or error or warn messages will be logged. You will not see messages of lower priority.
# Currently, console is being used. log4j.properties can be configured to use a FileLogger. Also, the pattern or format of writing a message can be described using the weird % symbols :)

Respond now.