001: package org.andromda.core.common;
002:
003: import java.io.InputStream;
004:
005: import java.net.URL;
006:
007: import org.andromda.core.configuration.Namespaces;
008: import org.apache.commons.lang.StringUtils;
009: import org.apache.log4j.BasicConfigurator;
010: import org.apache.log4j.LogManager;
011: import org.apache.log4j.Logger;
012: import org.apache.log4j.xml.DOMConfigurator;
013:
014: /**
015: * This is the logger used to write <em>AndroMDA</em> prefixed messages so that our informational logging is nice
016: * looking.
017: *
018: * @author <a href="http://www.mbohlen.de">Matthias Bohlen </a>
019: * @author Chad Brandon
020: * @since 26.11.2003
021: */
022: public class AndroMDALogger {
023: /**
024: * The default name to give the logger (if one isn't set)
025: */
026: private static final String DEFAULT_LOGGER_NAME = "AndroMDA";
027: private static Logger logger = Logger
028: .getLogger(DEFAULT_LOGGER_NAME);
029:
030: /**
031: * Configures logging for the AndroMDA application from the the xml resource "log4j.xml" found within the same
032: * package as this class.
033: */
034: public static void initialize() {
035: String defaultConfiguration = "log4j.xml";
036: URL url = null;
037: final String configuration = loggingConfigurationUri;
038: if (StringUtils.isNotEmpty(configuration)) {
039: try {
040: url = new URL(configuration);
041: InputStream stream = url.openStream();
042: stream.close();
043: stream = null;
044: configure(url);
045: logger
046: .info("Logging configured from external source --> '"
047: + configuration + "'");
048: } catch (Throwable throwable) {
049: url = AndroMDALogger.class
050: .getResource(defaultConfiguration);
051: configure(url);
052: logger.warn("Invalid logging configuration uri --> '"
053: + configuration + "'");
054: }
055: }
056: if (url == null) {
057: url = AndroMDALogger.class
058: .getResource(defaultConfiguration);
059: configure(url);
060: }
061: if (url == null) {
062: throw new RuntimeException(
063: "Could not find default logging configuration file '"
064: + defaultConfiguration + "'");
065: }
066: }
067:
068: /**
069: * The URI to an external logging configuration file.
070: */
071: private static String loggingConfigurationUri = null;
072:
073: /**
074: * Sets the URI to an external logging configuration file. This will override the default log4j.xml.
075: *
076: * @param loggingConfigurationUri the URI to the logging configuraiton file.
077: */
078: public static void setLoggingConfigurationUri(
079: final String loggingConfigurationUri) {
080: AndroMDALogger.loggingConfigurationUri = loggingConfigurationUri;
081: }
082:
083: /**
084: * Configures the Logger from the passed in logConfigurationXml
085: *
086: * @param logConfigurationXml
087: */
088: protected static void configure(final URL logConfigurationXml) {
089: try {
090: DOMConfigurator.configure(logConfigurationXml);
091: } catch (Exception ex) {
092: System.err.println("Unable to initialize logging system "
093: + "with configuration file '" + logConfigurationXml
094: + "' --> using basic configuration.");
095: BasicConfigurator.configure();
096: }
097: }
098:
099: /**
100: * Retrieves the namespace logger (if one is available) otherwise returns the root logger.
101: *
102: * @param namespaceName the name of the namespace for which we'll retrieve the logger instance.
103: * @return the namespace or root logger instance.
104: */
105: public static Logger getNamespaceLogger(final String namespaceName) {
106: Logger logger;
107: if (namespaceName != null
108: && !Namespaces.DEFAULT.equals(namespaceName)) {
109: logger = Logger
110: .getLogger(getNamespaceLoggerName(namespaceName));
111: } else {
112: logger = Logger.getRootLogger();
113: }
114: return logger;
115: }
116:
117: /**
118: * Gets the name of the logger.
119: *
120: * @param namespace the name of the namespace for which this logger is used.
121: * @return the logger name.
122: */
123: public static String getNamespaceLoggerName(final String namespace) {
124: return "org.andromda.namespaces." + namespace;
125: }
126:
127: /**
128: * Gets the name of the file to which namespace logging output will be written.
129: *
130: * @param namespace the name of the namespace for which this logger is used.
131: * @return the namespace logging file name.
132: */
133: public static String getNamespaceLogFileName(final String namespace) {
134: return "andromda-" + namespace + ".log";
135: }
136:
137: /**
138: * Allows us to add a suffix to the logger name.
139: *
140: * @param suffix the suffix to append to the logger name.
141: */
142: public static void setSuffix(final String suffix) {
143: logger = Logger.getLogger(DEFAULT_LOGGER_NAME + ':' + suffix);
144: }
145:
146: /**
147: * Resets the logger to the default name.
148: */
149: public static void reset() {
150: logger = Logger.getLogger(DEFAULT_LOGGER_NAME);
151: }
152:
153: /**
154: * Shuts down the logger and releases any resources.
155: */
156: public static void shutdown() {
157: LogManager.shutdown();
158: }
159:
160: public static void debug(Object object) {
161: logger.debug(object);
162: }
163:
164: public static void info(Object object) {
165: logger.info(object);
166: }
167:
168: public static void warn(Object object) {
169: logger.warn(object);
170: }
171:
172: public static void error(Object object) {
173: logger.error(object);
174: }
175: }
|