1 package org.andromda.core.common;
2
3 import java.io.InputStream;
4
5 import java.net.URL;
6
7 import org.andromda.core.configuration.Namespaces;
8 import org.apache.commons.lang.StringUtils;
9 import org.apache.log4j.BasicConfigurator;
10 import org.apache.log4j.LogManager;
11 import org.apache.log4j.Logger;
12 import org.apache.log4j.xml.DOMConfigurator;
13
14
15 /***
16 * This is the logger used to write <em>AndroMDA</em> prefixed messages so that our informational logging is nice
17 * looking.
18 *
19 * @author <a href="http://www.mbohlen.de">Matthias Bohlen </a>
20 * @author Chad Brandon
21 * @since 26.11.2003
22 */
23 public class AndroMDALogger
24 {
25 /***
26 * The default name to give the logger (if one isn't set)
27 */
28 private static final String DEFAULT_LOGGER_NAME = "AndroMDA";
29 private static Logger logger = Logger.getLogger(DEFAULT_LOGGER_NAME);
30
31 /***
32 * Configures logging for the AndroMDA application from the the xml resource "log4j.xml" found within the same
33 * package as this class.
34 */
35 public static void initialize()
36 {
37 String defaultConfiguration = "log4j.xml";
38 URL url = null;
39 final String configuration = loggingConfigurationUri;
40 if (StringUtils.isNotEmpty(configuration))
41 {
42 try
43 {
44 url = new URL(configuration);
45 InputStream stream = url.openStream();
46 stream.close();
47 stream = null;
48 configure(url);
49 logger.info("Logging configured from external source --> '" + configuration + "'");
50 }
51 catch (Throwable throwable)
52 {
53 url = AndroMDALogger.class.getResource(defaultConfiguration);
54 configure(url);
55 logger.warn("Invalid logging configuration uri --> '" + configuration + "'");
56 }
57 }
58 if (url == null)
59 {
60 url = AndroMDALogger.class.getResource(defaultConfiguration);
61 configure(url);
62 }
63 if (url == null)
64 {
65 throw new RuntimeException(
66 "Could not find default logging configuration file '" + defaultConfiguration + "'");
67 }
68 }
69
70 /***
71 * The URI to an external logging configuration file.
72 */
73 private static String loggingConfigurationUri = null;
74
75 /***
76 * Sets the URI to an external logging configuration file. This will override the default log4j.xml.
77 *
78 * @param loggingConfigurationUri the URI to the logging configuraiton file.
79 */
80 public static void setLoggingConfigurationUri(final String loggingConfigurationUri)
81 {
82 AndroMDALogger.loggingConfigurationUri = loggingConfigurationUri;
83 }
84
85 /***
86 * Configures the Logger from the passed in logConfigurationXml
87 *
88 * @param logConfigurationXml
89 */
90 protected static void configure(final URL logConfigurationXml)
91 {
92 try
93 {
94 DOMConfigurator.configure(logConfigurationXml);
95 }
96 catch (Exception ex)
97 {
98 System.err.println(
99 "Unable to initialize logging system " + "with configuration file '" + logConfigurationXml +
100 "' --> using basic configuration.");
101 BasicConfigurator.configure();
102 }
103 }
104
105 /***
106 * Retrieves the namespace logger (if one is available) otherwise returns the root logger.
107 *
108 * @param namespaceName the name of the namespace for which we'll retrieve the logger instance.
109 * @return the namespace or root logger instance.
110 */
111 public static Logger getNamespaceLogger(final String namespaceName)
112 {
113 Logger logger;
114 if (namespaceName != null && !Namespaces.DEFAULT.equals(namespaceName))
115 {
116 logger = Logger.getLogger(getNamespaceLoggerName(namespaceName));
117 }
118 else
119 {
120 logger = Logger.getRootLogger();
121 }
122 return logger;
123 }
124
125 /***
126 * Gets the name of the logger.
127 *
128 * @param namespace the name of the namespace for which this logger is used.
129 * @return the logger name.
130 */
131 public static String getNamespaceLoggerName(final String namespace)
132 {
133 return "org.andromda.namespaces." + namespace;
134 }
135
136 /***
137 * Gets the name of the file to which namespace logging output will be written.
138 *
139 * @param namespace the name of the namespace for which this logger is used.
140 * @return the namespace logging file name.
141 */
142 public static String getNamespaceLogFileName(final String namespace)
143 {
144 return "andromda-" + namespace + ".log";
145 }
146
147 /***
148 * Allows us to add a suffix to the logger name.
149 *
150 * @param suffix the suffix to append to the logger name.
151 */
152 public static void setSuffix(final String suffix)
153 {
154 logger = Logger.getLogger(DEFAULT_LOGGER_NAME + ':' + suffix);
155 }
156
157 /***
158 * Resets the logger to the default name.
159 */
160 public static void reset()
161 {
162 logger = Logger.getLogger(DEFAULT_LOGGER_NAME);
163 }
164
165 /***
166 * Shuts down the logger and releases any resources.
167 */
168 public static void shutdown()
169 {
170 LogManager.shutdown();
171 }
172
173 public static void debug(Object object)
174 {
175 logger.debug(object);
176 }
177
178 public static void info(Object object)
179 {
180 logger.info(object);
181 }
182
183 public static void warn(Object object)
184 {
185 logger.warn(object);
186 }
187
188 public static void error(Object object)
189 {
190 logger.error(object);
191 }
192 }