Java log4j logging priority issue


The log4j level class has the following constructor:

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

My question is: when is String levelStr parameter used?
I defined my own custom application, and in it I 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 application, in the append method append 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 log output, 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 at my user level (i.e. main ALERT ).
So what am I doing something wrong? Should I see ALERT in the output log?
If not, when is leveStr used?

+4
source share
2 answers

You initialize the level, but you do not override the other methods of the Level class. Therefore, when Logger calls CustomeLevel.toLevel (String) to get the level name, it sets it to "FATAL"

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

+1
source

I am sure this is due to this:

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

You initialize the level with the value FATAL_INT. The library then assumes that your user level should be a built-in FATAL level. I would supply, for example. Instead, DEBUG_INT + 1.

0
source

Source: https://habr.com/ru/post/1344461/


All Articles