1 package org.slf4j.simple;
2
3 import java.io.FileNotFoundException;
4 import java.io.FileOutputStream;
5 import java.io.InputStream;
6 import java.io.PrintStream;
7 import java.security.AccessController;
8 import java.security.PrivilegedAction;
9 import java.text.DateFormat;
10 import java.text.SimpleDateFormat;
11 import java.util.Properties;
12
13 import org.slf4j.helpers.Reporter;
14 import org.slf4j.helpers.Util;
15 import org.slf4j.simple.OutputChoice.OutputChoiceType;
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 public class SimpleLoggerConfiguration {
32
33 private static final String CONFIGURATION_FILE = "simplelogger.properties";
34
35 static int DEFAULT_LOG_LEVEL_DEFAULT = SimpleLogger.LOG_LEVEL_INFO;
36 int defaultLogLevel = DEFAULT_LOG_LEVEL_DEFAULT;
37
38 private static final boolean SHOW_DATE_TIME_DEFAULT = false;
39 boolean showDateTime = SHOW_DATE_TIME_DEFAULT;
40
41 private static final String DATE_TIME_FORMAT_STR_DEFAULT = null;
42 private static String dateTimeFormatStr = DATE_TIME_FORMAT_STR_DEFAULT;
43
44 DateFormat dateFormatter = null;
45
46 private static final boolean SHOW_THREAD_NAME_DEFAULT = true;
47 boolean showThreadName = SHOW_THREAD_NAME_DEFAULT;
48
49
50
51
52
53 private static final boolean SHOW_THREAD_ID_DEFAULT = false;
54 boolean showThreadId = SHOW_THREAD_ID_DEFAULT;
55
56 final static boolean SHOW_LOG_NAME_DEFAULT = true;
57 boolean showLogName = SHOW_LOG_NAME_DEFAULT;
58
59 private static final boolean SHOW_SHORT_LOG_NAME_DEFAULT = false;
60 boolean showShortLogName = SHOW_SHORT_LOG_NAME_DEFAULT;
61
62 private static final boolean LEVEL_IN_BRACKETS_DEFAULT = false;
63 boolean levelInBrackets = LEVEL_IN_BRACKETS_DEFAULT;
64
65 private static final String LOG_FILE_DEFAULT = "System.err";
66 private String logFile = LOG_FILE_DEFAULT;
67 OutputChoice outputChoice = null;
68
69 private static final boolean CACHE_OUTPUT_STREAM_DEFAULT = false;
70 private boolean cacheOutputStream = CACHE_OUTPUT_STREAM_DEFAULT;
71
72 private static final String WARN_LEVELS_STRING_DEFAULT = "WARN";
73 String warnLevelString = WARN_LEVELS_STRING_DEFAULT;
74
75 private final Properties properties = new Properties();
76
77 void init() {
78 loadProperties();
79
80 String defaultLogLevelString = getStringProperty(SimpleLogger.DEFAULT_LOG_LEVEL_KEY, null);
81 if (defaultLogLevelString != null)
82 defaultLogLevel = stringToLevel(defaultLogLevelString);
83
84 showLogName = getBooleanProperty(SimpleLogger.SHOW_LOG_NAME_KEY, SimpleLoggerConfiguration.SHOW_LOG_NAME_DEFAULT);
85 showShortLogName = getBooleanProperty(SimpleLogger.SHOW_SHORT_LOG_NAME_KEY, SHOW_SHORT_LOG_NAME_DEFAULT);
86 showDateTime = getBooleanProperty(SimpleLogger.SHOW_DATE_TIME_KEY, SHOW_DATE_TIME_DEFAULT);
87 showThreadName = getBooleanProperty(SimpleLogger.SHOW_THREAD_NAME_KEY, SHOW_THREAD_NAME_DEFAULT);
88 showThreadId = getBooleanProperty(SimpleLogger.SHOW_THREAD_ID_KEY, SHOW_THREAD_ID_DEFAULT);
89 dateTimeFormatStr = getStringProperty(SimpleLogger.DATE_TIME_FORMAT_KEY, DATE_TIME_FORMAT_STR_DEFAULT);
90 levelInBrackets = getBooleanProperty(SimpleLogger.LEVEL_IN_BRACKETS_KEY, LEVEL_IN_BRACKETS_DEFAULT);
91 warnLevelString = getStringProperty(SimpleLogger.WARN_LEVEL_STRING_KEY, WARN_LEVELS_STRING_DEFAULT);
92
93 logFile = getStringProperty(SimpleLogger.LOG_FILE_KEY, logFile);
94
95 cacheOutputStream = getBooleanProperty(SimpleLogger.CACHE_OUTPUT_STREAM_STRING_KEY, CACHE_OUTPUT_STREAM_DEFAULT);
96 outputChoice = computeOutputChoice(logFile, cacheOutputStream);
97
98 if (dateTimeFormatStr != null) {
99 try {
100 dateFormatter = new SimpleDateFormat(dateTimeFormatStr);
101 } catch (IllegalArgumentException e) {
102 Reporter.error("Bad date format in " + CONFIGURATION_FILE + "; will output relative time", e);
103 }
104 }
105 }
106
107 private void loadProperties() {
108
109 InputStream in = AccessController.doPrivileged((PrivilegedAction<InputStream>) () -> {
110 ClassLoader threadCL = Thread.currentThread().getContextClassLoader();
111 if (threadCL != null) {
112 return threadCL.getResourceAsStream(CONFIGURATION_FILE);
113 } else {
114 return ClassLoader.getSystemResourceAsStream(CONFIGURATION_FILE);
115 }
116 });
117 if (null != in) {
118 try {
119 properties.load(in);
120 } catch (java.io.IOException e) {
121
122 } finally {
123 try {
124 in.close();
125 } catch (java.io.IOException e) {
126
127 }
128 }
129 }
130 }
131
132 String getStringProperty(String name, String defaultValue) {
133 String prop = getStringProperty(name);
134 return (prop == null) ? defaultValue : prop;
135 }
136
137 boolean getBooleanProperty(String name, boolean defaultValue) {
138 String prop = getStringProperty(name);
139 return (prop == null) ? defaultValue : "true".equalsIgnoreCase(prop);
140 }
141
142 String getStringProperty(String name) {
143 String prop = null;
144 try {
145 prop = System.getProperty(name);
146 } catch (SecurityException e) {
147 ;
148 }
149 return (prop == null) ? properties.getProperty(name) : prop;
150 }
151
152 static int stringToLevel(String levelStr) {
153 if ("trace".equalsIgnoreCase(levelStr)) {
154 return SimpleLogger.LOG_LEVEL_TRACE;
155 } else if ("debug".equalsIgnoreCase(levelStr)) {
156 return SimpleLogger.LOG_LEVEL_DEBUG;
157 } else if ("info".equalsIgnoreCase(levelStr)) {
158 return SimpleLogger.LOG_LEVEL_INFO;
159 } else if ("warn".equalsIgnoreCase(levelStr)) {
160 return SimpleLogger.LOG_LEVEL_WARN;
161 } else if ("error".equalsIgnoreCase(levelStr)) {
162 return SimpleLogger.LOG_LEVEL_ERROR;
163 } else if ("off".equalsIgnoreCase(levelStr)) {
164 return SimpleLogger.LOG_LEVEL_OFF;
165 }
166
167 return SimpleLogger.LOG_LEVEL_INFO;
168 }
169
170 private static OutputChoice computeOutputChoice(String logFile, boolean cacheOutputStream) {
171 if ("System.err".equalsIgnoreCase(logFile))
172 if (cacheOutputStream)
173 return new OutputChoice(OutputChoiceType.CACHED_SYS_ERR);
174 else
175 return new OutputChoice(OutputChoiceType.SYS_ERR);
176 else if ("System.out".equalsIgnoreCase(logFile)) {
177 if (cacheOutputStream)
178 return new OutputChoice(OutputChoiceType.CACHED_SYS_OUT);
179 else
180 return new OutputChoice(OutputChoiceType.SYS_OUT);
181 } else {
182 try {
183 FileOutputStream fos = new FileOutputStream(logFile);
184 PrintStream printStream = new PrintStream(fos);
185 return new OutputChoice(printStream);
186 } catch (FileNotFoundException e) {
187 Reporter.error("Could not open [" + logFile + "]. Defaulting to System.err", e);
188 return new OutputChoice(OutputChoiceType.SYS_ERR);
189 }
190 }
191 }
192
193 }