View Javadoc
1   package org.slf4j.impl;
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.Util;
14  import org.slf4j.impl.OutputChoice.OutputChoiceType;
15  
16  /**
17   * This class holds configuration values for {@link SimpleLogger}. The
18   * values are computed at runtime. See {@link SimpleLogger} documentation for
19   * more information.
20   * 
21   * 
22   * @author Ceki Gülcü
23   * @author Scott Sanders
24   * @author Rod Waldhoff
25   * @author Robert Burrell Donkin
26   * @author Cédrik LIME
27   * 
28   * @since 1.7.25
29   */
30  public class SimpleLoggerConfiguration {
31  
32      private static final String CONFIGURATION_FILE = "simplelogger.properties";
33  
34      static int DEFAULT_LOG_LEVEL_DEFAULT = SimpleLogger.LOG_LEVEL_INFO;
35      int defaultLogLevel = DEFAULT_LOG_LEVEL_DEFAULT;
36  
37      private static final boolean SHOW_DATE_TIME_DEFAULT = false;
38      boolean showDateTime = SHOW_DATE_TIME_DEFAULT;
39  
40      private static final String DATE_TIME_FORMAT_STR_DEFAULT = null;
41      private static String dateTimeFormatStr = DATE_TIME_FORMAT_STR_DEFAULT;
42  
43      DateFormat dateFormatter = null;
44  
45      private static final boolean SHOW_THREAD_NAME_DEFAULT = true;
46      boolean showThreadName = SHOW_THREAD_NAME_DEFAULT;
47  
48      /**
49       * See https://jira.qos.ch/browse/SLF4J-499
50       * @since 1.7.33 and 2.0.0-alpha6
51       */
52      private static final boolean SHOW_THREAD_ID_DEFAULT = false;
53      boolean showThreadId = SHOW_THREAD_ID_DEFAULT;
54      
55      final static boolean SHOW_LOG_NAME_DEFAULT = true;
56      boolean showLogName = SHOW_LOG_NAME_DEFAULT;
57  
58      private static final boolean SHOW_SHORT_LOG_NAME_DEFAULT = false;
59      boolean showShortLogName = SHOW_SHORT_LOG_NAME_DEFAULT;
60  
61      private static final boolean LEVEL_IN_BRACKETS_DEFAULT = false;
62      boolean levelInBrackets = LEVEL_IN_BRACKETS_DEFAULT;
63  
64      private static String LOG_FILE_DEFAULT = "System.err";
65      private String logFile = LOG_FILE_DEFAULT;
66      OutputChoice outputChoice = null;
67  
68      private static final boolean CACHE_OUTPUT_STREAM_DEFAULT = false;
69      private boolean cacheOutputStream = CACHE_OUTPUT_STREAM_DEFAULT;
70  
71      private static final String WARN_LEVELS_STRING_DEFAULT = "WARN";
72      String warnLevelString = WARN_LEVELS_STRING_DEFAULT;
73  
74      private final Properties properties = new Properties();
75  
76      void init() {
77          loadProperties();
78  
79          String defaultLogLevelString = getStringProperty(SimpleLogger.DEFAULT_LOG_LEVEL_KEY, null);
80          if (defaultLogLevelString != null)
81              defaultLogLevel = stringToLevel(defaultLogLevelString);
82  
83          showLogName = getBooleanProperty(SimpleLogger.SHOW_LOG_NAME_KEY, SimpleLoggerConfiguration.SHOW_LOG_NAME_DEFAULT);
84          showShortLogName = getBooleanProperty(SimpleLogger.SHOW_SHORT_LOG_NAME_KEY, SHOW_SHORT_LOG_NAME_DEFAULT);
85          showDateTime = getBooleanProperty(SimpleLogger.SHOW_DATE_TIME_KEY, SHOW_DATE_TIME_DEFAULT);
86          showThreadName = getBooleanProperty(SimpleLogger.SHOW_THREAD_NAME_KEY, SHOW_THREAD_NAME_DEFAULT);
87          showThreadId = getBooleanProperty(SimpleLogger.SHOW_THREAD_ID_KEY, SHOW_THREAD_ID_DEFAULT);
88  
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                 Util.report("Bad date format in " + CONFIGURATION_FILE + "; will output relative time", e);
103             }
104         }
105     }
106 
107     private void loadProperties() {
108         // Add props from the resource simplelogger.properties
109         InputStream in = AccessController.doPrivileged(new PrivilegedAction<InputStream>() {
110             public InputStream run() {
111                 ClassLoader threadCL = Thread.currentThread().getContextClassLoader();
112                 if (threadCL != null) {
113                     return threadCL.getResourceAsStream(CONFIGURATION_FILE);
114                 } else {
115                     return ClassLoader.getSystemResourceAsStream(CONFIGURATION_FILE);
116                 }
117             }
118         });
119         if (null != in) {
120             try {
121                 properties.load(in);
122             } catch (java.io.IOException e) {
123                 // ignored
124             } finally {
125                 try {
126                     in.close();
127                 } catch (java.io.IOException e) {
128                     // ignored
129                 }
130             }
131         }
132     }
133 
134     String getStringProperty(String name, String defaultValue) {
135         String prop = getStringProperty(name);
136         return (prop == null) ? defaultValue : prop;
137     }
138 
139     boolean getBooleanProperty(String name, boolean defaultValue) {
140         String prop = getStringProperty(name);
141         return (prop == null) ? defaultValue : "true".equalsIgnoreCase(prop);
142     }
143 
144     String getStringProperty(String name) {
145         String prop = null;
146         try {
147             prop = System.getProperty(name);
148         } catch (SecurityException e) {
149             ; // Ignore
150         }
151         return (prop == null) ? properties.getProperty(name) : prop;
152     }
153 
154     static int stringToLevel(String levelStr) {
155         if ("trace".equalsIgnoreCase(levelStr)) {
156             return SimpleLogger.LOG_LEVEL_TRACE;
157         } else if ("debug".equalsIgnoreCase(levelStr)) {
158             return SimpleLogger.LOG_LEVEL_DEBUG;
159         } else if ("info".equalsIgnoreCase(levelStr)) {
160             return SimpleLogger.LOG_LEVEL_INFO;
161         } else if ("warn".equalsIgnoreCase(levelStr)) {
162             return SimpleLogger.LOG_LEVEL_WARN;
163         } else if ("error".equalsIgnoreCase(levelStr)) {
164             return SimpleLogger.LOG_LEVEL_ERROR;
165         } else if ("off".equalsIgnoreCase(levelStr)) {
166             return SimpleLogger.LOG_LEVEL_OFF;
167         }
168         // assume INFO by default
169         return SimpleLogger.LOG_LEVEL_INFO;
170     }
171 
172     private static OutputChoice computeOutputChoice(String logFile, boolean cacheOutputStream) {
173         if ("System.err".equalsIgnoreCase(logFile))
174             if (cacheOutputStream)
175                 return new OutputChoice(OutputChoiceType.CACHED_SYS_ERR);
176             else
177                 return new OutputChoice(OutputChoiceType.SYS_ERR);
178         else if ("System.out".equalsIgnoreCase(logFile)) {
179             if (cacheOutputStream)
180                 return new OutputChoice(OutputChoiceType.CACHED_SYS_OUT);
181             else
182                 return new OutputChoice(OutputChoiceType.SYS_OUT);
183         } else {
184             try {
185                 FileOutputStream fos = new FileOutputStream(logFile);
186                 PrintStream printStream = new PrintStream(fos);
187                 return new OutputChoice(printStream);
188             } catch (FileNotFoundException e) {
189                 Util.report("Could not open [" + logFile + "]. Defaulting to System.err", e);
190                 return new OutputChoice(OutputChoiceType.SYS_ERR);
191             }
192         }
193     }
194 
195 }