Initialize logs with slf4j root settings if no root level has been set
This commit is contained in:
parent
99c410c21c
commit
84cf90947e
@ -1,13 +1,37 @@
|
||||
package org.warp.commonutils.log;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
|
||||
public class Logs {
|
||||
|
||||
private static volatile boolean initialized;
|
||||
|
||||
private static boolean traceEnabled;
|
||||
private static boolean debugEnabled;
|
||||
private static boolean infoEnabled;
|
||||
private static boolean warnEnabled;
|
||||
private static Level rootLevel;
|
||||
|
||||
private static void initialize() {
|
||||
var logger = org.slf4j.LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
|
||||
traceEnabled = logger.isTraceEnabled();
|
||||
debugEnabled = logger.isDebugEnabled();
|
||||
infoEnabled = logger.isInfoEnabled();
|
||||
warnEnabled = logger.isWarnEnabled();
|
||||
if (!warnEnabled) {
|
||||
rootLevel = Level.ERROR;
|
||||
} else if (!infoEnabled) {
|
||||
rootLevel = Level.WARN;
|
||||
} else if (!debugEnabled) {
|
||||
rootLevel = Level.INFO;
|
||||
} else if (!traceEnabled) {
|
||||
rootLevel = Level.DEBUG;
|
||||
} else {
|
||||
rootLevel = Level.TRACE;
|
||||
}
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
public static void setRootLevel(Level level) {
|
||||
rootLevel = level;
|
||||
switch (level) {
|
||||
@ -42,29 +66,37 @@ public class Logs {
|
||||
warnEnabled = true;
|
||||
break;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
public static Level getRootLevel() {
|
||||
if (!initialized) initialize();
|
||||
return rootLevel;
|
||||
}
|
||||
|
||||
public static boolean isTraceEnabled() {
|
||||
if (!initialized) initialize();
|
||||
return traceEnabled;
|
||||
}
|
||||
|
||||
public static boolean isDebugEnabled() {
|
||||
if (!initialized) initialize();
|
||||
return debugEnabled;
|
||||
}
|
||||
|
||||
public static boolean isInfoEnabled() {
|
||||
if (!initialized) initialize();
|
||||
return infoEnabled;
|
||||
}
|
||||
|
||||
public static boolean isWarnEnabled() {
|
||||
if (!initialized) initialize();
|
||||
return warnEnabled;
|
||||
}
|
||||
|
||||
public static boolean isErrorEnabled() {
|
||||
if (!initialized) initialize();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user