java log4j logging priority question
prote开发者_如何学运维cted Level(int level, String levelStr,int syslogEquivalent)
My question is, when is the String levelStr
parameter used?
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 (i.e. main ALERT
).
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 e.g. DEBUG_INT + 1 instead.
精彩评论