Tomcat log4j setup

From Oxxus Wiki

Jump to: navigation, search

log4j is a popular logging package for Java developers and provides facilities of message and error logging while not negatively impacting the speed of your application.

Loggers can be arranged in a hierarchy and they can also be assigned levels to further control the process of reporting important messages in your application.

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 many other output targets.

There are 6 logger levels you can use: TRACE, DEBUG, INFO, WARN, ERROR and FATAL. Logger levels are arranged in a hierarchy: DEBUG < INFO < WARN < ERROR < FATAL.

Under Tomcat 3.x and 4.x, you should place the log4j.properties under the WEB-INF/classes directory of your web-applications. Log4j will find the properties file and initialize itself. This is easy to do and it works.

You can also choose to set the system property log4j.configuration before starting Tomcat. For Tomcat 3.x The TOMCAT_OPTS environment variable is used to set command line options. For Tomcat 4.0, set the CATALINA_OPTS environment variable instead of TOMCAT_OPTS.

TOMCAT_OPTS="-Dlog4j.configuration=log4j.conf"

You can obtain an instance of Logger object by using the following code.

Logger  logger = Logger.getLogger("myapp.mylogger");

You can assign a level to your logger object. This is usually done from a configuration file, so that you can control the output of your logger object without the need to rebuild the entire application.

logger.setLevel(Level.INFO);

Depending on the level of your logger various messages will either be displayed or hidden.

log4j class

This is basic overlay of the Logger class and it's methods.

  package org.apache.log4j;

  public class Logger {

    // Creation & retrieval methods:
    public static Logger getRootLogger();
    public static Logger getLogger(String name);

    // printing methods:
    public void trace(Object message);
    public void debug(Object message);
    public void info(Object message);
    public void warn(Object message);
    public void error(Object message);
    public void fatal(Object message);

    // generic printing method:
    public void log(Level l, Object message);
}

log4j example

Here is a short example of using log4j.

import myapp.Test;
import org.apache.log4j.Logger;

public class MyApp {
  static Logger logger = Logger.getLogger(MyApp.class);

  public static void main(String[] args) {
    logger.info("Starting ..");
    Test test = new Test();
    test.callMe();
    logger.info(".. exiting.");
  }
}

package myapp;
import org.apache.log4j.Logger;

public class Test {
  static Logger logger = Logger.getLogger(Test.class);

  public void callMe() {
    logger.debug("Method called!");
  }
}

External links

Contact About Us Support Network Servers Java Hosting Oxxus.net Order Now! Dedicated Servers VPS Hosting Tomcat Hosting Java Hosting Money Back Guarantee Privacy Policy Oxxus.net Terms of 
Service Contact About Us Servers Networks Support Domain Names SSL Certificates Java Wiki Tutorials E-learning 
Platforms