KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

99     public static Log getLog(String JavaDoc loggerName,String JavaDoc configurationName) {
100         if ( lm == null ) {
101             lm = new CommonsLogManager();
102         }
103         if ( configurationName == null ) {
104             Logger logger = lm.defaultConfiguration.getLogger(loggerName);
105             if ( logger == null ) {
106                 ErrorHandler.reportError("Logger "+loggerName+" in "+configurationName+" not found. Using default logger");
107                 return lm.defaultLogger;
108             }
109             return logger;
110         }
111         else {
112             Logger logger = lm.getLogConfiguration(configurationName).getLogger(loggerName);
113             if ( logger == null ) {
114                 ErrorHandler.reportError("Logger "+loggerName+" in "+configurationName+" not found. Using default logger");
115                 return lm.defaultLogger;
116             }
117             return logger;
118         }
119     }
120     
121     public LogConfiguration getLogConfiguration() {
122         return getLogConfiguration("Default");
123     }
124     
125     public LogConfiguration getLogConfiguration(String JavaDoc configurationName) {
126         if ( configurations.containsKey(configurationName) ) {
127             return (LogConfiguration)configurations.get(configurationName);
128         }
129         else {
130             XMLFileReader reader = new XMLFileReader();
131             reader.setFileName(configurationName+"_logging.xml");
132             try {
133                 LogConfiguration lc = reader.parseConfiguration(configurationName);
134                 configurations.put(lc.getName(),lc);
135                 fileWatcher.addFileListener(this,configurationName);
136                 return lc;
137             }
138             catch (ReaderException re) {
139                 ErrorHandler.reportError("Could not find file:"+configurationName+"_logging.xml in the classpath");
140                 return defaultConfiguration;
141             }
142         }
143     }
144     
145     /**
146      *
147      */

148     //TODO: do some error checking
149
//TODO: set up a filewatcher if it is not already running
150
public boolean readConfiguration(LogConfigurationReader reader,String JavaDoc configurationName) {
151         try {
152             LogConfiguration lc = reader.parseConfiguration(configurationName);
153             if ( configurationName == null ) {
154                 defaultConfiguration = lc;
155             }
156             else {
157                 configurations.put(configurationName, lc);
158             }
159             return true;
160         }
161         catch (ReaderException re) {
162             return false;
163         }
164     }
165     
166     /**
167      */

168     private Logger getDefaultLogger() {
169         LogGenerator lg = getDefaultLogGenerator();
170         Logger logger = new Logger("Default", Target.all.intValue(),"Default");
171         logger.addLogGenerator(lg);
172         return logger;
173     }
174     
175     private void addDefaultChannel() {
176         LogGenerator lg = getDefaultLogGenerator();
177         Channel channel = new Channel("Default",lg,false);
178         defaultChannel = channel;
179     }
180     
181     private LogGenerator getDefaultLogGenerator() {
182         Handler consoleHandler = new ConsoleHandler("Default");
183         Formatter defaultFormatter = new DefaultFormatter("Default");
184         LogGenerator lg = new LogGenerator("Default", consoleHandler, defaultFormatter);
185         return lg;
186     }
187     
188     /** This method, once implemented, will be called when the File object
189      * itself changes.
190      *
191      * @param FileListenerEvent The FileListener object.
192      *
193      */

194     public void fileChanged(FileListenerEvent e) {
195         String JavaDoc configName = e.getConfigurationName();
196         reloadLogConfiguration(configName);
197     }
198     
199     private void reloadLogConfiguration(String JavaDoc configurationName) {
200         XMLFileReader reader = new XMLFileReader();
201         reader.setFileName(configurationName+"_logging.xml");
202         try {
203             LogConfiguration lc = reader.parseConfiguration(configurationName);
204             configurations.put(lc.getName(),lc);
205         }
206         catch (ReaderException re) {
207         }
208     }
209     
210     /**
211      * Call thi smethod if you want to force the LogProcessor to write
212      * all pending log statements immediately
213      */

214     public void flush() {
215         LogProcessorFactory.getLogProcessor().flush();
216     }
217     
218     public Log getInstance(String JavaDoc str) throws org.apache.commons.logging.LogConfigurationException {
219         return getLog(str);
220     }
221     
222     public Log getInstance(Class JavaDoc clazz) throws org.apache.commons.logging.LogConfigurationException {
223         return getLog(clazz.getName());
224     }
225     
226     public void release() {
227         LogProcessorFactory.getLogProcessor().flush();
228     }
229     
230     public void removeAttribute(String JavaDoc str) {
231     }
232     
233     public void setAttribute(String JavaDoc str, Object JavaDoc obj) {
234     }
235
236     public Object JavaDoc getAttribute(String JavaDoc arg0) {
237         return null;
238     }
239
240     public String JavaDoc[] getAttributeNames() {
241         return null;
242     }
243     
244 }
245
Popular Tags