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 ; 14 39 public class LogManager implements FileListener { 40 41 private LogConfiguration defaultConfiguration; 42 private static LogManager lm = null; 43 private HashMap configurations; 44 private Logger defaultLogger; 45 private Channel defaultChannel; 46 private FileWatcher fileWatcher; 47 48 private LogManager() { 49 configurations = new HashMap (); 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 66 public static LogManager getInstance() { 67 if ( lm == null ) { 68 lm = new LogManager(); 69 } 70 return lm; 71 } 72 73 82 public static Logger getLogger(String loggerName) { 83 return getLogger(loggerName,"Default"); 84 } 85 86 102 public static Logger getLogger(String loggerName,String 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 128 public static Channel getChannel(String channelName) { 129 return getChannel(channelName,"Default"); 130 } 131 132 137 public static Channel getChannel(String channelName,String 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 161 public static boolean isChannelOn(String channelName) { 162 return isChannelOn(channelName,"Default"); 163 } 164 165 170 public static boolean isChannelOn(String channelName,String 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 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 205 public boolean readConfiguration(LogConfigurationReader reader,String 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 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 251 public void fileChanged(FileListenerEvent e) { 252 String configName = e.getConfigurationName(); 253 reloadLogConfiguration(configName); 254 } 255 256 private void reloadLogConfiguration(String 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 271 public void flush() { 272 LogProcessorFactory.getLogProcessor().flush(); 273 } 274 } 275 | Popular Tags |