KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jzonic > jlo > LogManager


1 package org.jzonic.jlo;
2
3 import org.jzonic.jlo.error.ErrorHandler;
4 import org.jzonic.jlo.events.FileListener;
5 import org.jzonic.jlo.events.FileListenerEvent;
6 import org.jzonic.jlo.formatter.DefaultFormatter;
7 import org.jzonic.jlo.formatter.Formatter;
8 import org.jzonic.jlo.handler.ConsoleHandler;
9 import org.jzonic.jlo.handler.Handler;
10 import org.jzonic.jlo.reader.*;
11 import org.jzonic.jlo.processor.*;
12
13 import java.util.HashMap JavaDoc;
14 /**
15  * This class manages all LogConfigurations. It is the point of entry for jLo.
16  * There is only one instance of the LogManager to make sure that all classes
17  * in one JVM will work with the same configurations. <BR/>
18  * In order to get a Logger you have to call:
19  * <pre>
20  * private static final Logger logger = LogManager.getLogger("com.foo.app");
21  * </pre>
22  * In this case the LogManager when instanciated will search for a file
23  * called "jlo_logging.xml" in your classpath and process it.<BR/>
24  * This method is error save that means that it will always return a valid
25  * Logger. <BR/>
26  * Since jLo supports many LogConfigurations you can also use this method:
27  * <pre>
28  * private static final Logger logger = LogManager.getLogger("com.foo.app","foo");
29  * </pre>
30  * If you provide a configuration name then the LogManager will search for a
31  * file called "foo_logging.xml" in your classpath and process it.
32  *
33  *
34  *@author Andreas Mecky
35  *@author Terry Dye
36  *@created 29. Januar 2002
37  *@version 1.0
38  */

39 public class LogManager implements FileListener {
40     
41     private LogConfiguration defaultConfiguration;
42     private static LogManager lm = null;
43     private HashMap JavaDoc configurations;
44     private Logger defaultLogger;
45     private Channel defaultChannel;
46     private FileWatcher fileWatcher;
47     
48     private LogManager() {
49         configurations = new HashMap JavaDoc();
50         XMLFileReader reader = new XMLFileReader();
51         reader.setFileName("jlo_logging.xml");
52         readConfiguration(reader,"Default");
53         fileWatcher = new FileWatcher();
54         fileWatcher.addFileListener(this, "Default");
55         defaultConfiguration = new LogConfiguration("default");
56         defaultConfiguration.addLogger(getDefaultLogger());
57         defaultLogger = getDefaultLogger();
58         addDefaultChannel();
59     }
60     
61     /**
62      * This method returns the one and only instance of the LogManager.
63      *
64      * @return the instance of the LogManager
65      */

66     public static LogManager getInstance() {
67         if ( lm == null ) {
68             lm = new LogManager();
69         }
70         return lm;
71     }
72     
73     /**
74      * This method returns a Logger from the LogConfiguration.
75      * The name of the LogConfiguration is default and the corresponding
76      * file is jlo_logging.xml. This method uses the LogConfiguration.getLogger
77      * method.
78      *
79      * @param loggerName the name of the logger
80      * @return the Logger or the DefaultLogger if not found
81      */

82     public static Logger getLogger(String JavaDoc loggerName) {
83         return getLogger(loggerName,"Default");
84     }
85     
86     /**
87      * This methods returns a logger for the given name from the specific
88      * logconfiguration. If a logger with the given name does not exist then
89      * the LogConfiguration will try to search for it by going up the hierarchy.
90      * If still no logger is found then this method will return the DefaultLogger.
91      * This method will never return null therefore it is save to do this:
92      * <pre>
93      * private static final Logger logger = LogManager.getLogger("org.foo.app");
94      * </pre>
95      * in your class. If there is no instance of the LogManager so far then
96      * a new one will be created.
97      *
98      * @param loggerName the name of the logger
99      * @param configurationName the name of the configuration
100      * @return the requested logger or the next one in the hierarchy or the default
101      */

102     public static Logger getLogger(String JavaDoc loggerName,String JavaDoc configurationName) {
103         if ( lm == null ) {
104             lm = new LogManager();
105         }
106         if ( configurationName == null ) {
107             Logger logger = lm.defaultConfiguration.getLogger(loggerName);
108             if ( logger == null ) {
109                 ErrorHandler.reportError("Logger "+loggerName+" in "+configurationName+" not found. Using default logger");
110                 return lm.defaultLogger;
111             }
112             return logger;
113         }
114         else {
115             Logger logger = lm.getLogConfiguration(configurationName).getLogger(loggerName);
116             if ( logger == null ) {
117                 ErrorHandler.reportError("Logger "+loggerName+" in "+configurationName+" not found. Using default logger");
118                 return lm.defaultLogger;
119             }
120             return logger;
121         }
122     }
123     
124     /**
125      * @param channelName
126      * @return
127      */

128     public static Channel getChannel(String JavaDoc channelName) {
129         return getChannel(channelName,"Default");
130     }
131     
132     /**
133      * @param channelName
134      * @param configurationName
135      * @return
136      */

137     public static Channel getChannel(String JavaDoc channelName,String JavaDoc configurationName) {
138         if ( lm == null ) {
139             lm = new LogManager();
140         }
141         if ( configurationName == null ) {
142             Channel channel = lm.getLogConfiguration(configurationName).getChannel(channelName);
143             if ( channel == null ) {
144                 return lm.defaultChannel;
145             }
146             return channel;
147         }
148         else {
149             Channel channel = lm.getLogConfiguration(configurationName).getChannel(channelName);
150             if ( channel == null ) {
151                 return lm.defaultChannel;
152             }
153             return channel;
154         }
155     }
156     
157     /**
158      * @param channelName
159      * @return
160      */

161     public static boolean isChannelOn(String JavaDoc channelName) {
162         return isChannelOn(channelName,"Default");
163     }
164     
165     /**
166      * @param channelName
167      * @param configurationName
168      * @return
169      */

170     public static boolean isChannelOn(String JavaDoc channelName,String JavaDoc configurationName) {
171         Channel channel = getChannel(channelName, configurationName);
172         if ( channel == null ) {
173             return false;
174         }
175         return channel.isOn();
176     }
177     
178     public LogConfiguration getLogConfiguration() {
179         return getLogConfiguration("Default");
180     }
181     
182     public LogConfiguration getLogConfiguration(String JavaDoc configurationName) {
183         if ( configurations.containsKey(configurationName) ) {
184             return (LogConfiguration)configurations.get(configurationName);
185         }
186         else {
187             XMLFileReader reader = new XMLFileReader();
188             reader.setFileName(configurationName+"_logging.xml");
189             try {
190                 LogConfiguration lc = reader.parseConfiguration(configurationName);
191                 configurations.put(lc.getName(),lc);
192                 fileWatcher.addFileListener(this,configurationName);
193                 return lc;
194             }
195             catch (ReaderException re) {
196                 ErrorHandler.reportError("Could not find file:"+configurationName+"_logging.xml in the classpath");
197                 return defaultConfiguration;
198             }
199         }
200     }
201     
202     /**
203      *
204      */

205     //TODO: do some error checking
206
//TODO: set up a filewatcher if it is not already running
207
public boolean readConfiguration(LogConfigurationReader reader,String JavaDoc configurationName) {
208         try {
209             LogConfiguration lc = reader.parseConfiguration(configurationName);
210             if ( configurationName == null ) {
211                 defaultConfiguration = lc;
212             }
213             else {
214                 configurations.put(configurationName, lc);
215             }
216             return true;
217         }
218         catch (ReaderException re) {
219             return false;
220         }
221     }
222     
223     /**
224      */

225     private Logger getDefaultLogger() {
226         LogGenerator lg = getDefaultLogGenerator();
227         Logger logger = new Logger("Default", Target.all.intValue(),"Default");
228         logger.addLogGenerator(lg);
229         return logger;
230     }
231     
232     private void addDefaultChannel() {
233         LogGenerator lg = getDefaultLogGenerator();
234         Channel channel = new Channel("Default",lg,false);
235         defaultChannel = channel;
236     }
237     
238     private LogGenerator getDefaultLogGenerator() {
239         Handler consoleHandler = new ConsoleHandler("Default");
240         Formatter defaultFormatter = new DefaultFormatter("Default");
241         LogGenerator lg = new LogGenerator("Default", consoleHandler, defaultFormatter);
242         return lg;
243     }
244     
245     /** This method, once implemented, will be called when the File object
246      * itself changes.
247      *
248      * @param FileListenerEvent The FileListener object.
249      *
250      */

251     public void fileChanged(FileListenerEvent e) {
252         String JavaDoc configName = e.getConfigurationName();
253         reloadLogConfiguration(configName);
254     }
255     
256     private void reloadLogConfiguration(String JavaDoc configurationName) {
257         XMLFileReader reader = new XMLFileReader();
258         reader.setFileName(configurationName+"_logging.xml");
259         try {
260             LogConfiguration lc = reader.parseConfiguration(configurationName);
261             configurations.put(lc.getName(),lc);
262         }
263         catch (ReaderException re) {
264         }
265     }
266     
267     /**
268      * Call thi smethod if you want to force the LogProcessor to write
269      * all pending log statements immediately
270      */

271     public void flush() {
272         LogProcessorFactory.getLogProcessor().flush();
273     }
274 }
275
Popular Tags