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      final static boolean SHOW_LOG_NAME_DEFAULT = true;
49      boolean showLogName = SHOW_LOG_NAME_DEFAULT;
50  
51      private static final boolean SHOW_SHORT_LOG_NAME_DEFAULT = false;
52      boolean showShortLogName = SHOW_SHORT_LOG_NAME_DEFAULT;
53  
54      private static final boolean LEVEL_IN_BRACKETS_DEFAULT = false;
55      boolean levelInBrackets = LEVEL_IN_BRACKETS_DEFAULT;
56  
57      private static String LOG_FILE_DEFAULT = "System.err";
58      private String logFile = LOG_FILE_DEFAULT;
59      OutputChoice outputChoice = null;
60  
61      private static final boolean CACHE_OUTPUT_STREAM_DEFAULT = false;
62      private boolean cacheOutputStream = CACHE_OUTPUT_STREAM_DEFAULT;
63  
64      private static final String WARN_LEVELS_STRING_DEFAULT = "WARN";
65      String warnLevelString = WARN_LEVELS_STRING_DEFAULT;
66  
67      private final Properties properties = new Properties();
68  
69      void init() {
70          loadProperties();
71  
72          String defaultLogLevelString = getStringProperty(SimpleLogger.DEFAULT_LOG_LEVEL_KEY, null);
73          if (defaultLogLevelString != null)
74              defaultLogLevel = stringToLevel(defaultLogLevelString);
75  
76          showLogName = getBooleanProperty(SimpleLogger.SHOW_LOG_NAME_KEY, SimpleLoggerConfiguration.SHOW_LOG_NAME_DEFAULT);
77          showShortLogName = getBooleanProperty(SimpleLogger.SHOW_SHORT_LOG_NAME_KEY, SHOW_SHORT_LOG_NAME_DEFAULT);
78          showDateTime = getBooleanProperty(SimpleLogger.SHOW_DATE_TIME_KEY, SHOW_DATE_TIME_DEFAULT);
79          showThreadName = getBooleanProperty(SimpleLogger.SHOW_THREAD_NAME_KEY, SHOW_THREAD_NAME_DEFAULT);
80          dateTimeFormatStr = getStringProperty(SimpleLogger.DATE_TIME_FORMAT_KEY, DATE_TIME_FORMAT_STR_DEFAULT);
81          levelInBrackets = getBooleanProperty(SimpleLogger.LEVEL_IN_BRACKETS_KEY, LEVEL_IN_BRACKETS_DEFAULT);
82          warnLevelString = getStringProperty(SimpleLogger.WARN_LEVEL_STRING_KEY, WARN_LEVELS_STRING_DEFAULT);
83  
84          logFile = getStringProperty(SimpleLogger.LOG_FILE_KEY, logFile);
85  
86          cacheOutputStream = getBooleanProperty(SimpleLogger.CACHE_OUTPUT_STREAM_STRING_KEY, CACHE_OUTPUT_STREAM_DEFAULT);
87          outputChoice = computeOutputChoice(logFile, cacheOutputStream);
88  
89          if (dateTimeFormatStr != null) {
90              try {
91                  dateFormatter = new SimpleDateFormat(dateTimeFormatStr);
92              } catch (IllegalArgumentException e) {
93                  Util.report("Bad date format in " + CONFIGURATION_FILE + "; will output relative time", e);
94              }
95          }
96      }
97  
98      private void loadProperties() {
99          // Add props from the resource simplelogger.properties
100         InputStream in = AccessController.doPrivileged(new PrivilegedAction<InputStream>() {
101             public InputStream run() {
102                 ClassLoader threadCL = Thread.currentThread().getContextClassLoader();
103                 if (threadCL != null) {
104                     return threadCL.getResourceAsStream(CONFIGURATION_FILE);
105                 } else {
106                     return ClassLoader.getSystemResourceAsStream(CONFIGURATION_FILE);
107                 }
108             }
109         });
110         if (null != in) {
111             try {
112                 properties.load(in);
113             } catch (java.io.IOException e) {
114                 // ignored
115             } finally {
116                 try {
117                     in.close();
118                 } catch (java.io.IOException e) {
119                     // ignored
120                 }
121             }
122         }
123     }
124 
125     String getStringProperty(String name, String defaultValue) {
126         String prop = getStringProperty(name);
127         return (prop == null) ? defaultValue : prop;
128     }
129 
130     boolean getBooleanProperty(String name, boolean defaultValue) {
131         String prop = getStringProperty(name);
132         return (prop == null) ? defaultValue : "true".equalsIgnoreCase(prop);
133     }
134 
135     String getStringProperty(String name) {
136         String prop = null;
137         try {
138             prop = System.getProperty(name);
139         } catch (SecurityException e) {
140             ; // Ignore
141         }
142         return (prop == null) ? properties.getProperty(name) : prop;
143     }
144 
145     static int stringToLevel(String levelStr) {
146         if ("trace".equalsIgnoreCase(levelStr)) {
147             return SimpleLogger.LOG_LEVEL_TRACE;
148         } else if ("debug".equalsIgnoreCase(levelStr)) {
149             return SimpleLogger.LOG_LEVEL_DEBUG;
150         } else if ("info".equalsIgnoreCase(levelStr)) {
151             return SimpleLogger.LOG_LEVEL_INFO;
152         } else if ("warn".equalsIgnoreCase(levelStr)) {
153             return SimpleLogger.LOG_LEVEL_WARN;
154         } else if ("error".equalsIgnoreCase(levelStr)) {
155             return SimpleLogger.LOG_LEVEL_ERROR;
156         } else if ("off".equalsIgnoreCase(levelStr)) {
157             return SimpleLogger.LOG_LEVEL_OFF;
158         }
159         // assume INFO by default
160         return SimpleLogger.LOG_LEVEL_INFO;
161     }
162 
163     private static OutputChoice computeOutputChoice(String logFile, boolean cacheOutputStream) {
164         if ("System.err".equalsIgnoreCase(logFile))
165             if (cacheOutputStream)
166                 return new OutputChoice(OutputChoiceType.CACHED_SYS_ERR);
167             else
168                 return new OutputChoice(OutputChoiceType.SYS_ERR);
169         else if ("System.out".equalsIgnoreCase(logFile)) {
170             if (cacheOutputStream)
171                 return new OutputChoice(OutputChoiceType.CACHED_SYS_OUT);
172             else
173                 return new OutputChoice(OutputChoiceType.SYS_OUT);
174         } else {
175             try {
176                 FileOutputStream fos = new FileOutputStream(logFile);
177                 PrintStream printStream = new PrintStream(fos);
178                 return new OutputChoice(printStream);
179             } catch (FileNotFoundException e) {
180                 Util.report("Could not open [" + logFile + "]. Defaulting to System.err", e);
181                 return new OutputChoice(OutputChoiceType.SYS_ERR);
182             }
183         }
184     }
185 
186 }