1 package org.jacorb.config; 2 3 22 23 import org.apache.avalon.framework.logger.Logger; 24 import org.apache.avalon.framework.logger.LogKitLogger; 25 import org.apache.avalon.framework.configuration.ConfigurationException; 26 27 import org.apache.log.*; 28 import org.apache.log.format.*; 29 import org.apache.log.output.io.*; 30 import org.apache.log.output.io.rotate.*; 31 32 import java.util.*; 33 import java.io.*; 34 35 59 60 class LogKitLoggerFactory 61 implements LoggerFactory 62 { 63 private final static String DEFAULT_LOG_PATTERN = 64 "[%.20{category}] %.7{priority} : %{message}\\n%{throwable}"; 65 66 private final static String name = "logkit"; 67 private PatternFormatter logFormatter = null; 68 69 70 private int defaultPriority = 0; 71 72 73 private final Map namedLoggers = new HashMap(); 74 75 76 private boolean append = false; 77 78 79 private Writer consoleWriter; 80 81 82 private LogTarget consoleTarget; 83 84 85 private LogTarget defaultTarget = null; 86 87 private Configuration configuration = null; 88 89 public void configure(org.apache.avalon.framework.configuration.Configuration configuration) 90 throws org.apache.avalon.framework.configuration.ConfigurationException 91 { 92 this.configuration = (Configuration)configuration; 93 94 defaultPriority = 95 configuration.getAttributeAsInteger("jacorb.log.default.verbosity", 0); 96 97 append = 98 configuration.getAttribute("jacorb.logfile.append","off").equals("on"); 99 100 logFormatter = 101 new PatternFormatter(configuration.getAttribute("jacorb.log.default.log_pattern", 102 DEFAULT_LOG_PATTERN)); 103 switch (defaultPriority) 104 { 105 case 0: 106 case 1: 107 case 2: 108 case 3: 109 case 4: 110 break; 111 default: 112 throw new ConfigurationException("'" + defaultPriority + "' is an illegal" 113 + " value for the property jacorb.log.default.verbosity. Valid values are [0-4]"); 114 } 115 consoleWriter = new OutputStreamWriter(System.err); 116 consoleTarget = new WriterTarget(consoleWriter, logFormatter); 117 } 118 119 123 124 public void setDefaultPriority(int priority) 125 { 126 defaultPriority = priority; 127 } 128 129 133 134 public final String getLoggingBackendName() 135 { 136 return name; 137 } 138 139 144 145 public Logger getNamedLogger(String name) 146 { 147 return getNamedLogger(name, null); 148 } 149 150 155 156 public Logger getNamedRootLogger(String name) 157 { 158 return getNamedLogger(name, consoleTarget); 159 } 160 161 162 172 173 public Logger getNamedLogger(String name, String logFileName, long maxLogSize) 174 throws IOException 175 { 176 if (name == null) 177 throw new IllegalArgumentException ("Log file name must not be null!"); 178 179 FileOutputStream logStream = 180 new FileOutputStream(logFileName, append); 181 182 LogTarget target = null; 183 if (maxLogSize == 0) 184 { 185 Writer logWriter = new OutputStreamWriter(logStream); 187 target = new WriterTarget(logWriter, logFormatter); 188 } 189 else 190 { 191 target = 193 new RotatingFileTarget(append, 194 logFormatter, 195 new RotateStrategyBySize(maxLogSize * 1000), 196 new RevolvingFileStrategy(new File(logFileName), 10000)); 197 } 198 return getNamedLogger(name, target); 199 } 200 201 202 209 public Logger getNamedLogger(String name, LogTarget target) 210 { 211 Object o = namedLoggers.get(name); 212 213 if ( o != null ) 214 { 215 return (Logger)o; 216 } 217 218 org.apache.log.Logger logger = 219 Hierarchy.getDefaultHierarchy().getLoggerFor(name); 220 221 int priority = getPriorityForNamedLogger(name); 222 223 logger.setPriority(intToPriority(priority)); 224 225 if (target != null ) 226 { 227 logger.setLogTargets( new LogTarget[] { target } ); 229 } 230 else 231 { 232 if (defaultTarget == null) 234 { 235 logger.setLogTargets(new LogTarget[] { consoleTarget } ); 236 } 237 else 238 { 239 logger.setLogTargets(new LogTarget[] { defaultTarget } ); 240 } 241 } 242 243 Logger result = new LogKitLogger(logger); 244 245 namedLoggers.put(name, result); 246 247 return result; 248 } 249 250 251 260 public int getPriorityForNamedLogger(String name) 261 { 262 String prefix = name; 263 264 while (!prefix.equals("")) 265 { 266 String priorityString = null; 267 268 try 269 { 270 int priorityForLogger = 271 configuration.getAttributeAsInteger( prefix + ".log.verbosity"); 272 if (priorityForLogger > 4) 273 priorityForLogger = 4; 274 else if (priorityForLogger < 0) 275 priorityForLogger = 0; 276 return priorityForLogger; 277 } 278 catch( ConfigurationException ce ) 279 { 280 } 281 282 if (prefix.lastIndexOf(".") >= 0) 283 { 284 prefix = prefix.substring(0, prefix.lastIndexOf(".")); 285 } 286 else 287 { 288 prefix = ""; 289 } 290 } 291 292 return defaultPriority; 294 } 295 296 297 303 public static org.apache.log.Priority intToPriority(int priority) 304 { 305 switch (priority) 306 { 307 case 4 : 308 return org.apache.log.Priority.DEBUG; 309 case 3 : 310 return org.apache.log.Priority.INFO; 311 case 2 : 312 return org.apache.log.Priority.WARN; 313 case 1 : 314 return org.apache.log.Priority.ERROR; 315 case 0 : 316 default : 317 return org.apache.log.Priority.FATAL_ERROR; 318 } 319 } 320 321 public void setDefaultLogFile(String fileName, long maxLogSize) throws java.io.IOException 322 { 323 FileOutputStream logStream = 324 new FileOutputStream(fileName, append); 325 326 if (maxLogSize == 0) 327 { 328 Writer logWriter = new OutputStreamWriter(logStream); 330 defaultTarget = new WriterTarget(logWriter, logFormatter); 331 } 332 else 333 { 334 defaultTarget = 336 new RotatingFileTarget(append, 337 logFormatter, 338 new RotateStrategyBySize(maxLogSize * 1000), 339 new RevolvingFileStrategy(new File(fileName), 10000)); 340 } 341 } 342 } 343 | Popular Tags |