]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.logging/src/org/simantics/logging/LogConfigurator.java
Add UI dialog for dynamic reconfiguration of Logback loggers
[simantics/platform.git] / bundles / org.simantics.logging / src / org / simantics / logging / LogConfigurator.java
1 package org.simantics.logging;
2
3 import java.util.List;
4 import java.util.Objects;
5 import java.util.stream.Collectors;
6
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9
10 import ch.qos.logback.classic.Level;
11 import ch.qos.logback.classic.LoggerContext;
12
13 /**
14  * Class for modifying the active logging configuration
15  * 
16  * @author Jani Simomaa
17  *
18  */
19 public final class LogConfigurator {
20
21     private static final Logger LOGGER = LoggerFactory.getLogger(LogConfigurator.class);
22
23     private LogConfigurator() {
24     }
25
26     /**
27      * Sets logging level to represent the given argument
28      * 
29      * @param level ERROR WARN INFO DEBUG TRACE
30      */
31     public static void setLoggingLevel(String level) {
32         if (LOGGER.isInfoEnabled())
33             LOGGER.info("Setting logger level to {}", level);
34         LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
35         Level ll = getLoggerLevel(level);
36         List<ch.qos.logback.classic.Logger> loggerList = context.getLoggerList();
37         loggerList.forEach(l -> l.setLevel(ll));
38         if (LOGGER.isDebugEnabled())
39             LOGGER.debug("Loggers installed {}", loggerList);
40     }
41
42     public static void setLoggingLevelForLogger(String logger, String level) {
43         if (LOGGER.isInfoEnabled())
44             LOGGER.info("Setting logger level to {} for loggers {}", level, logger);
45         LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
46         Level ll = getLoggerLevel(level);
47         ch.qos.logback.classic.Logger l = context.getLogger(logger);
48         l.setLevel(ll);
49         if (LOGGER.isDebugEnabled())
50             LOGGER.debug("Logger {} level set to {}", l, ll);
51     }
52
53     private static Level getLoggerLevel(String level) {
54         return Level.valueOf(level);
55     }
56
57     /**
58      * @since 1.47.0
59      */
60     public static List<LoggerLevel> listConfiguredLoggers() {
61         LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
62         return context.getLoggerList().stream()
63                 .map(l -> {
64                     //LOGGER.info("{} : {} : {} : {}", l, l.getName(), l.getLevel(), l.getEffectiveLevel());
65                     if (l.getName().isEmpty())
66                         return null;
67                     return new LoggerLevel(l, l.getEffectiveLevel().toString());
68                 })
69                 .filter(Objects::nonNull)
70                 .sorted()
71                 .collect(Collectors.toList());
72     }
73
74     /**
75      * @since 1.47.0
76      */
77     public static void applyLogLevels(List<LoggerLevel> loggers) {
78         loggers.forEach(l -> {
79             Level level = getLoggerLevel(l.getLevel());
80             Logger logger = l.getLogger();
81             if (logger instanceof ch.qos.logback.classic.Logger) {
82                 LOGGER.info("Setting existing logger {} level to {}", l.getName(), l.getLevel());
83                 ch.qos.logback.classic.Logger ll = (ch.qos.logback.classic.Logger) l.getLogger();
84                 ll.setLevel(level);
85             } else if (!l.getName().trim().isEmpty()) {
86                 LOGGER.info("Defining new logger {} with level {}", l.getName(), l.getLevel());
87                 logger = LoggerFactory.getLogger(l.getName());
88                 if (logger instanceof ch.qos.logback.classic.Logger) {
89                     ch.qos.logback.classic.Logger ll = (ch.qos.logback.classic.Logger) logger;
90                     ll.setLevel(level);
91                 }
92             }
93         });
94     }
95
96 }