Differences between revisions 4 and 5
Revision 4 as of 2009-09-20 23:51:06
Size: 3100
Editor: localhost
Comment: converted to 1.6 markup
Revision 5 as of 2011-12-14 10:56:44
Size: 3145
Editor: static-58-108-220-236
Deletions are marked like this. Additions are marked like this.
Line 12: Line 12:

Here's an example of how to create a  Level :

public class TraceLevel extends Level {
  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 TraceLevel TRACE = 
      new TraceLevel(TRACE_INT, "TRACE", SYSLOG_TRACE_INT); 
  final static public Level ALL = new TraceLevel(ALL_INT, "ALL", 9); 

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

  public static Priority toPriority(String sArg) { 
    return toPriority(sArg, TraceLevel.TRACE); 

  public static Priority toPriority(int i) throws IllegalArgumentException { 
    return toPriority(i, TraceLevel.TRACE); 

  public static Priority toPriority(int i, Priority priority) { 
    Priority p = (i == TRACE_INT) ? 
        TraceLevel.TRACE : 
        Priority.toPriority(i, priority); 

    return p; 

  public static Priority toPriority(String sArg, Priority priority) { 
    Priority p = (sArg == null) ? 
        TraceLevel.TRACE : 
        ((sArg.equalsIgnoreCase("TRACE")) ? 
        TraceLevel.TRACE : 
        Priority.toPriority(sArg, priority)); 

    return p; 

  public static Level toLevel(String s) { 
    return toLevel(s, TraceLevel.TRACE); 

  public static Level toLevel(int i) { 
    return toLevel(i, TraceLevel.TRACE); 

  public static Level toLevel(int i, Level level) { 
    Level l = (i == TRACE_INT) ? 
        TraceLevel.TRACE : 
        Level.toLevel(i, level); 

    return l; 

  public static Level toLevel(String s, Level level) { 
    Level l = (s == null) ? 
        TraceLevel.TRACE : 
        ((s.equalsIgnoreCase("TRACE")) ? 
        TraceLevel.TRACE : 
        Level.toLevel(s, level)); 

    return l; 

   * @deprecated 
  public static Priority[] getAllPossiblePriorities() { 
    return new Priority[]{Priority.FATAL, Priority.ERROR, Level.WARN, 
                          Priority.INFO, Priority.DEBUG, TRACE}; 

The value of TRACE_INT affects what log level messages appear at. For example, setting TRACE_INT to LogLevel.DEBUG_INT +1 will make it equivalent to the DEBUG level statements. Setting a value of LogLevel.FATAL_INT + 10000 will set the level above any default level and allow you to only log those messages.

The way it would be used is:

if (LOG.isEnabledFor(TraceLevel.TRACE)) LOG.log(TraceLevel.TRACE, "Got here"); 

To get it to show in your logs, of course you need to tell your Logger to use the TRACE level and above. Because it's not a predefined Level, you have to specify the class name. Here's an example of how to set the root Logger in your log4j.properties file:

log4j.rootLogger=trace#my.package.TraceLevel, MAIN, STDERR 


Starting from version 1.2.12 of Log4J the support for a trace Level has been added and there is no need to add this as a custom Level.

TraceLevel (last edited 2011-12-14 10:56:44 by static-58-108-220-236)