tomcat-taglibs-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "songjae han" <>
Subject log tag: using logtag with log4j configuration file
Date Wed, 06 Jun 2001 22:07:14 GMT

This email is a proposal to use <context-param> element of web.xml to pass
log4j configuration parameter to log tag handler.

Log tag uses BasicConfigurator of log4j and I wanted to use
PropertyConfigurator that will let me configure appenders and categories
using log4j configuration file.  To do this, Log tag handler needs a name of
log4j configuration file.
I used <context-param> element of web.xml to pass the config filename to log
tag handler.
a little desc of  <context-param> element : lets you define initialization
parameters that are available to all components of the application.

I would be very happy if this(or other implementation which lets me use log
tag with log4j configuration file) be included in log tag.

Below are modified file, a portion of web.xml and log4j
config file

SongJae Han

package org.apache.taglibs.log;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.ServletContext;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Category;
import org.apache.log4j.PropertyConfigurator;


/** Abstract base class for the logging tags which log a message to a
  * log4j category.
  * @author <a href="">Joseph Ottinger</a>
  * @author <a href="">James Strachan</a>

public abstract class LoggerTag extends BodyTagSupport {
    private static boolean initialized = false;
    private void initializeConfigurator() {
        // Force log4j to be initialised on Class load...
        // XXXX: Is there a better, more deployment descriptor
        // way of doing this?
        ServletContext context = pageContext.getServletContext();
        String log4jConfig = context.getInitParameter("log4j");

     if(log4jConfig != null) {
                System.out.println("Using properties configurator");
    URL configURL = new URL(log4jConfig);
                System.out.println("this is the logfile "+log4jConfig);
            }catch(MalformedURLException e) {
            System.out.println("Using basic configurator");
        initialized = true;

    private String category = "";
    private String message;

    public void setCategory(String category) {
        this.category = category;

    public void setMessage(String message) {
        this.message = message;

    // Tag interface

    public int doStartTag() throws JspException  {
            log("initializing log4j configurator");
        if ( message != null ) {
            /** Log now as doAfterBody() may not be called for an empty body
tag */
            log( message );
            return SKIP_BODY;
        return EVAL_BODY_TAG;

    public int doAfterBody() throws JspException {
        if (message == null) {
            log( getBodyContent().getString().trim() );
        return SKIP_BODY;

    // Implementation methods

    protected Category getCategory() {
        return Category.getInstance(category);

    protected abstract void log( String message );

======log4j config file=================
log4j.rootCategory=debug, stdout, logFile
#out to console
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
#out to file
#tomcat uses bin as current directory when writing log
# Pattern to output the caller's file name and line number.
log4j.appender.logFile.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

Do You Yahoo!?
Get your free address at

View raw message