tomcat-taglibs-user mailing list archives

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

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

Background:
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 LoggerTag.java file, a portion of web.xml and log4j
config file

Thanks,
SongJae Han
song_jae@yahoo.com


====modified LoggerTag.java==========
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;

import java.net.*;

/** Abstract base class for the logging tags which log a message to a
  * log4j category.
  *
  * @author <a href="mailto:joeo@epesh.com">Joseph Ottinger</a>
  * @author <a href="mailto:jstrachan@apache.org">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) {
            try{
                System.out.println("Using properties configurator");
    URL configURL = new URL(log4jConfig);
    PropertyConfigurator.configure(configURL);
                System.out.println("this is the logfile "+log4jConfig);
            }catch(MalformedURLException e) {
    PropertyConfigurator.configure(log4jConfig);
            }
     }else{
            System.out.println("Using basic configurator");
            BasicConfigurator.configure();
     }
        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  {
        if(!initialized){
            initializeConfigurator();
            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 );
}

=================================
=======web.xml====================
<web-app>
...
    <context-param>
        <param-name>log4j</param-name>
        <param-value>file:../webapps/log4j.properties</param-value>
    </context-param>
...
</web-app>
=================================
======log4j config file=================
log4j.rootCategory=debug, stdout, logFile
log4j.category.com.foo=WARN
#out to console
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=%-4r [%t] %-5p %c %x - %m%n
#out to file
log4j.appender.logFile=org.apache.log4j.RollingFileAppender
#tomcat uses bin as current directory when writing log
log4j.appender.logFile.File=../webapps/logtag.txt
log4j.appender.logFile.MaxFileSize=100KB
log4j.appender.logFile.MaxBackupIndex=1
# Pattern to output the caller's file name and line number.
log4j.appender.logFile.layout=org.apache.log4j.PatternLayout
log4j.appender.logFile.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
=================================


_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


Mime
View raw message