Using the following code by passing the desired log level the root log level can be set on run time.
private void setLogLevelWithParameter(String logLevel) {
Logger root = Logger.getRootLogger();
if ("DEBUG".equalsIgnoreCase(logLevel)) {
root.setLevel(Level.DEBUG);
} else if ("INFO".equalsIgnoreCase(logLevel)) {
root.setLevel(Level.INFO);
} else if ("WARN".equalsIgnoreCase(logLevel)) {
root.setLevel(Level.WARN);
} else if ("ERROR".equalsIgnoreCase(logLevel)) {
root.setLevel(Level.ERROR);
} else if ("FATAL".equalsIgnoreCase(logLevel)) {
root.setLevel(Level.FATAL);
}
}
In case of web application the desired loglevel can be fetched from the UI. and the same can be passed to the above method which does the work for you.
The standard log4j levels and their details are as follows.
TRACE Level
This log4j level gives more detailed information than the DEBUG level and sits top of the hierarchy. This level is introduced from version 1.2.12 in log4j.
DEBUG Level
This log4j level helps developer to debug application. Level of message logged will be focused on providing support to a application developer.
INFO Level
This log4j level gives the progress and chosen state information. This level will be generally useful for end user. This level is one level higher than DEBUG.
WARN Level
This log4j level gives a warning about an unexpected event to the user. The messages coming out of this level may not halt the progress of the system.
ERROR Level
This log4j level gives information about a serious error which needs to be addressed and may result in unstable state. This level is one level higher than WARN.
FATAL Level
This log4j level is straightforward and you don’t get it quite often. Once you get this level and it indicates application death.
All Level
This log4j level is used to turn on all levels of logging. Once this is configured and the levels are not considered.
OFF Level
This log4j level is opposite to ALL level. It turns off all the logging.Log Level Matrix
This matrix will help you understand the levels in a better way.
When a logger is created, generally you assign a level. The logger outputs all those messages equal to that level and also all greater levels than it. So the order for the standard log4j levels are:
ROOT Level\Display Level
| TRACE Level
| DEBUG Level
| INFO Level
| WARN Level
| ERROR Level
| FATAL Level
|
TRACE Level
|
Y
|
Y
|
Y
| Y
|
Y
|
Y
|
DEBUG Level
|
N
|
Y
|
Y
|
Y
|
Y
|
Y
|
INFO Level
|
N
|
N
|
Y
|
Y
|
Y
|
Y
|
WARN Level
|
N
|
N
|
N
|
Y
|
Y
|
Y
|
ERROR Level
|
N
|
N
|
N
|
N
|
Y
|
Y
|
FATAL Level
|
N
|
N
|
N
|
N
|
N
|
Y
|
ALL Level
|
Y
|
Y
|
Y
|
Y
|
Y
|
Y
|
OFF Level
|
N
|
N
|
N
|
N
|
N
|
N
|
No comments:
Post a Comment