1 18 19 package org.apache.jorphan.logging; 20 21 import java.io.FileWriter ; 22 import java.io.IOException ; 23 import java.io.PrintWriter ; 24 import java.io.Writer ; 25 import java.util.Iterator ; 26 import java.util.Properties ; 27 28 import org.apache.avalon.excalibur.logger.LogKitLoggerManager; 29 import org.apache.avalon.framework.configuration.Configuration; 30 import org.apache.avalon.framework.configuration.ConfigurationException; 31 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 32 import org.apache.avalon.framework.context.Context; 33 import org.apache.avalon.framework.context.ContextException; 34 import org.apache.avalon.framework.context.DefaultContext; 35 import org.apache.jorphan.util.ClassContext; 36 import org.apache.log.Hierarchy; 37 import org.apache.log.LogTarget; 38 import org.apache.log.Logger; 39 import org.apache.log.Priority; 40 import org.apache.log.format.PatternFormatter; 41 import org.apache.log.output.NullOutputLogTarget; 42 import org.apache.log.output.io.WriterTarget; 43 import org.xml.sax.SAXException ; 44 45 48 public final class LoggingManager 49 { 50 56 private static final String DEFAULT_PATTERN = 57 "%{time:yyyy/MM/dd HH:mm:ss} %5.5{priority} - " 58 + "%{category}: %{message} %{throwable}"; 59 60 private static final String PATTERN_THREAD_PREFIX = 61 "%{time:yyyy/MM/dd HH:mm:ss} %5.5{priority} " 62 + "%12{thread} %{category}: %{message} %{throwable}"; 63 64 private static final String PATTERN_THREAD_SUFFIX = 65 "%{time:yyyy/MM/dd HH:mm:ss} %5.5{priority} " 66 + "%{category}[%{thread}]: %{message} %{throwable}"; 67 68 69 private static PatternFormatter format = null; 70 71 72 private static LogTarget target; 73 74 private static boolean isTargetSystemOut = false; private static boolean isWriterSystemOut = false; 78 public final static String LOG_FILE = "log_file"; 79 public final static String LOG_PRIORITY = "log_level"; 80 private static LoggingManager logManager = null; 81 82 private LoggingManager() 83 { 84 target = new NullOutputLogTarget(); 86 } 87 88 public static LoggingManager getLogManager() 89 { 90 return logManager; 91 } 92 93 102 public static void initializeLogging(Properties properties) 103 { 104 if (logManager == null) 105 { 106 logManager = new LoggingManager(); 107 } 108 109 setFormat(properties); 110 111 setTarget( 113 makeWriter( 114 properties.getProperty(LOG_FILE, "jmeter.log"), 115 LOG_FILE)); 116 setPriority(properties.getProperty(LOG_PRIORITY, "INFO")); 117 118 setLoggingLevels(properties); 119 121 setConfig(properties); } 123 124 private static void setFormat(Properties properties) 125 { 126 String pattern = DEFAULT_PATTERN; 127 String type = properties.getProperty("log_format_type",""); 128 if (type.length()==0) 129 { 130 pattern=properties.getProperty("log_format",DEFAULT_PATTERN); 131 } 132 else 133 { 134 if (type.equalsIgnoreCase("thread_suffix")) 135 { 136 pattern=PATTERN_THREAD_SUFFIX; 137 } 138 else 139 if (type.equalsIgnoreCase("thread_prefix")) 140 { 141 pattern=PATTERN_THREAD_PREFIX; 142 } 143 else 144 { 145 pattern=DEFAULT_PATTERN; 146 } 147 } 148 format = new PatternFormatter(pattern+"\n"); 149 } 150 151 152 private static void setConfig(Properties p) 153 { 154 String cfg = p.getProperty("log_config"); 155 if (cfg==null) return; 156 157 Hierarchy hier = Hierarchy.getDefaultHierarchy(); 159 LogKitLoggerManager manager = new LogKitLoggerManager( 160 null,hier,null,null 161 ); 162 163 DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); 164 try { 165 Configuration c = builder.buildFromFile(cfg); 166 Context ctx = new DefaultContext(); 167 manager.contextualize(ctx); 168 manager.configure(c); 169 } catch (IllegalArgumentException e) { 170 System.out.println("Error processing logging config "+cfg); 172 System.out.println(e.toString()); 173 } catch (NullPointerException e) { 174 System.out.println("Error processing logging config "+cfg); 176 System.out.println("Perhaps a log target is missing?"); 177 } catch (ConfigurationException e) { 178 System.out.println("Error processing logging config "+cfg); 179 System.out.println(e.toString()); 180 } catch (SAXException e) { 181 System.out.println("Error processing logging config "+cfg); 182 System.out.println(e.toString()); 183 } catch (IOException e) { 184 System.out.println("Error processing logging config "+cfg); 185 System.out.println(e.toString()); 186 } catch (ContextException e) { 187 System.out.println("Error processing logging config "+cfg); 188 System.out.println(e.toString()); 189 } 190 } 191 195 private static PatternFormatter getFormat() 196 { 197 if (format == null) 198 { 199 format = new PatternFormatter(DEFAULT_PATTERN+"\n"); 200 } 201 return format; 202 } 203 204 205 209 private static Writer makeWriter(String logFile, String propName) 210 { 211 Writer wt; 212 isWriterSystemOut=false; 213 try 214 { 215 wt = new FileWriter (logFile); 216 } 217 catch (Exception e) 218 { 219 System.out.println(propName + "=" + logFile + " " + e.toString()); 220 System.out.println("[" + propName + "-> System.out]"); 221 isWriterSystemOut=true; 222 wt = new PrintWriter (System.out); 223 } 224 return wt; 225 } 226 227 232 private static void setLoggingLevels(Properties appProperties) 233 { 234 Iterator props = appProperties.keySet().iterator(); 235 while (props.hasNext()) 236 { 237 String prop = (String ) props.next(); 238 if (prop.startsWith(LOG_PRIORITY + ".")) 239 { 241 String category = prop.substring(LOG_PRIORITY.length() + 1); 242 setPriority(appProperties.getProperty(prop), category); 243 } 244 if (prop.startsWith(LOG_FILE + ".")) 245 { 246 String category = prop.substring(LOG_FILE.length() + 1); 247 String file = appProperties.getProperty(prop); 248 setTarget( 249 new WriterTarget(makeWriter(file, prop), getFormat()), 250 category); 251 } 252 } 253 } 254 255 private final static String PACKAGE_PREFIX = "org.apache."; 256 257 264 private static String getCallerClassName() 265 { 266 String name = ClassContext.getCallerClassNameAt(3); 267 if (name.startsWith(PACKAGE_PREFIX)) 268 { name = name.substring(PACKAGE_PREFIX.length()); 270 } 271 return name; 272 } 273 274 280 public static Logger getLoggerForClass() 281 { 282 String className = getCallerClassName(); 283 return Hierarchy.getDefaultHierarchy().getLoggerFor(className); 284 } 285 286 public static Logger getLoggerFor(String category) 287 { 288 return Hierarchy.getDefaultHierarchy().getLoggerFor(category); 289 } 290 291 public static void setPriority(String p, String category) 292 { 293 setPriority(Priority.getPriorityForName(p), category); 294 } 295 public static void setPriority(Priority p, String category) 296 { 297 Hierarchy.getDefaultHierarchy().getLoggerFor(category).setPriority(p); 298 } 299 public static void setPriority(String p) 300 { 301 setPriority(Priority.getPriorityForName(p)); 302 } 303 public static void setPriority(Priority p) 304 { 305 Hierarchy.getDefaultHierarchy().setDefaultPriority(p); 306 } 307 public static void setTarget(LogTarget target, String category) 308 { 309 Logger logger = Hierarchy.getDefaultHierarchy().getLoggerFor(category); 310 logger.setLogTargets(new LogTarget[] { target }); 311 } 312 313 319 public static void setTarget(Writer targetFile) 320 { 321 if (target == null) 322 { 323 target = getTarget(targetFile, getFormat()); 324 isTargetSystemOut=isWriterSystemOut; 325 } 326 else 327 { 328 if (!isTargetSystemOut && target instanceof WriterTarget) 329 { 330 ((WriterTarget) target).close(); 331 } 332 target = getTarget(targetFile, getFormat()); 333 isTargetSystemOut=isWriterSystemOut; 334 } 335 Hierarchy.getDefaultHierarchy().setDefaultLogTarget(target); 336 } 337 338 private static LogTarget getTarget(Writer targetFile, PatternFormatter fmt) 339 { 340 return new WriterTarget(targetFile, fmt); 341 } 342 } 343 | Popular Tags |