View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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.text.DateFormat;
26  import java.text.SimpleDateFormat;
27  import java.util.Properties;
28  
29  import org.apache.maven.slf4j.OutputChoice.OutputChoiceType;
30  import org.slf4j.helpers.Reporter;
31  
32  /**
33   * This class holds configuration values for {@link MavenBaseLogger}. The
34   * values are computed at runtime. See {@link MavenBaseLogger} documentation for
35   * more information.
36   *
37   *
38   * @author Ceki Gülcü
39   * @author Scott Sanders
40   * @author Rod Waldhoff
41   * @author Robert Burrell Donkin
42   * @author Cédrik LIME
43   *
44   * @since 1.7.25
45   */
46  public class SimpleLoggerConfiguration {
47  
48      private static final String CONFIGURATION_FILE = "simplelogger.properties";
49  
50      static final int DEFAULT_LOG_LEVEL_DEFAULT = MavenBaseLogger.LOG_LEVEL_INFO;
51      int defaultLogLevel = DEFAULT_LOG_LEVEL_DEFAULT;
52  
53      private static final boolean SHOW_DATE_TIME_DEFAULT = false;
54      boolean showDateTime = SHOW_DATE_TIME_DEFAULT;
55  
56      private static final String DATE_TIME_FORMAT_STR_DEFAULT = null;
57      private static String dateTimeFormatStr = DATE_TIME_FORMAT_STR_DEFAULT;
58  
59      DateFormat dateFormatter = null;
60  
61      private static final boolean SHOW_THREAD_NAME_DEFAULT = true;
62      boolean showThreadName = SHOW_THREAD_NAME_DEFAULT;
63  
64      /**
65       * See https://jira.qos.ch/browse/SLF4J-499
66       * @since 1.7.33 and 2.0.0-alpha6
67       */
68      private static final boolean SHOW_THREAD_ID_DEFAULT = false;
69  
70      boolean showThreadId = SHOW_THREAD_ID_DEFAULT;
71  
72      static final boolean SHOW_LOG_NAME_DEFAULT = true;
73      boolean showLogName = SHOW_LOG_NAME_DEFAULT;
74  
75      private static final boolean SHOW_SHORT_LOG_NAME_DEFAULT = false;
76      boolean showShortLogName = SHOW_SHORT_LOG_NAME_DEFAULT;
77  
78      private static final boolean LEVEL_IN_BRACKETS_DEFAULT = false;
79      boolean levelInBrackets = LEVEL_IN_BRACKETS_DEFAULT;
80  
81      private static final String LOG_FILE_DEFAULT = "System.err";
82      private String logFile = LOG_FILE_DEFAULT;
83      OutputChoice outputChoice = null;
84  
85      private static final boolean CACHE_OUTPUT_STREAM_DEFAULT = false;
86      private boolean cacheOutputStream = CACHE_OUTPUT_STREAM_DEFAULT;
87  
88      private static final String WARN_LEVELS_STRING_DEFAULT = "WARN";
89      String warnLevelString = WARN_LEVELS_STRING_DEFAULT;
90  
91      private final Properties properties = new Properties();
92  
93      void init() {
94          loadProperties();
95  
96          String defaultLogLevelString = getStringProperty(MavenBaseLogger.DEFAULT_LOG_LEVEL_KEY, null);
97          if (defaultLogLevelString != null) {
98              defaultLogLevel = stringToLevel(defaultLogLevelString);
99          }
100 
101         showLogName =
102                 getBooleanProperty(MavenBaseLogger.SHOW_LOG_NAME_KEY, SimpleLoggerConfiguration.SHOW_LOG_NAME_DEFAULT);
103         showShortLogName = getBooleanProperty(MavenBaseLogger.SHOW_SHORT_LOG_NAME_KEY, SHOW_SHORT_LOG_NAME_DEFAULT);
104         showDateTime = getBooleanProperty(MavenBaseLogger.SHOW_DATE_TIME_KEY, SHOW_DATE_TIME_DEFAULT);
105         showThreadName = getBooleanProperty(MavenBaseLogger.SHOW_THREAD_NAME_KEY, SHOW_THREAD_NAME_DEFAULT);
106         showThreadId = getBooleanProperty(MavenBaseLogger.SHOW_THREAD_ID_KEY, SHOW_THREAD_ID_DEFAULT);
107         dateTimeFormatStr = getStringProperty(MavenBaseLogger.DATE_TIME_FORMAT_KEY, DATE_TIME_FORMAT_STR_DEFAULT);
108         levelInBrackets = getBooleanProperty(MavenBaseLogger.LEVEL_IN_BRACKETS_KEY, LEVEL_IN_BRACKETS_DEFAULT);
109         warnLevelString = getStringProperty(MavenBaseLogger.WARN_LEVEL_STRING_KEY, WARN_LEVELS_STRING_DEFAULT);
110 
111         logFile = getStringProperty(MavenBaseLogger.LOG_FILE_KEY, logFile);
112 
113         cacheOutputStream =
114                 getBooleanProperty(MavenBaseLogger.CACHE_OUTPUT_STREAM_STRING_KEY, CACHE_OUTPUT_STREAM_DEFAULT);
115         outputChoice = computeOutputChoice(logFile, cacheOutputStream);
116 
117         if (dateTimeFormatStr != null) {
118             try {
119                 dateFormatter = new SimpleDateFormat(dateTimeFormatStr);
120             } catch (IllegalArgumentException e) {
121                 Reporter.error("Bad date format in " + CONFIGURATION_FILE + "; will output relative time", e);
122             }
123         }
124     }
125 
126     private void loadProperties() {
127         // Add props from the resource simplelogger.properties
128         ClassLoader threadCL = Thread.currentThread().getContextClassLoader();
129         ClassLoader toUseCL = (threadCL != null ? threadCL : ClassLoader.getSystemClassLoader());
130         try (InputStream in = toUseCL.getResourceAsStream(CONFIGURATION_FILE)) {
131             if (in != null) {
132                 properties.load(in);
133             }
134         } catch (java.io.IOException e) {
135             // ignored
136         }
137     }
138 
139     String getStringProperty(String name, String defaultValue) {
140         String prop = getStringProperty(name);
141         return (prop == null) ? defaultValue : prop;
142     }
143 
144     boolean getBooleanProperty(String name, boolean defaultValue) {
145         String prop = getStringProperty(name);
146         return (prop == null) ? defaultValue : "true".equalsIgnoreCase(prop);
147     }
148 
149     String getStringProperty(String name) {
150         String prop = null;
151         try {
152             prop = System.getProperty(name);
153         } catch (SecurityException e) {
154             // Ignore
155         }
156         return (prop == null) ? properties.getProperty(name) : prop;
157     }
158 
159     static int stringToLevel(String levelStr) {
160         if ("trace".equalsIgnoreCase(levelStr)) {
161             return MavenBaseLogger.LOG_LEVEL_TRACE;
162         } else if ("debug".equalsIgnoreCase(levelStr)) {
163             return MavenBaseLogger.LOG_LEVEL_DEBUG;
164         } else if ("info".equalsIgnoreCase(levelStr)) {
165             return MavenBaseLogger.LOG_LEVEL_INFO;
166         } else if ("warn".equalsIgnoreCase(levelStr)) {
167             return MavenBaseLogger.LOG_LEVEL_WARN;
168         } else if ("error".equalsIgnoreCase(levelStr)) {
169             return MavenBaseLogger.LOG_LEVEL_ERROR;
170         } else if ("off".equalsIgnoreCase(levelStr)) {
171             return MavenBaseLogger.LOG_LEVEL_OFF;
172         }
173         // assume INFO by default
174         return MavenBaseLogger.LOG_LEVEL_INFO;
175     }
176 
177     private static OutputChoice computeOutputChoice(String logFile, boolean cacheOutputStream) {
178         if ("System.err".equalsIgnoreCase(logFile)) {
179             return new OutputChoice(cacheOutputStream ? OutputChoiceType.CACHED_SYS_ERR : OutputChoiceType.SYS_ERR);
180         } else if ("System.out".equalsIgnoreCase(logFile)) {
181             return new OutputChoice(cacheOutputStream ? OutputChoiceType.CACHED_SYS_OUT : OutputChoiceType.SYS_OUT);
182         } else {
183             try {
184                 FileOutputStream fos = new FileOutputStream(logFile);
185                 PrintStream printStream = new PrintStream(fos);
186                 return new OutputChoice(printStream);
187             } catch (FileNotFoundException e) {
188                 Reporter.error("Could not open [" + logFile + "]. Defaulting to System.err", e);
189                 return new OutputChoice(OutputChoiceType.SYS_ERR);
190             }
191         }
192     }
193 }