1 25 26 27 package org.objectweb.jonas.common; 28 29 import java.io.File ; 30 import java.io.FileInputStream ; 31 import java.io.FileNotFoundException ; 32 import java.io.InputStream ; 33 import java.io.PrintWriter ; 34 import java.text.SimpleDateFormat ; 35 import java.util.Date ; 36 import java.util.Enumeration ; 37 import java.util.Properties ; 38 39 import org.objectweb.jonas_timer.TraceTimer; 40 41 import org.objectweb.common.TraceCore; 42 43 import org.objectweb.jonas_ejb.container.TraceEjb; 44 45 import org.objectweb.jonas_jms.TraceJms; 46 47 import org.objectweb.util.monolog.Monolog; 48 import org.objectweb.util.monolog.api.BasicLevel; 49 import org.objectweb.util.monolog.api.HandlerFactory; 50 import org.objectweb.util.monolog.api.LevelFactory; 51 import org.objectweb.util.monolog.api.Logger; 52 import org.objectweb.util.monolog.api.LoggerFactory; 53 import org.objectweb.util.monolog.api.MonologFactory; 54 import org.objectweb.util.monolog.file.DottedStringTools; 55 import org.objectweb.util.monolog.file.monolog.PropertiesConfAccess; 56 import org.objectweb.util.monolog.wrapper.p6spy.P6SpyLogger; 57 import org.objectweb.util.monolog.wrapper.printwriter.PrintWriterImpl; 58 59 67 public class Log { 68 69 private static String configFile = "trace"; 71 72 75 private static LoggerFactory lf = null; 76 77 81 private static Properties props = null; 82 83 88 public static final String JONAS_ADMIN_PREFIX = "org.objectweb.jonas.admin"; 89 90 public static final String JONAS_DBM_PREFIX = "org.objectweb.jonas.dbm"; 91 92 public static final String JONAS_JCA_PREFIX = "org.objectweb.jonas.jca"; 93 94 public static final String JONAS_CLIENT_PREFIX = "org.objectweb.jonas.client"; 95 96 public static final String JONAS_JDBCXA_PREFIX = "org.objectweb.jonas.jdbc_xa"; 97 98 public static final String JONAS_NAMING_PREFIX = "org.objectweb.jonas.naming"; 99 100 public static final String JONAS_PROPCTX_PREFIX = "org.objectweb.jonas.propagation"; 101 102 public static final String JONAS_REGISTRY_PREFIX = "org.objectweb.jonas.registry"; 103 104 public static final String JONAS_SECURITY_PREFIX = "org.objectweb.jonas.security"; 105 106 public static final String JONAS_CLIENTSTUBGEN_PREFIX = "org.objectweb.jonas_lib.genclientstub"; 107 108 public static final String JONAS_GENBASE_PREFIX = "org.objectweb.jonas_lib.genbase"; 109 110 public static final String JONAS_JACC_SECURITY_PREFIX = "org.objectweb.jonas.security.jacc"; 111 112 public static final String JONAS_WS_SECURITY_PREFIX = "org.objectweb.jonas.security.ws"; 113 114 public static final String JONAS_CSIV2_SECURITY_PREFIX = "org.objectweb.jonas.security.csiv2"; 115 116 public static final String JONAS_CSIV2_DETAILS_SECURITY_PREFIX = "org.objectweb.jonas.security.csiv2_details"; 117 118 public static final String JONAS_SERVER_PREFIX = "org.objectweb.jonas.server"; 119 120 public static final String JONAS_WEB_PREFIX = "org.objectweb.jonas.web"; 121 122 public static final String JONAS_EAR_PREFIX = "org.objectweb.jonas.ear"; 123 124 public static final String JONAS_MAIL_PREFIX = "org.objectweb.jonas.mail"; 125 126 public static final String JONAS_TESTS_PREFIX = "org.objectweb.jonas_tests"; 127 128 public static final String JONAS_LOADER_PREFIX = "org.objectweb.jonas.loader"; 129 130 public static final String JONAS_GENIC_PREFIX = "org.objectweb.jonas.genic"; 131 132 public static final String JONAS_GENIC_VELOCITY_PREFIX = "org.objectweb.jonas.genic.velocity"; 133 134 public static final String JONAS_WSGEN_PREFIX = "org.objectweb.jonas_ws.wsgen"; 135 136 public static final String JONAS_WSGEN_EWS_PREFIX = "org.objectweb.jonas_ws.wsgen.ews"; 137 138 public static final String JONAS_WS_PREFIX = "org.objectweb.jonas.ws"; 139 140 public static final String JONAS_WS_EJBPROVIDER_PREFIX = "org.objectweb.jonas.ws.ejbprovider"; 141 142 public static final String JONAS_PUBLISH_PREFIX = "org.objectweb.jonas.publication"; 143 144 public static final String JONAS_JMX_PREFIX = "org.objectweb.jonas.jmx"; 145 146 public static final String JONAS_MANAGEMENT_PREFIX = "org.objectweb.jonas.management"; 147 148 public static final String JONAS_DOMAIN_MANAGEMENT_PREFIX = "org.objectweb.jonas.domain.management"; 149 150 public static final String JONAS_MANAGEMENT_EVENT_PREFIX = "org.objectweb.jonas.management.event"; 151 152 public static final String JONAS_MEJB = "org.objectweb.jonas.mejb"; 153 154 public static final String JONAS_DISCOVERY_PREFIX = "org.objectweb.jonas.discovery"; 155 156 public static final String SPY_LOGGER_NAME = "org.objectweb.jonas.jdbc.sql"; 157 158 public static final String JONAS_DEPLOY_WORK_PREFIX = "org.objectweb.jonas_lib.deployment.work"; 159 160 public static final String JONAS_JAXR_PREFIX = "org.objectweb.jonas.jaxr"; 161 162 public static final String JONAS_WORK_MGR_PREFIX = "org.objectweb.jonas_lib.work"; 163 164 165 168 private static boolean isDebugNaming = false; 169 170 171 174 public static final String JONAS_DB_PREFIX = "org.objectweb.jonas.db"; 175 176 179 private static final String HANDLER_OUTPUT_ATTRIBUTE = "output"; 180 181 184 private static final String AUTOMATIC_CONFIG = "automatic"; 185 186 189 private static final String LOG_DIRECTORY = "logs"; 190 191 194 private static final String TIMESTAMP_FORMAT = "-yyyy-MM-dd"; 195 196 199 private static final String SUFFIX_LOGFILE = "log"; 200 201 204 private static final String DEFAULT_LOGGERFACTORY = "org.objectweb.util.monolog.wrapper.javaLog.LoggerFactory"; 205 206 private static boolean clientcontainer = false; 207 208 211 private static final String JPROP_CLASSNAME = "org.objectweb.jonas.common.JProp"; 212 213 217 public static void configure(String file) { 218 configFile = file; 219 getLoggerFactory(); 220 if (!clientcontainer) { 221 P6SpyLogger.logger = lf.getLogger(SPY_LOGGER_NAME); 222 TraceTimer.configure(lf); 223 TraceCore.configure(lf); 224 } 227 228 TraceEjb.configure(lf); 229 TraceJms.configure(lf); 230 } 231 232 236 public static synchronized LoggerFactory getLoggerFactory() { 237 if (lf == null) { 238 try { 239 if (isClient()) { 241 clientcontainer = true; 242 InputStream is = null; 243 244 String traceClient = System.getProperty("jonas.client.trace.file"); 246 if (traceClient != null) { 247 try { 248 is = new FileInputStream (new File (traceClient)); 249 } catch (FileNotFoundException fne) { 250 System.err.println("Can not init logger with the supplied file '" + traceClient + "', this file doesn't exist. Init with default values."); 251 252 } 253 } else { 254 is = Thread.currentThread().getContextClassLoader().getResourceAsStream("traceclient.properties"); 257 } 258 if (is == null) { 259 props = new Properties (); 262 props.put("log.config.classname", DEFAULT_LOGGERFACTORY); 263 props.put("logger.root.level", "INFO"); 264 } else { 265 props = new Properties (); 267 props.load(is); 268 } 269 lf = Monolog.getMonologFactory(props); 270 return lf; 271 } 272 Object jProp = null; 275 try { 276 jProp = JProp.getInstance(); 277 JProp.getInstance(configFile); 280 } catch (Exception e) { 281 System.err.println("Can't read jonas.properties. Check that you have defined a $JONAS_BASE variable"); 282 } 283 284 285 props = JProp.getInstance(configFile).getConfigFileEnv(); 287 String jonasBase = null; 288 String nameOfServer = null; 289 if (jProp != null) { 290 jonasBase = JProp.getJonasBase(); 291 nameOfServer = ((JProp) jProp).getValue("jonas.name", "jonas"); 292 } else { 293 jonasBase = "."; 295 nameOfServer = "jonas"; 296 } 297 298 File logFirFile = new File (jonasBase + File.separator + LOG_DIRECTORY); 301 302 Properties updatedProps = null; 305 306 SimpleDateFormat sdf = new SimpleDateFormat (TIMESTAMP_FORMAT); 308 String date = sdf.format(new Date ()); 309 int nbAut = 0; 311 312 File logFile = null; 313 for (Enumeration keys = props.keys(); keys.hasMoreElements();) { 314 String key = (String ) keys.nextElement(); 315 if (key == null) { 316 continue; 317 } 318 319 if (key.startsWith(PropertiesConfAccess.HANDLER_FIELD)) { 320 String temp = DottedStringTools.getFirst(key); 321 322 if (temp == null) { 323 continue; 324 } 325 326 temp = DottedStringTools.getEnd(key); 327 if (temp == null) { 328 continue; 329 } 330 331 String handlerName = DottedStringTools.getBegin(temp); 332 333 String stringType = props.getProperty(PropertiesConfAccess.HANDLER_FIELD + PropertiesConfAccess.DOT + handlerName + PropertiesConfAccess.DOT + PropertiesConfAccess.HANDLER_TYPE_ATTRIBUTE, null); 334 String stringOutput = props.getProperty(PropertiesConfAccess.HANDLER_FIELD + PropertiesConfAccess.DOT + handlerName + PropertiesConfAccess.DOT + HANDLER_OUTPUT_ATTRIBUTE, null); 335 if ((stringType != null) && (stringOutput != null)) { 338 if (PropertiesConfAccess.HANDLER_TYPE_ATTRIBUTE_FILE_VALUE.equalsIgnoreCase(stringType) 339 || (PropertiesConfAccess.HANDLER_TYPE_ATTRIBUTE_ROLLING_FILE_VALUE.equalsIgnoreCase(stringType))) { 340 if (stringOutput.equalsIgnoreCase(AUTOMATIC_CONFIG)) { 341 String fileName = null; 343 344 if (nbAut > 0) { 345 fileName = nameOfServer + date + PropertiesConfAccess.DOT + nbAut + PropertiesConfAccess.DOT + SUFFIX_LOGFILE; 347 } else { 348 updatedProps = (Properties ) props.clone(); 350 fileName = nameOfServer + date + PropertiesConfAccess.DOT + SUFFIX_LOGFILE; 351 } 352 353 if (logFirFile.exists()) { 355 logFile = new File (logFirFile, fileName); 356 } else { 357 logFile = new File (fileName); 359 } 360 361 updatedProps.setProperty(PropertiesConfAccess.HANDLER_FIELD + PropertiesConfAccess.DOT + handlerName + PropertiesConfAccess.DOT + HANDLER_OUTPUT_ATTRIBUTE, logFile.getPath()); 362 nbAut++; 363 364 } 365 else { 366 logFile = new File (stringOutput); 368 if (!logFile.isAbsolute()) { 369 if (logFirFile.exists()) { logFile = new File (logFirFile, stringOutput); 372 } else { logFile = new File (stringOutput); 374 } 375 if (nbAut == 0) { 376 updatedProps = (Properties ) props.clone(); 377 nbAut++; 378 } 379 if (stringOutput.endsWith(".automatic") || 380 stringOutput.endsWith(".auto")) { 381 nbAut++; 382 String autoStr = logFile.getPath().substring(0, logFile.getPath().lastIndexOf('.')) + date + 383 PropertiesConfAccess.DOT + nbAut + PropertiesConfAccess.DOT + SUFFIX_LOGFILE; 384 logFile = new File (autoStr); 385 } 386 updatedProps.setProperty(PropertiesConfAccess.HANDLER_FIELD + PropertiesConfAccess.DOT + handlerName + PropertiesConfAccess.DOT + HANDLER_OUTPUT_ATTRIBUTE, logFile.getPath()); 387 } 388 } 389 } 390 } 391 } 392 } 393 394 String b = props.getProperty("log.config.classname", null); 396 if (b == null) { 397 System.err.println("Malformed configuration log file: log.config.classname not available"); 398 return null; 399 } 400 lf = (LoggerFactory) Class.forName(b).newInstance(); 401 if (updatedProps == null) { 403 PropertiesConfAccess.load(props, lf, (HandlerFactory) lf, (LevelFactory) lf); 404 } else { 405 PropertiesConfAccess.load(updatedProps, lf, (HandlerFactory) lf, (LevelFactory) lf); 406 } 407 Monolog.monologFactory = (MonologFactory) lf; 409 } catch (Exception e) { 410 System.err.println("Cannot get LoggerFactory:" + e); 411 e.printStackTrace(); 412 } 413 } 414 return lf; 415 } 416 417 420 private static boolean isClient() { 421 boolean hasJonasBase = System.getProperty("jonas.base") != null; 422 boolean hasJonasRoot = System.getProperty("install.root") != null; 423 boolean hasJProp = true; 424 try { 425 Thread.currentThread().getContextClassLoader().loadClass(JPROP_CLASSNAME); 426 } catch (ClassNotFoundException cnfe) { 427 hasJProp = false; 428 } 429 return !(hasJProp && hasJonasBase && hasJonasRoot); 431 } 432 433 436 public static void reset() { 437 lf = null; 438 } 439 440 445 public static PrintWriter getLogWriter(String topic) { 446 return new PrintWriterImpl(getLogger(topic), getLoggerFactory()); 448 } 449 450 453 public static LevelFactory getLevelFactory() { 454 return (LevelFactory) getLoggerFactory(); 455 } 456 457 462 public static Logger getLogger(String topic) { 463 return getLoggerFactory().getLogger(topic); 464 } 465 466 public static Properties getProperties() { 467 return props; 468 } 469 470 public static final boolean isDebugNaming() { 471 return isDebugNaming; 472 } 473 474 475 478 public static void syncLevels() { 479 isDebugNaming = getLogger(JONAS_NAMING_PREFIX).isLoggable(BasicLevel.DEBUG); 481 } 482 } 483 | Popular Tags |