-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathLoggerConfiguration.java
73 lines (59 loc) · 2.14 KB
/
LoggerConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package org.utplsql.cli;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.ConsoleAppender;
import org.slf4j.LoggerFactory;
public class LoggerConfiguration {
public enum ConfigLevel {
BASIC, NONE, DEBUG
}
private LoggerConfiguration() {
throw new UnsupportedOperationException();
}
static void configure(ConfigLevel level) {
switch (level) {
case BASIC:
configureInfo();
break;
case NONE:
configureSilent();
break;
case DEBUG:
configureDebug();
break;
}
}
private static void configureSilent() {
setRootLoggerLevel(Level.OFF);
}
private static void configureInfo() {
setRootLoggerLevel(Level.INFO);
setSingleConsoleAppenderWithLayout("%msg%n");
}
private static void configureDebug() {
setRootLoggerLevel(Level.DEBUG);
setSingleConsoleAppenderWithLayout("%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n");
}
private static void setRootLoggerLevel(Level level) {
Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
root.setLevel(level);
}
private static void setSingleConsoleAppenderWithLayout(String patternLayout) {
Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
PatternLayoutEncoder ple = new PatternLayoutEncoder();
ple.setPattern(patternLayout);
ple.setContext(lc);
ple.start();
ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();
consoleAppender.setEncoder(ple);
consoleAppender.setContext(lc);
consoleAppender.start();
logger.detachAndStopAllAppenders();
logger.setAdditive(false);
logger.addAppender(consoleAppender);
}
}