1 package org.andromda.core.common;
2
3 import java.io.InputStream;
4 import java.net.URL;
5 import org.andromda.core.configuration.Namespaces;
6 import org.apache.commons.lang.StringUtils;
7 import org.apache.log4j.BasicConfigurator;
8 import org.apache.log4j.LogManager;
9 import org.apache.log4j.Logger;
10 import org.apache.log4j.xml.DOMConfigurator;
11
12
13
14
15
16
17
18
19
20
21 public class AndroMDALogger
22 {
23
24
25
26 private static final String DEFAULT_LOGGER_NAME = "AndroMDA";
27 private static Logger logger = Logger.getLogger(DEFAULT_LOGGER_NAME);
28
29
30
31
32
33 public static void initialize()
34 {
35 String defaultConfiguration = "log4j.xml";
36 URL url = null;
37 final String configuration = loggingConfigurationUri;
38 if (StringUtils.isNotBlank(configuration))
39 {
40 try
41 {
42 url = new URL(configuration);
43 InputStream stream = url.openStream();
44 stream.close();
45
46 configure(url);
47 logger.info("Logging configured from external source --> '" + configuration + '\'');
48 }
49 catch (Throwable throwable)
50 {
51 url = AndroMDALogger.class.getResource(defaultConfiguration);
52 configure(url);
53 logger.warn("Invalid logging configuration uri --> '" + configuration + '\'');
54 }
55 }
56 if (url == null)
57 {
58 url = AndroMDALogger.class.getResource(defaultConfiguration);
59 configure(url);
60 }
61 if (url == null)
62 {
63 throw new RuntimeException(
64 "Could not find default logging configuration file '" + defaultConfiguration + '\'');
65 }
66 }
67
68
69
70
71 private static String loggingConfigurationUri = null;
72
73
74
75
76
77
78 public static void setLoggingConfigurationUri(final String loggingConfigurationUri)
79 {
80 AndroMDALogger.loggingConfigurationUri = loggingConfigurationUri;
81 }
82
83
84
85
86
87
88 protected static void configure(final URL logConfigurationXml)
89 {
90 try
91 {
92 DOMConfigurator.configure(logConfigurationXml);
93 }
94 catch (Exception ex)
95 {
96
97 System.err.println(
98 "Unable to initialize logging system " + "with configuration file '" + logConfigurationXml +
99 "' --> using basic configuration.");
100 BasicConfigurator.configure();
101 }
102 }
103
104
105
106
107
108
109
110 public static Logger getNamespaceLogger(final String namespaceName)
111 {
112 Logger logger;
113 if (namespaceName != null && !Namespaces.DEFAULT.equals(namespaceName))
114 {
115 logger = Logger.getLogger(getNamespaceLoggerName(namespaceName));
116 }
117 else
118 {
119 logger = Logger.getRootLogger();
120 }
121 return logger;
122 }
123
124
125
126
127
128
129
130 public static String getNamespaceLoggerName(final String namespace)
131 {
132 return "org.andromda.namespaces." + namespace;
133 }
134
135
136
137
138
139
140
141 public static String getNamespaceLogFileName(final String namespace)
142 {
143 return "andromda-" + namespace + ".log";
144 }
145
146
147
148
149
150
151 public static void setSuffix(final String suffix)
152 {
153 logger = getSuffixLogger(suffix);
154 }
155
156
157
158
159
160
161 public static Logger getSuffixLogger(final String suffix)
162 {
163 return Logger.getLogger(DEFAULT_LOGGER_NAME + ':' + suffix);
164 }
165
166
167
168
169
170
171 public static boolean isDebugEnabled()
172 {
173 return logger.isDebugEnabled();
174 }
175
176
177
178
179 public static void reset()
180 {
181 logger = Logger.getLogger(DEFAULT_LOGGER_NAME);
182 }
183
184
185
186
187 public static void shutdown()
188 {
189 LogManager.shutdown();
190 }
191
192
193
194
195 public static void debug(Object object)
196 {
197 logger.debug(object);
198 }
199
200
201
202
203 public static void info(Object object)
204 {
205 logger.info(object);
206 }
207
208
209
210
211 public static void warn(Object object)
212 {
213 logger.warn(object);
214 }
215
216
217
218
219 public static void error(Object object)
220 {
221 logger.error(object);
222 }
223 }