View Javadoc

1   package org.andromda.core.common;
2   
3   import java.net.URL;
4   
5   import org.apache.log4j.BasicConfigurator;
6   import org.apache.log4j.Logger;
7   import org.apache.log4j.xml.DOMConfigurator;
8   
9   /***
10   * This is the logger used to write <em>AndroMDA</em> prefixed messages so
11   * that our informational logging is nice looking.
12   * 
13   * @since 26.11.2003
14   * @author <a href="http://www.mbohlen.de">Matthias Bohlen </a>
15   * @author Chad Brandon
16   */
17  public class AndroMDALogger
18  {
19      private static final String DEFAULT_LOGGER_NAME = "AndroMDA";
20  
21      private static Logger logger = Logger.getLogger(DEFAULT_LOGGER_NAME);
22  
23      /***
24       * Configures logging for the AndroMDA application from the the xml resource
25       * "log4j.xml" found within the same package as this class.
26       */
27      public static void configure()
28      {
29          final String methodName = "StdoutLogger.configure";
30          String loggingConfiguration = "log4j.xml";
31          URL url = AndroMDALogger.class.getResource(loggingConfiguration);
32          if (url == null)
33          {
34              throw new RuntimeException(methodName
35                  + " - could not find Logger configuration file '"
36                  + loggingConfiguration + "'");
37          }
38          configure(url);
39      }
40  
41      /***
42       * Configures the Logger from the passed in logConfigurationXml
43       * 
44       * @param logConfigurationXml
45       */
46      protected static void configure(URL logConfigurationXml)
47      {
48          try
49          {
50              DOMConfigurator.configure(logConfigurationXml);
51          }
52          catch (Exception ex)
53          {
54              System.err
55                  .println("Unable to initialize logging system with configuration file '"
56                      + logConfigurationXml + "' --> using basic configuration.");
57              ex.printStackTrace();
58              BasicConfigurator.configure();
59          }
60      }
61  
62      /***
63       * Allows us to add a suffix to the logger name.
64       * 
65       * @param name
66       */
67      public static void setSuffix(String suffix)
68      {
69          logger = Logger.getLogger(DEFAULT_LOGGER_NAME + ':' + suffix);
70      }
71  
72      /***
73       * Resets the logger to the default name.
74       */
75      public static void reset()
76      {
77          logger = Logger.getLogger(DEFAULT_LOGGER_NAME);
78      }
79  
80      public static void debug(Object object)
81      {
82          logger.debug(object);
83      }
84  
85      public static void info(Object object)
86      {
87          logger.info(object);
88      }
89  
90      public static void warn(Object object)
91      {
92          logger.warn(object);
93      }
94  
95      public static void error(Object object)
96      {
97          logger.error(object);
98      }
99  }