Home | Articles | Resumé | yCamCap |  
Minimalistic guide to using log4j
Prerequisite:


1. Log4j(Ver. 1.2.x or better)

2. JDK(1.3 or better)

3. Any Editor  

Warm up :

extract the log4j_xxxx.jar (xxxx is the version no.) from the distribution and put it in the classpath. for jsp/servlet containers put it in web-inf/classes/lib so that it is part of the distribution.

Starting out :

import the needed classes :


//for .java files

import org.apache.log4j.*;

//for .jsp files

<%@ page import="org.apache.log4j.*"%>

Configuring for the properties file: log4j needs to know where if the configuration files for it. should be named "log4j.properties"

//for .java files

public class myclass{

  static Logger logger = Logger.getLogger(myclass.class.getName());
  String LogConfigFilePath = null;

  publicmyclass(String String LogConfigFilePath){
    this.LogConfigFilePath = LogConfigFilePath;
    BasicConfigurator.resetConfiguration();
    PropertyConfigurator.configure(LogConfigFilePath);
  }

}

//for a webapp (ie jsp)

<% 
Logger logger = Logger.getLogger(getClass());

//this will look in "webappdirname"/config/ directory

//String LogConfigFilePath = getServletContext().getRealPath("/") + "config//log4j.properties"

//we can also give absolute path

String LogConfigFilePath = "c:\\log4j.properties"
BasicConfigurator.resetConfiguration();
PropertyConfigurator.configure(LogConfigFilePath);
%>

Preparing properties file :

just copy following lines and save it as log4j.properties and put it in the place you have specified above.


#we can change the mode from debug to info, error, warn, fatal
log4j.rootLogger=debug, stdout,R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

#this will append the log messeges in a file
log4j.appender.R=org.apache.log4j.RollingFileAppender

#change it to directory where you want to save your log files

log4j.appender.R.File=c:\\mylogfile.log
log4j.appender.R.MaxFileSize=100KB

# Keep one backup file
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n


Getting the log messeges :

okey.. so you have used System.out.println till this time. Now this can be achieved by using thsese methods:

logger.info("somemessage"); //will be shown if rootlogger in properties file is set to info

logger.debug("somemessage"); //will be shown if rootlogger in properties file is set to debug

logger.warn("somemessage"); //will be shown if rootlogger in properties file is set to warn

logger.error("somemessage"); //will be shown if rootlogger in properties file is set to error

logger.fatal("somemessage"); //will be shown if rootlogger in properties file is set to fatal

including the stack trace :

logger.info/debug/warn/error/fatal("some message",Exception object);

That's it. For more information look in log4j documentation.

//Report errors/questions to p.kumar@pkumar.de