001: package org.jzonic.jlo;
002:
003: import java.util.HashMap;
004: import org.jzonic.jlo.handler.*;
005: import org.jzonic.jlo.formatter.*;
006: import org.jzonic.jlo.processor.LogProcessorFactory;
007: import org.jzonic.jlo.reader.*;
008: import org.jzonic.jlo.error.ErrorHandler;
009: import org.jzonic.jlo.events.*;
010: import org.apache.commons.logging.*;
011:
012: /**
013: * This class manages all LogConfigurations. It is the point of entry for jLo.
014: * There is only one instance of the LogManager to make sure that all classes
015: * in one JVM will work with the same configurations. <BR/>
016: * In order to get a Logger you have to call:
017: * <pre>
018: * private static final Logger logger = LogManager.getLogger("com.foo.app");
019: * </pre>
020: * In this case the LogManager when instanciated will search for a file
021: * called "jlo_logging.xml" in your classpath and process it.<BR/>
022: * This method is error save that means that it will always return a valid
023: * Logger. <BR/>
024: * Since jLo supports many LogConfigurations you can also use this method:
025: * <pre>
026: * private static final Logger logger = LogManager.getLogger("com.foo.app","foo");
027: * </pre>
028: * If you provide a configuration name then the LogManager will search for a
029: * file called "foo_logging.xml" in your classpath and process it.
030: *
031: *
032: *@author Andreas Mecky
033: *@author Terry Dye
034: *@created 29. Januar 2002
035: *@version 1.0
036: */
037: public class CommonsLogManager extends LogFactory implements
038: FileListener {
039:
040: private LogConfiguration defaultConfiguration;
041: private static CommonsLogManager lm = null;
042: private HashMap configurations;
043: private Logger defaultLogger;
044: private Channel defaultChannel;
045: private FileWatcher fileWatcher;
046:
047: public CommonsLogManager() {
048: configurations = new HashMap();
049: XMLFileReader reader = new XMLFileReader();
050: reader.setFileName("jlo_logging.xml");
051: readConfiguration(reader, "Default");
052: fileWatcher = new FileWatcher();
053: fileWatcher.addFileListener(this , "Default");
054: defaultConfiguration = new LogConfiguration("default");
055: defaultConfiguration.addLogger(getDefaultLogger());
056: defaultLogger = getDefaultLogger();
057: addDefaultChannel();
058: }
059:
060: /**
061: * This method returns the one and only instance of the LogManager.
062: *
063: * @return the instance of the LogManager
064: */
065: public static CommonsLogManager getInstance() {
066: if (lm == null) {
067: lm = new CommonsLogManager();
068: }
069: return lm;
070: }
071:
072: /**
073: * This method returns a Logger from the LogConfiguration.
074: * The name of the LogConfiguration is default and the corresponding
075: * file is jlo_logging.xml. This method uses the LogConfiguration.getLogger
076: * method.
077: *
078: * @param loggerName the name of the logger
079: * @return the Logger or the DefaultLogger if not found
080: */
081: public static Log getLog(String loggerName) {
082: return getLog(loggerName, "Default");
083: }
084:
085: /**
086: * This methods returns a logger for the given name from the specific
087: * logconfiguration. If a logger with the given name does not exist then
088: * the LogConfiguration will try to search for it by going up the hierarchy.
089: * If still no logger is found then this method will return the DefaultLogger.
090: * This method will never return null therefore it is save to do this:
091: * <pre>
092: * private static final Logger logger = LogManager.getLogger("org.foo.app");
093: * </pre>
094: * in your class. If there is no instance of the LogManager so far then
095: * a new one will be created.
096: *
097: * @param loggerName the name of the logger
098: * @param configurationName the name of the configuration
099: * @return the requested logger or the next one in the hierarchy or the default
100: */
101: public static Log getLog(String loggerName, String configurationName) {
102: if (lm == null) {
103: lm = new CommonsLogManager();
104: }
105: if (configurationName == null) {
106: Logger logger = lm.defaultConfiguration
107: .getLogger(loggerName);
108: if (logger == null) {
109: ErrorHandler.reportError("Logger " + loggerName
110: + " in " + configurationName
111: + " not found. Using default logger");
112: return lm.defaultLogger;
113: }
114: return logger;
115: } else {
116: Logger logger = lm.getLogConfiguration(configurationName)
117: .getLogger(loggerName);
118: if (logger == null) {
119: ErrorHandler.reportError("Logger " + loggerName
120: + " in " + configurationName
121: + " not found. Using default logger");
122: return lm.defaultLogger;
123: }
124: return logger;
125: }
126: }
127:
128: public LogConfiguration getLogConfiguration() {
129: return getLogConfiguration("Default");
130: }
131:
132: public LogConfiguration getLogConfiguration(String configurationName) {
133: if (configurations.containsKey(configurationName)) {
134: return (LogConfiguration) configurations
135: .get(configurationName);
136: } else {
137: XMLFileReader reader = new XMLFileReader();
138: reader.setFileName(configurationName + "_logging.xml");
139: try {
140: LogConfiguration lc = reader
141: .parseConfiguration(configurationName);
142: configurations.put(lc.getName(), lc);
143: fileWatcher.addFileListener(this , configurationName);
144: return lc;
145: } catch (ReaderException re) {
146: ErrorHandler.reportError("Could not find file:"
147: + configurationName
148: + "_logging.xml in the classpath");
149: return defaultConfiguration;
150: }
151: }
152: }
153:
154: /**
155: *
156: */
157: //TODO: do some error checking
158: //TODO: set up a filewatcher if it is not already running
159: public boolean readConfiguration(LogConfigurationReader reader,
160: String configurationName) {
161: try {
162: LogConfiguration lc = reader
163: .parseConfiguration(configurationName);
164: if (configurationName == null) {
165: defaultConfiguration = lc;
166: } else {
167: configurations.put(configurationName, lc);
168: }
169: return true;
170: } catch (ReaderException re) {
171: return false;
172: }
173: }
174:
175: /**
176: */
177: private Logger getDefaultLogger() {
178: LogGenerator lg = getDefaultLogGenerator();
179: Logger logger = new Logger("Default", Target.all.intValue(),
180: "Default");
181: logger.addLogGenerator(lg);
182: return logger;
183: }
184:
185: private void addDefaultChannel() {
186: LogGenerator lg = getDefaultLogGenerator();
187: Channel channel = new Channel("Default", lg, false);
188: defaultChannel = channel;
189: }
190:
191: private LogGenerator getDefaultLogGenerator() {
192: Handler consoleHandler = new ConsoleHandler("Default");
193: Formatter defaultFormatter = new DefaultFormatter("Default");
194: LogGenerator lg = new LogGenerator("Default", consoleHandler,
195: defaultFormatter);
196: return lg;
197: }
198:
199: /** This method, once implemented, will be called when the File object
200: * itself changes.
201: *
202: * @param FileListenerEvent The FileListener object.
203: *
204: */
205: public void fileChanged(FileListenerEvent e) {
206: String configName = e.getConfigurationName();
207: reloadLogConfiguration(configName);
208: }
209:
210: private void reloadLogConfiguration(String configurationName) {
211: XMLFileReader reader = new XMLFileReader();
212: reader.setFileName(configurationName + "_logging.xml");
213: try {
214: LogConfiguration lc = reader
215: .parseConfiguration(configurationName);
216: configurations.put(lc.getName(), lc);
217: } catch (ReaderException re) {
218: }
219: }
220:
221: /**
222: * Call thi smethod if you want to force the LogProcessor to write
223: * all pending log statements immediately
224: */
225: public void flush() {
226: LogProcessorFactory.getLogProcessor().flush();
227: }
228:
229: public Log getInstance(String str)
230: throws org.apache.commons.logging.LogConfigurationException {
231: return getLog(str);
232: }
233:
234: public Log getInstance(Class clazz)
235: throws org.apache.commons.logging.LogConfigurationException {
236: return getLog(clazz.getName());
237: }
238:
239: public void release() {
240: LogProcessorFactory.getLogProcessor().flush();
241: }
242:
243: public void removeAttribute(String str) {
244: }
245:
246: public void setAttribute(String str, Object obj) {
247: }
248:
249: public Object getAttribute(String arg0) {
250: return null;
251: }
252:
253: public String[] getAttributeNames() {
254: return null;
255: }
256:
257: }
|