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