View Javadoc
1   package org.fax4j.common;
2   
3   import java.util.Map;
4   import org.fax4j.util.LibraryConfigurationLoader;
5   import org.fax4j.util.ReflectionHelper;
6   
7   /**
8    * Manages the internal fax4j logger.
9    *
10   * @author Sagie Gur-Ari
11   * @version 1.04
12   * @since 0.40.6
13   */
14  public final class LoggerManager {
15      /** The fax4j logger */
16      private Logger logger;
17      /** The global instance */
18      private static final LoggerManagererManager">LoggerManager INSTANCE = new LoggerManager();
19      /** The logger class name property key */
20      public static final String LOGGER_CLASS_NAME_PROPERTY_KEY = "org.fax4j.logger.class.name";
21      /** The logger log level property key */
22      public static final String LOGGER_LOG_LEVEL_PROPERTY_KEY = "org.fax4j.logger.log.level";
23  
24      /**
25       * This is the class constructor.
26       */
27      private LoggerManager() {
28          super();
29  
30          // initialize logger
31          this.initializeLogger();
32      }
33  
34      /**
35       * This function returns the global instance.
36       *
37       * @return The global instance
38       */
39      public static LoggerManager getInstance() {
40          return LoggerManager.INSTANCE;
41      }
42  
43      /**
44       * This function initializes the fax4j global logger.
45       */
46      private void initializeLogger() {
47          // get system configuration
48          Map<String, String> systemConfiguration = LibraryConfigurationLoader.getSystemConfiguration();
49  
50          // create new logger
51          this.createLogger(systemConfiguration);
52      }
53  
54      /**
55       * This function returns the fax4j internal logger.
56       *
57       * @return The fax4j internal logger
58       */
59      public Logger getLogger() {
60          return this.logger;
61      }
62  
63      /**
64       * This function creates the logger used by the fax4j framework.<br>
65       * The logger must extend org.fax4j.util.Logger and it's class name is defined by the org.fax4j.logger.class.name
66       * property in the fax4j configuration.<br>
67       * The org.fax4j.logger.log.level property is used to set the intial log level of the logger.
68       *
69       * @param systemConfiguration
70       *            The system configuration
71       */
72      private void createLogger(Map<String, String> systemConfiguration) {
73          // get class name
74          String className = systemConfiguration.get(LoggerManager.LOGGER_CLASS_NAME_PROPERTY_KEY);
75          if (className == null || className.length() == 0) {
76              className = SimpleLogger.class.getName();
77          }
78  
79          // create new instance
80          Logger/org/fax4j/common/Logger.html#Logger">Logger loggerInstance = (Logger) ReflectionHelper.createInstance(className);
81  
82          // set default log level as NONE
83          loggerInstance.setLogLevel(LogLevel.NONE);
84  
85          // get log level
86          String logLevelName = systemConfiguration.get(LoggerManager.LOGGER_LOG_LEVEL_PROPERTY_KEY);
87  
88          // set log level (ignore invalid log level)
89          if (logLevelName != null) {
90              if (logLevelName.equalsIgnoreCase(LogLevel.ERROR.getName())) {
91                  loggerInstance.setLogLevel(LogLevel.ERROR);
92              } else if (logLevelName.equalsIgnoreCase(LogLevel.INFO.getName())) {
93                  loggerInstance.setLogLevel(LogLevel.INFO);
94              } else if (logLevelName.equalsIgnoreCase(LogLevel.DEBUG.getName())) {
95                  loggerInstance.setLogLevel(LogLevel.DEBUG);
96              }
97          }
98  
99          // get reference
100         this.logger = loggerInstance;
101     }
102 }