]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.logging/src/org/simantics/logging/LogConfigurator.java
Logging configuration via SCL and UI & saving to ZIP-archive
[simantics/platform.git] / bundles / org.simantics.logging / src / org / simantics / logging / LogConfigurator.java
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 (file)
index 0000000..047bac8
--- /dev/null
@@ -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<ch.qos.logback.classic.Logger> 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);
+    }
+}