logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jim Moore <jim.mo...@veritas.com>
Subject RE: Extending Priority below DEBUG
Date Wed, 13 Feb 2002 18:21:02 GMT
import org.apache.log4j.Priority;


/**
 * This class introduces a new priority level called TRACE. TRACE has
 *   lower priority than DEBUG.
 */
public class TraceablePriority extends Priority {
  public static final int  TRACE_INT  = 800;

  // We assimilate TRACE to DEBUG on Syslog
  private static final int SYSLOG_TRACE_INT  = 7;

  public static final TraceablePriority TRACE =
    new TraceablePriority(TRACE_INT, "TRACE", SYSLOG_TRACE_INT);


  protected TraceablePriority(int level, String strLevel, int syslogEquiv) {
    super(level, strLevel, syslogEquiv);
  }


  public static Priority toPriority(String sArg) {
    if (sArg == null)
      return TraceablePriority.TRACE;

    return (sArg.equalsIgnoreCase("TRACE")) ?
      TraceablePriority.TRACE :
      Priority.toPriority(sArg);
  }


  public static Priority toPriority(int i) throws IllegalArgumentException {
    return (i == TRACE_INT) ?
      TraceablePriority.TRACE :
      Priority.toPriority(i);
  }

}

import org.apache.log4j.*;
import org.apache.log4j.spi.*;
import org.apache.log4j.helpers.LogLog;

/**
 * Provides support for the {@link TraceablePriority.TRACE} priority.
 */
public class TraceableCategory extends Category {
  private static final Factory _factory = new Factory();

  private static final String instanceFQCN =
TraceableCategory.class.getName();


  /**
   * Sets up a default Appender to STDERR if none already exists
   */
  static {
    Category root = Category.getRoot();
    java.util.Enumeration enum = root.getAllAppenders();
    if (!enum.hasMoreElements()) {
      Layout layout = new PatternLayout("%-4r %c{1} [%t] %-5p %x - %m%n");
      ConsoleAppender appender = new ConsoleAppender(layout);
      root.addAppender(appender);
    }
  }


  /**
   * Just calls the parent constuctor.
   * 
   * @param name
   */
  public TraceableCategory(String name) {
    super(name);
  }


  /**
   * This method overrides {@link Category#getInstance(String)} by supplying
   *   its own factory type as a parameter.
   * 
   * @param name
   * 
   * @return 
   */
  public static Category getInstance(String name) {
    return getInstance(name, (_factory == null ? new Factory() : _factory));
  }


  /**
   * This method overrides {@link Category#getInstance(String)} by supplying
   *   its own factory type as a parameter.
   * 
   * @param name
   * 
   * @return 
   */
  public static Category getInstance(String name, CategoryFactory f) {
    return Category.getInstance(name, f);
  }


  /**
   * This method overrides {@link Category#getInstance(String)} by supplying
   *   its own factory type as a parameter.
   * 
   * @param name
   *
   * @return 
   */
  public static Category getInstance(Class cls) {
    return getInstance(cls.getName(), (_factory == null ? new Factory() :
_factory));
  }


  /**
   * This method overrides {@link Category#getInstance(String)} by supplying
   *   its own factory type as a parameter.
   * 
   * @param name
   *
   * @return 
   */
  public static Category getInstance(Class cls, CategoryFactory f) {
    return Category.getInstance(cls.getName(), f);
  }


  /**
   * Set the priority of this Category.
   * 
   * <p>Null values are admitted.
   * 
   * @param priority
   */
  public void setPriority(Priority priority) {
    this.priority = priority;
  }


  /**
   * Log a message object with the {@link TraceablePriority#TRACE TRACE}
   *   priority.<p>
   * 
   * This method first checks if this category is <code>TRACE</code>
   *   enabled by comparing the priority of this category with the {@link
   *   TraceablePriority#TRACE TRACE} priority. If this category is
   *   <code>TRACE</code> enabled, then it proceeds to call all the
   *   registered appenders in this category and also higher in the
   *   hierarchy depending on the value of the additivity flag.
   * 
   * @param message  the message object to log.
   */
  public void trace(String message) {
    if (isTraceEnabled())
      callAppenders(new LoggingEvent(instanceFQCN, this,
TraceablePriority.TRACE,
                                     message, null));
  }


  /**
   * Log a message object with the {@link TraceablePriority#TRACE TRACE}
   *   priority.<p>
   * 
   * This method first checks if this category is <code>TRACE</code>
   *   enabled by comparing the priority of this category with the {@link
   *   TraceablePriority#TRACE TRACE} priority. If this category is
   *   <code>TRACE</code> enabled, then it proceeds to call all the
   *   registered appenders in this category and also higher in the
   *   hierarchy depending on the value of the additivity flag.
   * 
   * @param message  the message object to log.
   * 
   * @param exp      the exception to log
   */
  public void trace(String message, Throwable exp) {
    if (isTraceEnabled())
      callAppenders(new LoggingEvent(instanceFQCN, this,
TraceablePriority.TRACE,
                                     message, exp));
  }


  /**
   * Check whether this category is enabled for the <code>TRACE</code>
   * priority.<p>
   * 
   * This function is intended to lessen the computational cost of
   * disabled log trace statements.<p>
   * 
   * For some <code>cat</code> Category object, when you write,
   * <pre>
   *   cat.trace("This is entry number: " + i );
   * </pre>
   * 
   * You incur the cost constructing the message, concatenatiion in
   * this case, regardless of whether the message is logged or not.<p>
   * 
   * If you are worried about speed, then you should write
   * <pre>
   *   if(cat.isTraceEnabled()) {
   *     cat.trace("This is entry number: " + i );
   *   }
   * </pre>
   * 
   * This way you will not incur the cost of parameter construction
   * if tracing is disabled for <code>cat</code>. On the other hand,
   * if the <code>cat</code> is trace enabled, you will incur the cost
   * of evaluating whether the category is trace enabled twice. Once
   * in <code>isTraceEnabled</code> and once in the <code>trace</code>.
   * This is an insignificant overhead since evaluating a category
   * takes about 1% of the time it takes to actually log.
   * 
   * @return boolean - <code>true</code> if this category is trace
   *         enabled, <code>false</code> otherwise.
   */
  public boolean isTraceEnabled() {
    return (hierarchy.isDisabled(TraceablePriority.TRACE_INT)) ?
      false :
      TraceablePriority.TRACE.isGreaterOrEqual(this.getChainedPriority());
  }


  public String toString() {
    return
    ast.common.util.StringUtilities.getNameOfClass(this.getClass())+
      "("+this.getName()+")";
  }


  // Any sub-class of Category must also have its own implementation of 
  // CategoryFactory.
  public static class Factory implements CategoryFactory {
    public Factory() {
    }

    public Category makeNewCategoryInstance(String name) {
      return new TraceableCategory(name);
    }
  }

}


Usage in code is:
  private static final TraceableCategory LOG =
      (TraceableCategory)TraceableCategory.getInstance(MyClass.class);


Usage in log4j.xml is:
  <!-- Note the class attributes! -->
  <category name="ast.titan.awt.TreePopup"
            class="ast.common.log.TraceableCategory">
    <priority value ="trace"
              class="ast.common.log.TraceablePriority" />
  </category>


-----Original Message-----
From: Mark Douglas [mailto:Mark_Douglas@systemsunion.com] 
Sent: Wednesday, February 13, 2002 11:49 AM
To: 'Log4J Developers List'
Subject: RE: Extending Priority below DEBUG


This was a major headache when I first started using Log4j, seems it's still
a headache.

Can anyone think of an easy way of adding new Priorities AND getting them to
work correctly without modifying log4j itself?

Sorry, but I'm a bit cheesed-off at the moment (not with log4j though).

Mark Douglas

-----Original Message-----
From: P Coyote [mailto:pcoyote99@yahoo.com]
Sent: 13 February 2002 16:53
To: Log4J Developers List
Subject: Extending Priority below DEBUG


We're extending the existing priority levels by adding one below DEBUG.
Since the ROOT category cannot be below DEBUG, our new lower priority is not
being honored without modifying PropertyConfigurator.parseCategory(). Is
there any way to do what we need without having to modify log4j code?

Thanks for your help in advance,

Peter

--
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