00001
00027 package org.objectweb.jonas.common;
00028
00029 import java.io.File;
00030 import java.io.FileInputStream;
00031 import java.io.FileNotFoundException;
00032 import java.io.InputStream;
00033 import java.io.PrintWriter;
00034 import java.text.SimpleDateFormat;
00035 import java.util.Date;
00036 import java.util.Enumeration;
00037 import java.util.Properties;
00038
00039 import org.objectweb.jonas_timer.TraceTimer;
00040
00041 import org.objectweb.common.TraceCore;
00042
00043 import org.objectweb.jonas_ejb.container.TraceEjb;
00044
00045 import org.objectweb.jonas_jms.TraceJms;
00046
00047 import org.objectweb.util.monolog.Monolog;
00048 import org.objectweb.util.monolog.api.HandlerFactory;
00049 import org.objectweb.util.monolog.api.LevelFactory;
00050 import org.objectweb.util.monolog.api.Logger;
00051 import org.objectweb.util.monolog.api.LoggerFactory;
00052 import org.objectweb.util.monolog.api.MonologFactory;
00053 import org.objectweb.util.monolog.file.DottedStringTools;
00054 import org.objectweb.util.monolog.file.monolog.PropertiesConfAccess;
00055 import org.objectweb.util.monolog.wrapper.p6spy.P6SpyLogger;
00056 import org.objectweb.util.monolog.wrapper.printwriter.PrintWriterImpl;
00057
00066 public class Log {
00067
00068
00069 private static String configFile = "trace";
00070
00074 private static LoggerFactory lf = null;
00075
00080 private static Properties props = null;
00081
00082
00083
00084
00085
00086
00087 public static final String JONAS_DBM_PREFIX = "org.objectweb.jonas.dbm";
00088
00089 public static final String JONAS_JCA_PREFIX = "org.objectweb.jonas.jca";
00090
00091 public static final String JONAS_CLIENT_PREFIX = "org.objectweb.jonas.client";
00092
00093 public static final String JONAS_JDBCXA_PREFIX = "org.objectweb.jonas.jdbc_xa";
00094
00095 public static final String JONAS_NAMING_PREFIX = "org.objectweb.jonas.naming";
00096
00097 public static final String JONAS_PROPCTX_PREFIX = "org.objectweb.jonas.propagation";
00098
00099 public static final String JONAS_REGISTRY_PREFIX = "org.objectweb.jonas.registry";
00100
00101 public static final String JONAS_SECURITY_PREFIX = "org.objectweb.jonas.security";
00102
00103 public static final String JONAS_CLIENTSTUBGEN_PREFIX = "org.objectweb.jonas_lib.genclientstub";
00104
00105 public static final String JONAS_GENBASE_PREFIX = "org.objectweb.jonas_lib.genbase";
00106
00107 public static final String JONAS_JACC_SECURITY_PREFIX = "org.objectweb.jonas.security.jacc";
00108
00109 public static final String JONAS_WS_SECURITY_PREFIX = "org.objectweb.jonas.security.ws";
00110
00111 public static final String JONAS_CSIV2_SECURITY_PREFIX = "org.objectweb.jonas.security.csiv2";
00112
00113 public static final String JONAS_CSIV2_DETAILS_SECURITY_PREFIX = "org.objectweb.jonas.security.csiv2_details";
00114
00115 public static final String JONAS_SERVER_PREFIX = "org.objectweb.jonas.server";
00116
00117 public static final String JONAS_WEB_PREFIX = "org.objectweb.jonas.web";
00118
00119 public static final String JONAS_EAR_PREFIX = "org.objectweb.jonas.ear";
00120
00121 public static final String JONAS_MAIL_PREFIX = "org.objectweb.jonas.mail";
00122
00123 public static final String JONAS_TESTS_PREFIX = "org.objectweb.jonas_tests";
00124
00125 public static final String JONAS_LOADER_PREFIX = "org.objectweb.jonas.loader";
00126
00127 public static final String JONAS_GENIC_PREFIX = "org.objectweb.jonas.genic";
00128
00129 public static final String JONAS_GENIC_VELOCITY_PREFIX = "org.objectweb.jonas.genic.velocity";
00130
00131 public static final String JONAS_WSGEN_PREFIX = "org.objectweb.jonas_ws.wsgen";
00132
00133 public static final String JONAS_WSGEN_EWS_PREFIX = "org.objectweb.jonas_ws.wsgen.ews";
00134
00135 public static final String JONAS_WS_PREFIX = "org.objectweb.jonas.ws";
00136
00137 public static final String JONAS_WS_EJBPROVIDER_PREFIX = "org.objectweb.jonas.ws.ejbprovider";
00138
00139 public static final String JONAS_PUBLISH_PREFIX = "org.objectweb.jonas.publication";
00140
00141 public static final String JONAS_JMX_PREFIX = "org.objectweb.jonas.jmx";
00142
00143 public static final String JONAS_MANAGEMENT_PREFIX = "org.objectweb.jonas.management";
00144
00145 public static final String JONAS_MANAGEMENT_EVENT_PREFIX = "org.objectweb.jonas.management.event";
00146
00147 public static final String JONAS_MEJB = "org.objectweb.jonas.mejb";
00148
00149 public static final String JONAS_DISCOVERY_PREFIX = "org.objectweb.jonas.discovery";
00150
00151 public static final String SPY_LOGGER_NAME = "org.objectweb.jonas.jdbc.sql";
00152
00153 public static final String JONAS_DEPLOY_WORK_PREFIX = "org.objectweb.jonas_lib.deployment.work";
00154
00155 public static final String JONAS_JAXR_PREFIX = "org.objectweb.jonas.jaxr";
00156
00157
00161 public static final String JONAS_DB_PREFIX = "org.objectweb.jonas.db";
00162
00166 private static final String HANDLER_OUTPUT_ATTRIBUTE = "output";
00167
00171 private static final String AUTOMATIC_CONFIG = "automatic";
00172
00176 private static final String LOG_DIRECTORY = "logs";
00177
00181 private static final String TIMESTAMP_FORMAT = "-yyyy-MM-dd";
00182
00186 private static final String SUFFIX_LOGFILE = "log";
00187
00191 private static final String DEFAULT_LOGGERFACTORY = "org.objectweb.util.monolog.wrapper.javaLog.LoggerFactory";
00192
00193 private static boolean clientcontainer = false;
00194
00198 private static final String JPROP_CLASSNAME = "org.objectweb.jonas.common.JProp";
00199
00204 public static void configure(String file) {
00205 configFile = file;
00206 getLoggerFactory();
00207 if (!clientcontainer) {
00208 P6SpyLogger.logger = lf.getLogger(SPY_LOGGER_NAME);
00209 TraceTimer.configure(lf);
00210 TraceCore.configure(lf);
00211
00212
00213 }
00214
00215 TraceEjb.configure(lf);
00216 TraceJms.configure(lf);
00217 }
00218
00223 public static synchronized LoggerFactory getLoggerFactory() {
00224 if (lf == null) {
00225 try {
00226
00227 if (isClient()) {
00228 clientcontainer = true;
00229 InputStream is = null;
00230
00231
00232 String traceClient = System.getProperty("jonas.client.trace.file");
00233 if (traceClient != null) {
00234 try {
00235 is = new FileInputStream(new File(traceClient));
00236 } catch (FileNotFoundException fne) {
00237 System.err.println("Can not init logger with the supplied file '" + traceClient + "', this file doesn't exist. Init with default values.");
00238
00239 }
00240 } else {
00241
00242
00243 is = Thread.currentThread().getContextClassLoader().getResourceAsStream("traceclient.properties");
00244 }
00245 if (is == null) {
00246
00247
00248 props = new Properties();
00249 props.put("log.config.classname", DEFAULT_LOGGERFACTORY);
00250 props.put("logger.root.level", "INFO");
00251 } else {
00252
00253 props = new Properties();
00254 props.load(is);
00255 }
00256 lf = Monolog.getMonologFactory(props);
00257 return lf;
00258 }
00259
00260
00261 Object jProp = null;
00262 try {
00263 jProp = JProp.getInstance();
00264
00265
00266 JProp.getInstance(configFile);
00267 } catch (Exception e) {
00268 System.err.println("Can't read jonas.properties. Check that you have defined a $JONAS_BASE variable");
00269 }
00270
00271
00272
00273 props = JProp.getInstance(configFile).getConfigFileEnv();
00274 String jonasBase = null;
00275 String nameOfServer = null;
00276 if (jProp != null) {
00277 jonasBase = JProp.getJonasBase();
00278 nameOfServer = ((JProp) jProp).getValue("jonas.name", "jonas");
00279 } else {
00280
00281 jonasBase = ".";
00282 nameOfServer = "jonas";
00283 }
00284
00285
00286
00287 File logFirFile = new File(jonasBase + File.separator + LOG_DIRECTORY);
00288
00289
00290
00291 Properties updatedProps = null;
00292
00293
00294 SimpleDateFormat sdf = new SimpleDateFormat(TIMESTAMP_FORMAT);
00295 String date = sdf.format(new Date());
00296
00297 int nbAut = 0;
00298
00299 File logFile = null;
00300 for (Enumeration keys = props.keys(); keys.hasMoreElements();) {
00301 String key = (String) keys.nextElement();
00302 if (key == null) {
00303 continue;
00304 }
00305
00306 if (key.startsWith(PropertiesConfAccess.HANDLER_FIELD)) {
00307 String temp = DottedStringTools.getFirst(key);
00308
00309 if (temp == null) {
00310 continue;
00311 }
00312
00313 temp = DottedStringTools.getEnd(key);
00314 if (temp == null) {
00315 continue;
00316 }
00317
00318 String handlerName = DottedStringTools.getBegin(temp);
00319
00320 String stringType = props.getProperty(PropertiesConfAccess.HANDLER_FIELD + PropertiesConfAccess.DOT + handlerName + PropertiesConfAccess.DOT + PropertiesConfAccess.HANDLER_TYPE_ATTRIBUTE, null);
00321 String stringOutput = props.getProperty(PropertiesConfAccess.HANDLER_FIELD + PropertiesConfAccess.DOT + handlerName + PropertiesConfAccess.DOT + HANDLER_OUTPUT_ATTRIBUTE, null);
00322
00323
00324 if ((stringType != null) && (stringOutput != null)) {
00325 if (PropertiesConfAccess.HANDLER_TYPE_ATTRIBUTE_FILE_VALUE.equalsIgnoreCase(stringType)
00326 || (PropertiesConfAccess.HANDLER_TYPE_ATTRIBUTE_ROLLING_FILE_VALUE.equalsIgnoreCase(stringType))) {
00327 if (stringOutput.equalsIgnoreCase(AUTOMATIC_CONFIG)) {
00328
00329 String fileName = null;
00330
00331 if (nbAut > 0) {
00332
00333 fileName = nameOfServer + date + PropertiesConfAccess.DOT + nbAut + PropertiesConfAccess.DOT + SUFFIX_LOGFILE;
00334 } else {
00335
00336 updatedProps = (Properties) props.clone();
00337 fileName = nameOfServer + date + PropertiesConfAccess.DOT + SUFFIX_LOGFILE;
00338 }
00339
00340
00341 if (logFirFile.exists()) {
00342 logFile = new File(logFirFile, fileName);
00343 } else {
00344
00345 logFile = new File(fileName);
00346 }
00347
00348 updatedProps.setProperty(PropertiesConfAccess.HANDLER_FIELD + PropertiesConfAccess.DOT + handlerName + PropertiesConfAccess.DOT + HANDLER_OUTPUT_ATTRIBUTE, logFile.getPath());
00349 nbAut++;
00350
00351 }
00352 else {
00353
00354 logFile = new File(stringOutput);
00355 if (!logFile.isAbsolute()) {
00356
00357 if (logFirFile.exists()) {
00358 logFile = new File(logFirFile, stringOutput);
00359 } else {
00360 logFile = new File(stringOutput);
00361 }
00362 if (nbAut == 0) {
00363 updatedProps = (Properties) props.clone();
00364 nbAut++;
00365 }
00366 if (stringOutput.endsWith(".automatic") ||
00367 stringOutput.endsWith(".auto")) {
00368 nbAut++;
00369 String autoStr = logFile.getPath().substring(0, logFile.getPath().lastIndexOf('.')) + date +
00370 PropertiesConfAccess.DOT + nbAut + PropertiesConfAccess.DOT + SUFFIX_LOGFILE;
00371 logFile = new File(autoStr);
00372 }
00373 updatedProps.setProperty(PropertiesConfAccess.HANDLER_FIELD + PropertiesConfAccess.DOT + handlerName + PropertiesConfAccess.DOT + HANDLER_OUTPUT_ATTRIBUTE, logFile.getPath());
00374 }
00375 }
00376 }
00377 }
00378 }
00379 }
00380
00381
00382 String b = props.getProperty("log.config.classname", null);
00383 if (b == null) {
00384 System.err.println("Malformed configuration log file: log.config.classname not available");
00385 return null;
00386 }
00387 lf = (LoggerFactory) Class.forName(b).newInstance();
00388
00389 if (updatedProps == null) {
00390 PropertiesConfAccess.load(props, lf, (HandlerFactory) lf, (LevelFactory) lf);
00391 } else {
00392 PropertiesConfAccess.load(updatedProps, lf, (HandlerFactory) lf, (LevelFactory) lf);
00393 }
00394
00395 Monolog.monologFactory = (MonologFactory) lf;
00396 } catch (Exception e) {
00397 System.err.println("Cannot get LoggerFactory:" + e);
00398 e.printStackTrace();
00399 }
00400 }
00401 return lf;
00402 }
00403
00407 private static boolean isClient() {
00408 boolean hasJonasBase = System.getProperty("jonas.base") != null;
00409 boolean hasJonasRoot = System.getProperty("install.root") != null;
00410 boolean hasJProp = true;
00411 try {
00412 Thread.currentThread().getContextClassLoader().loadClass(JPROP_CLASSNAME);
00413 } catch (ClassNotFoundException cnfe) {
00414 hasJProp = false;
00415 }
00416
00417 return !(hasJProp && hasJonasBase && hasJonasRoot);
00418 }
00419
00423 public static void reset() {
00424 lf = null;
00425 }
00426
00432 public static PrintWriter getLogWriter(String topic) {
00433
00434 return new PrintWriterImpl(getLogger(topic), getLoggerFactory());
00435 }
00436
00440 public static LevelFactory getLevelFactory() {
00441 return (LevelFactory) getLoggerFactory();
00442 }
00443
00449 public static Logger getLogger(String topic) {
00450 return getLoggerFactory().getLogger(topic);
00451 }
00452
00453 public static Properties getProperties() {
00454 return props;
00455 }
00456 }