1 package org.jzonic.jlo; 2 3 import java.util.HashMap ; 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 36 public class CommonsLogManager extends LogFactory implements FileListener { 37 38 private LogConfiguration defaultConfiguration; 39 private static CommonsLogManager lm = null; 40 private HashMap configurations; 41 private Logger defaultLogger; 42 private Channel defaultChannel; 43 private FileWatcher fileWatcher; 44 45 public CommonsLogManager() { 46 configurations = new HashMap (); 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 63 public static CommonsLogManager getInstance() { 64 if ( lm == null ) { 65 lm = new CommonsLogManager(); 66 } 67 return lm; 68 } 69 70 79 public static Log getLog(String loggerName) { 80 return getLog(loggerName,"Default"); 81 } 82 83 99 public static Log getLog(String loggerName,String 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 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 148 public boolean readConfiguration(LogConfigurationReader reader,String 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 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 194 public void fileChanged(FileListenerEvent e) { 195 String configName = e.getConfigurationName(); 196 reloadLogConfiguration(configName); 197 } 198 199 private void reloadLogConfiguration(String 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 214 public void flush() { 215 LogProcessorFactory.getLogProcessor().flush(); 216 } 217 218 public Log getInstance(String str) throws org.apache.commons.logging.LogConfigurationException { 219 return getLog(str); 220 } 221 222 public Log getInstance(Class 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 str) { 231 } 232 233 public void setAttribute(String str, Object obj) { 234 } 235 236 public Object getAttribute(String arg0) { 237 return null; 238 } 239 240 public String [] getAttributeNames() { 241 return null; 242 } 243 244 } 245 | Popular Tags |