logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Leathers, Burton" <Burton.Leath...@Cognos.COM>
Subject Proposed addition to log4j
Date Mon, 29 Oct 2001 18:25:30 GMT
I have developed a class for use at Cognos and think it may be of value to
others. In essence, it constrains the type of the "object" which may be used
as the argument to the logging facility. We, at Cognos, wish to impose this
constraint in order to minimize the risk of a Babel of message types being
generated.

I would appreciate any suggestions for improvements in the class. I am still
sufficiently new to both Java and log4j that I am sure that my draft could
be improved.

When I have finished responding to suggestions for improvement, I will need
advice on the method for a formal submission.

Thanks in advance.

Burton

Burton Leathers
Software Architect
The end of the human race will be that it will eventually die of
civilization.  Ralph Waldo Emerson


Here is the text of the class

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

/**
 * Copyright (C) The Apache Software Foundation. All rights reserved.
 *
 * This software is published under the terms of the Apache Software
 * License version 1.1, a copy of which has been included with this
 * distribution in the LICENSE.APL file.  */

// Contibutors: Burton Leathers <burton.leathers@cognos.com>

package org.apache.log4j;

import org.apache.log4j.Category;
import org.apache.log4j.Priority;
import org.apache.log4j.spi.CategoryFactory;

public class TypedCategory extends Category {

  // This is the class of which arguments to logging methods must be an
instance
  private Class baseMessageType = null;

  // It's enough to instantiate a factory once and for all.
  private static TypedFactory factory = new TypedFactory();

  protected TypedCategory(String name) {
    super(name);
  }

  public void debug(Object p0) {
    if(this.baseMessageType.isInstance(p0)) {
      super.debug(p0);
    }
  }

  public void debug(Object p0, Throwable p1) {
    if(this.baseMessageType.isInstance(p0)) {
      super.debug(p0, p1);
    }
  }

  public void error(Object p0) {
    if(this.baseMessageType.isInstance(p0)) {
      super.error(p0);
    }
  }

  public void error(Object p0, Throwable p1) {
    if(this.baseMessageType.isInstance(p0)) {
      super.error(p0, p1);
    }
  }

  public void fatal(Object p0) {
    if(this.baseMessageType.isInstance(p0)) {
      super.fatal(p0);
    }
  }

  public void fatal(Object p0, Throwable p1) {
    if(this.baseMessageType.isInstance(p0)) {
      super.fatal(p0, p1);
    }
  }

  /*
   * This method overrides {@link Category#getInstance}
   * by supplying its own factory type as a parameter.
   */
  public static Category getInstance(String name, Class base) {
    TypedCategory t;
    Category c = Category.getInstance(name, factory);
    try {
      t = (TypedCategory) c;
    } catch (ClassCastException z) {
      return null;
    }
    if(t.baseMessageType == null) {
      t.baseMessageType = base;
    }
    return c;
  }

  /*
   * This method overrides {@link Category#getInstance(Class)}
   * by supplying its own factory type as a parameter.
   */
  public static Category getInstance(Class clazz, Class base) {
    TypedCategory t;
    Category c = Category.getInstance(clazz.getName(), factory);
    try {
      t = (TypedCategory) c;
    } catch (ClassCastException z) {
      return null;
    }
    if(t.baseMessageType == null) {
      t.baseMessageType = base;
    }
    return c;
  }

  public void info(Object p0) {
    if(this.baseMessageType.isInstance(p0)) {
      super.info(p0);
    }
  }

  public void info(Object p0, Throwable p1) {
    if(this.baseMessageType.isInstance(p0)) {
      super.info(p0, p1);
    }
  }

  public void log(Priority priority, Object p0) {
    if(this.baseMessageType.isInstance(p0)) {
      super.log(priority, p0);
    }
  }

  public void log(Priority priority, Object p0, Throwable p1) {
    if(this.baseMessageType.isInstance(p0)) {
      super.log(priority, p0, p1);
    }
  }

  public void warn(Object p0) {
    if(this.baseMessageType.isInstance(p0)) {
      super.warn(p0);
    }
  }

  public void warn(Object p0, Throwable p1) {
    if(this.baseMessageType.isInstance(p0)) {
      super.warn(p0, p1);
    }
  }


  // Any sub-class of Category must also have its own implementation of
  // CategoryFactory.
  public static class TypedFactory implements CategoryFactory {

    public TypedFactory() {
    }

    public Category makeNewCategoryInstance(String name) {
      TypedCategory t = new TypedCategory(name);
      t.baseMessageType = null;
      return t;
    }
  }
}

  
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

This message may contain privileged and/or confidential information.  If you
have received this e-mail in error or are not the intended recipient, you
may not use, copy, disseminate, or distribute it; do not open any
attachments, delete it immediately from your system and notify the sender by
e-mail promptly that you have done so.  Thank You.

--
To unsubscribe, e-mail:   <mailto:log4j-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:log4j-dev-help@jakarta.apache.org>


Mime
View raw message