Initialize logs with slf4j root settings if no root level has been set

This commit is contained in:
Andrea Cavalli 2021-06-09 01:49:58 +02:00
parent 99c410c21c
commit 84cf90947e
1 changed files with 32 additions and 0 deletions

View File

@ -1,13 +1,37 @@
package org.warp.commonutils.log; package org.warp.commonutils.log;
import org.slf4j.Logger;
public class Logs { public class Logs {
private static volatile boolean initialized;
private static boolean traceEnabled; private static boolean traceEnabled;
private static boolean debugEnabled; private static boolean debugEnabled;
private static boolean infoEnabled; private static boolean infoEnabled;
private static boolean warnEnabled; private static boolean warnEnabled;
private static Level rootLevel; 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) { public static void setRootLevel(Level level) {
rootLevel = level; rootLevel = level;
switch (level) { switch (level) {
@ -42,29 +66,37 @@ public class Logs {
warnEnabled = true; warnEnabled = true;
break; break;
} }
initialized = true;
} }
public static Level getRootLevel() { public static Level getRootLevel() {
if (!initialized) initialize();
return rootLevel; return rootLevel;
} }
public static boolean isTraceEnabled() { public static boolean isTraceEnabled() {
if (!initialized) initialize();
return traceEnabled; return traceEnabled;
} }
public static boolean isDebugEnabled() { public static boolean isDebugEnabled() {
if (!initialized) initialize();
return debugEnabled; return debugEnabled;
} }
public static boolean isInfoEnabled() { public static boolean isInfoEnabled() {
if (!initialized) initialize();
return infoEnabled; return infoEnabled;
} }
public static boolean isWarnEnabled() { public static boolean isWarnEnabled() {
if (!initialized) initialize();
return warnEnabled; return warnEnabled;
} }
public static boolean isErrorEnabled() { public static boolean isErrorEnabled() {
if (!initialized) initialize();
return true; return true;
} }
} }