Source Code Cross Referenced for LogManager.java in  » Development » jLo » org » jzonic » jlo » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Development » jLo » org.jzonic.jlo 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.