You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Jim Moore <ji...@veritas.com> on 2001/01/25 01:40:57 UTC

New Priority

Because I want to be able to have a TRACE Priority, I went ahead and
followed the example code and created the following classes:

public class MyCategory extends Category {
  private static final MyCategoryFactory factory = new MyCategoryFactory();
  private static final String instanceFQCN = MyCategory.class.getName();
  public MyCategory(String name) {
    super(name);
  }
  public static Category getInstance(String name) {
    return Category.getInstance(name, factory); 
  }
  // this burned me -- it's needed, but not in the example
  public static Category getInstance(Class cls) {
    return Category.getInstance(cls.getName(), factory);
  }
  public void trace(String message) {
    if (isTraceEnabled())
      callAppenders(new LoggingEvent(instanceFQCN, this, MyPriority.TRACE,
                                     message, null));
  }
  public boolean isTraceEnabled() {
    return (disable >=  MyPriority.TRACE_INT) ?
      false : MyPriority.TRACE.isGreaterOrEqual(this.getChainedPriority());
  }
  private static class MyCategoryFactory implements CategoryFactory {
    MyCategoryFactory() {
    }
    public Category makeNewCategoryInstance(String name) {
      return new MyCategory(name);
    }
  }
}
public class MyPriority extends Priority {
  static final int  TRACE_INT  = 800;
  private static final int SYSLOG_TRACE_INT  = 7;
  public static final MyPriority TRACE =
    new MyPriority(TRACE_INT, "TRACE", SYSLOG_TRACE_INT);
  protected MyPriority(int level, String strLevel, int syslogEquiv) {
    super(level, strLevel, syslogEquiv);
  }
  public static Priority toPriority(String sArg) {
    if (sArg == null)
      return MyPriority.TRACE;
    return (sArg.equalsIgnoreCase("TRACE")) ?
      MyPriority.TRACE : Priority.toPriority(sArg);
  }
  public static Priority toPriority(int i) throws IllegalArgumentException {
    return (i == TRACE_INT) ?
      MyPriority.TRACE : Priority.toPriority(i);
  }
}

When, from withing my code, I do something like:
  protected static final MyCategory logger =
MyCategory.getInstance(MyClass.class);
  static { logger.setPriority(MyPriority.TRACE); }

... everything works peachy.

If, however, I remove the hard-coded setPriority and add the following to my
properties file, it barfs:
  log4j.category.MyClass=TRACE, MAIN

Even if I do:
  log4j.category.MyClass=DEBUG, MAIN

... it still doesn't work.  What am I missing so that I can set things from
my configuration file instead of hard-coding it?  Thanks!

-Jim Moore