简体   繁体   中英

java log4j logging priority question


The Level class of log4j has the following constructor:

protected Level(int level, String levelStr,int syslogEquivalent)

My question is, when is the String levelStr parameter used?
I have defined my own custom appender and in it I have defined a custom Level as follows:

public static class CustomLevel extends Level {
  private static final long serialVersionUID = 1L;
  public static final Level ALERT = new CustomLevel(FATAL_INT, "ALERT", 2);
  protected CustomLevel(int level, String levelStr, int syslogEquivalent) {
           super(level, levelStr, syslogEquivalent);
      }
 }

In my appender, in the append method I do:

Level newLevel = CustomLevel.ALERT; 
etc.

LoggingEvent newLoggingEvent = new LoggingEvent(
            event.getFQNOfLoggerClass(), event.getLogger(), event.getTimeStamp(), newLevel,
            event.getMessage(), event.getThreadName(), event.getThrowableInformation(),
            event.getNDC(), event.getLocationInformation(), event.getProperties()); 
super.append(newLoggingEvent);

In the output of the log, when I do custom_Logger.fatal(msg); I can see my message but the level is FATAL .

main FATAL logging.customlog - Send an Error Message to log

I thought it would be ALERT as defined in my custom level (ie main ALERT ).
So am I doing something wrong here? Shouldn't I see ALERT in the output log?
If not then when is the leveStr used?

You are initializing the level, but you are not overriding the rest of the methods in the Level class. So when the Logger calls CustomeLevel.toLevel(String) to get the name of the level it is setting it as "FATAL"

See an example at http://jaitechwriteups.blogspot.com/2006/07/create-your-own-logging-level-in-log4j.html

I'm pretty sure that it has to do with this:

public static final Level ALERT = new CustomLevel(FATAL_INT, "ALERT", 2);

You're initializing the level with the value of FATAL_INT. The library is then assuming that your custom level must be the built-in FATAL level. I would try supplying eg DEBUG_INT + 1 instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM