forked from qos-ch/slf4j
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleLoggerConfiguration.java
executable file
·184 lines (155 loc) · 7.08 KB
/
SimpleLoggerConfiguration.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package org.slf4j.simple;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Properties;
import org.slf4j.helpers.Util;
import org.slf4j.simple.OutputChoice.OutputChoiceType;
/**
* This class holds configuration values for {@link SimpleLogger}. The
* values are computed at runtime. See {@link SimpleLogger} documentation for
* more information.
*
*
* @author Ceki Gülcü
* @author Scott Sanders
* @author Rod Waldhoff
* @author Robert Burrell Donkin
* @author Cédrik LIME
*
* @since 1.7.25
*/
public class SimpleLoggerConfiguration {
private static final String CONFIGURATION_FILE = "simplelogger.properties";
static int DEFAULT_LOG_LEVEL_DEFAULT = SimpleLogger.LOG_LEVEL_INFO;
int defaultLogLevel = DEFAULT_LOG_LEVEL_DEFAULT;
private static final boolean SHOW_DATE_TIME_DEFAULT = false;
boolean showDateTime = SHOW_DATE_TIME_DEFAULT;
private static final String DATE_TIME_FORMAT_STR_DEFAULT = null;
private static String dateTimeFormatStr = DATE_TIME_FORMAT_STR_DEFAULT;
DateFormat dateFormatter = null;
private static final boolean SHOW_THREAD_NAME_DEFAULT = true;
boolean showThreadName = SHOW_THREAD_NAME_DEFAULT;
final static boolean SHOW_LOG_NAME_DEFAULT = true;
boolean showLogName = SHOW_LOG_NAME_DEFAULT;
private static final boolean SHOW_SHORT_LOG_NAME_DEFAULT = false;
boolean showShortLogName = SHOW_SHORT_LOG_NAME_DEFAULT;
private static final boolean LEVEL_IN_BRACKETS_DEFAULT = false;
boolean levelInBrackets = LEVEL_IN_BRACKETS_DEFAULT;
private static final String LOG_FILE_DEFAULT = "System.err";
private String logFile = LOG_FILE_DEFAULT;
OutputChoice outputChoice = null;
private static final boolean CACHE_OUTPUT_STREAM_DEFAULT = false;
private boolean cacheOutputStream = CACHE_OUTPUT_STREAM_DEFAULT;
private static final String WARN_LEVELS_STRING_DEFAULT = "WARN";
String warnLevelString = WARN_LEVELS_STRING_DEFAULT;
private final Properties properties = new Properties();
void init() {
loadProperties();
String defaultLogLevelString = getStringProperty(SimpleLogger.DEFAULT_LOG_LEVEL_KEY, null);
if (defaultLogLevelString != null)
defaultLogLevel = stringToLevel(defaultLogLevelString);
showLogName = getBooleanProperty(SimpleLogger.SHOW_LOG_NAME_KEY, SimpleLoggerConfiguration.SHOW_LOG_NAME_DEFAULT);
showShortLogName = getBooleanProperty(SimpleLogger.SHOW_SHORT_LOG_NAME_KEY, SHOW_SHORT_LOG_NAME_DEFAULT);
showDateTime = getBooleanProperty(SimpleLogger.SHOW_DATE_TIME_KEY, SHOW_DATE_TIME_DEFAULT);
showThreadName = getBooleanProperty(SimpleLogger.SHOW_THREAD_NAME_KEY, SHOW_THREAD_NAME_DEFAULT);
dateTimeFormatStr = getStringProperty(SimpleLogger.DATE_TIME_FORMAT_KEY, DATE_TIME_FORMAT_STR_DEFAULT);
levelInBrackets = getBooleanProperty(SimpleLogger.LEVEL_IN_BRACKETS_KEY, LEVEL_IN_BRACKETS_DEFAULT);
warnLevelString = getStringProperty(SimpleLogger.WARN_LEVEL_STRING_KEY, WARN_LEVELS_STRING_DEFAULT);
logFile = getStringProperty(SimpleLogger.LOG_FILE_KEY, logFile);
cacheOutputStream = getBooleanProperty(SimpleLogger.CACHE_OUTPUT_STREAM_STRING_KEY, CACHE_OUTPUT_STREAM_DEFAULT);
outputChoice = computeOutputChoice(logFile, cacheOutputStream);
if (dateTimeFormatStr != null) {
try {
dateFormatter = new SimpleDateFormat(dateTimeFormatStr);
} catch (IllegalArgumentException e) {
Util.report("Bad date format in " + CONFIGURATION_FILE + "; will output relative time", e);
}
}
}
private void loadProperties() {
// Add props from the resource simplelogger.properties
InputStream in = AccessController.doPrivileged((PrivilegedAction<InputStream>) () -> {
ClassLoader threadCL = Thread.currentThread().getContextClassLoader();
if (threadCL != null) {
return threadCL.getResourceAsStream(CONFIGURATION_FILE);
} else {
return ClassLoader.getSystemResourceAsStream(CONFIGURATION_FILE);
}
});
if (null != in) {
try {
properties.load(in);
} catch (java.io.IOException e) {
// ignored
} finally {
try {
in.close();
} catch (java.io.IOException e) {
// ignored
}
}
}
}
String getStringProperty(String name, String defaultValue) {
String prop = getStringProperty(name);
return (prop == null) ? defaultValue : prop;
}
boolean getBooleanProperty(String name, boolean defaultValue) {
String prop = getStringProperty(name);
return (prop == null) ? defaultValue : "true".equalsIgnoreCase(prop);
}
String getStringProperty(String name) {
String prop = null;
try {
prop = System.getProperty(name);
} catch (SecurityException e) {
; // Ignore
}
return (prop == null) ? properties.getProperty(name) : prop;
}
static int stringToLevel(String levelStr) {
if ("trace".equalsIgnoreCase(levelStr)) {
return SimpleLogger.LOG_LEVEL_TRACE;
} else if ("debug".equalsIgnoreCase(levelStr)) {
return SimpleLogger.LOG_LEVEL_DEBUG;
} else if ("info".equalsIgnoreCase(levelStr)) {
return SimpleLogger.LOG_LEVEL_INFO;
} else if ("warn".equalsIgnoreCase(levelStr)) {
return SimpleLogger.LOG_LEVEL_WARN;
} else if ("error".equalsIgnoreCase(levelStr)) {
return SimpleLogger.LOG_LEVEL_ERROR;
} else if ("off".equalsIgnoreCase(levelStr)) {
return SimpleLogger.LOG_LEVEL_OFF;
}
// assume INFO by default
return SimpleLogger.LOG_LEVEL_INFO;
}
private static OutputChoice computeOutputChoice(String logFile, boolean cacheOutputStream) {
if ("System.err".equalsIgnoreCase(logFile))
if (cacheOutputStream)
return new OutputChoice(OutputChoiceType.CACHED_SYS_ERR);
else
return new OutputChoice(OutputChoiceType.SYS_ERR);
else if ("System.out".equalsIgnoreCase(logFile)) {
if (cacheOutputStream)
return new OutputChoice(OutputChoiceType.CACHED_SYS_OUT);
else
return new OutputChoice(OutputChoiceType.SYS_OUT);
} else {
try {
FileOutputStream fos = new FileOutputStream(logFile);
PrintStream printStream = new PrintStream(fos);
return new OutputChoice(printStream);
} catch (FileNotFoundException e) {
Util.report("Could not open [" + logFile + "]. Defaulting to System.err", e);
return new OutputChoice(OutputChoiceType.SYS_ERR);
}
}
}
}