Log.java

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     // name of the config file - initialized by the configure method
00069     private static String configFile = "trace";
00070 
00074     private static LoggerFactory lf = null;
00075 
00080     private static Properties props = null;
00081 
00082     // List of topic used inside JOnAS
00083     // This list may be not exhaustive since many topics can be added for
00084     // different purposes or to refine debugging.
00085     // To get the complete list, use "jonas admin -t"
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             // Comment out call until JONAS uses Log4j
00212             // TraceTm.configure(lf);
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                 // Detect client case = no JONAS_BASE
00227                 if (isClient()) {
00228                     clientcontainer = true;
00229                     InputStream is = null;
00230 
00231                     // Is there a specific file to use instead the default name
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                         //Client
00242                         //Is there a trace properties file in the classpath ?
00243                         is = Thread.currentThread().getContextClassLoader().getResourceAsStream("traceclient.properties");
00244                     }
00245                     if (is == null) {
00246                         // no traceclient.properties found
00247                         // using a default configuration
00248                         props = new Properties();
00249                         props.put("log.config.classname", DEFAULT_LOGGERFACTORY);
00250                         props.put("logger.root.level", "INFO");
00251                     } else {
00252                         // use the configuration of the properties file.
00253                         props = new Properties();
00254                         props.load(is);
00255                     }
00256                     lf = Monolog.getMonologFactory(props);
00257                     return lf;
00258                 }
00259                 // server case
00260                 // get jonas configuration properties
00261                 Object jProp = null;
00262                 try {
00263                     jProp = JProp.getInstance();
00264                     // Run jonas
00265                     // Create a JProp object for the configFile
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                 // load properties from config file
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                     // Set default
00281                     jonasBase = ".";
00282                     nameOfServer = "jonas";
00283                 }
00284 
00285                 // Before sending props, replace AUTOMATIC pattern by a file in $JONAS_BASE/logs/
00286                 // File are named <JOnAS_server_name>TIMESTAMP_FORMAT.SUFFIX_LOGFILE
00287                 File logFirFile = new File(jonasBase + File.separator + LOG_DIRECTORY);
00288 
00289                 // Properties sent may be updated if AUTOMATIC_CONFIG is used
00290                 // Updated properties are in
00291                 Properties updatedProps = null;
00292 
00293                 //Create a date format
00294                 SimpleDateFormat sdf = new SimpleDateFormat(TIMESTAMP_FORMAT);
00295                 String date = sdf.format(new Date());
00296                 // How many AUTOMATIC field have been replaced
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                         // Got the type and the output
00323                         // --> Compare with the pattern AUTOMATIC_CONFIG for the FILE type.
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                                     // OK, it's match
00329                                     String fileName = null;
00330 
00331                                     if (nbAut > 0) {
00332                                         // Add an unique entry by adding nbAut value.
00333                                         fileName = nameOfServer + date + PropertiesConfAccess.DOT + nbAut + PropertiesConfAccess.DOT + SUFFIX_LOGFILE;
00334                                     } else {
00335                                         // this is the first occurence of AUTOMATIC_CONFIG, create updatedProps
00336                                         updatedProps = (Properties) props.clone();
00337                                         fileName = nameOfServer + date + PropertiesConfAccess.DOT + SUFFIX_LOGFILE;
00338                                     }
00339 
00340                                     // Log directory exists ?
00341                                     if (logFirFile.exists()) {
00342                                         logFile = new File(logFirFile, fileName);
00343                                     } else {
00344                                         // No $JONAS_BASE/logs directory, try current directory
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                                     // not automatic -- change relative file name to be relative to JONAS_BASE/logs
00354                                     logFile = new File(stringOutput);
00355                                     if (!logFile.isAbsolute()) {
00356                                         // file name is relative so try to create in JONAS_BASE/logs
00357                                         if (logFirFile.exists()) {  // Log directory exists ?
00358                                             logFile = new File(logFirFile, stringOutput);
00359                                         } else {                    // No $JONAS_BASE/logs directory, try current directory
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                 // Instanciate the LoggerFactory
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                 // Configure the LoggerFactory with the updated properties or the initial ones
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                 //Register the monolog factory in Monolog
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         // a server has the 3 params, all other case are clients case
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         // TODO : should not create a new object at each call
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 }

Generated on Tue Feb 15 15:05:14 2005 for JOnAS by  doxygen 1.3.9.1