X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.logging%2Fsrc%2Forg%2Fsimantics%2Flogging%2FLogConfigurator.java;fp=bundles%2Forg.simantics.logging%2Fsrc%2Forg%2Fsimantics%2Flogging%2FLogConfigurator.java;h=047bac8b61cd03f6b18b8317929cebaa0c3a3753;hb=7d5a5691780ed8373e77641b4a08f18cba0b9fab;hp=0000000000000000000000000000000000000000;hpb=33b349001037197a526a49e5820f0317dc74d934;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.logging/src/org/simantics/logging/LogConfigurator.java b/bundles/org.simantics.logging/src/org/simantics/logging/LogConfigurator.java new file mode 100644 index 000000000..047bac8b6 --- /dev/null +++ b/bundles/org.simantics.logging/src/org/simantics/logging/LogConfigurator.java @@ -0,0 +1,54 @@ +package org.simantics.logging; + +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.LoggerContext; + +/** + * Class for modifying the active logging configuration + * + * @author Jani Simomaa + * + */ +public final class LogConfigurator { + + private static final Logger LOGGER = LoggerFactory.getLogger(LogConfigurator.class); + + private LogConfigurator() { + } + + /** + * Sets logging level to represent the given argument + * + * @param level ERROR WARN INFO DEBUG TRACE + */ + public static void setLoggingLevel(String level) { + if (LOGGER.isInfoEnabled()) + LOGGER.info("Setting logger level to {}", level); + LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); + Level ll = getLoggerLevel(level); + List loggerList = context.getLoggerList(); + loggerList.forEach(l -> l.setLevel(ll)); + if (LOGGER.isDebugEnabled()) + LOGGER.debug("Loggers installed {}", loggerList); + } + + public static void setLoggingLevelForLogger(String logger, String level) { + if (LOGGER.isInfoEnabled()) + LOGGER.info("Setting logger level to {} for loggers {}", level, logger); + LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); + Level ll = getLoggerLevel(level); + ch.qos.logback.classic.Logger loggerList = context.getLogger(logger); + loggerList.setLevel(ll); + if (LOGGER.isDebugEnabled()) + LOGGER.debug("Loggers installed {}", loggerList); + } + + private static Level getLoggerLevel(String level) { + return Level.valueOf(level); + } +}