From 84cf90947ea85d7a65c6c272ef362293a3d92815 Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Wed, 9 Jun 2021 01:49:58 +0200 Subject: [PATCH] Initialize logs with slf4j root settings if no root level has been set --- .../java/org/warp/commonutils/log/Logs.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/main/java/org/warp/commonutils/log/Logs.java b/src/main/java/org/warp/commonutils/log/Logs.java index 7a93959..b38cdb3 100644 --- a/src/main/java/org/warp/commonutils/log/Logs.java +++ b/src/main/java/org/warp/commonutils/log/Logs.java @@ -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; } }