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