[Batchserver-cvs] batchserver/src/org/jmonks/batchserver/framework LoggingManager.java,1.2,1.3 Main.
Brought to you by:
suresh_pragada
From: Suresh <sur...@us...> - 2006-03-13 14:32:41
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/framework In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19442 Modified Files: LoggingManager.java Main.java Log Message: no message Index: Main.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/framework/Main.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Main.java 4 Mar 2006 04:48:52 -0000 1.2 --- Main.java 13 Mar 2006 14:32:38 -0000 1.3 *************** *** 3,6 **** --- 3,7 ---- import java.util.HashMap; import java.util.Map; + import java.util.logging.Logger; import org.jmonks.batchserver.framework.mgmtmntr.*; import org.jmonks.batchserver.framework.common.*; *************** *** 22,27 **** --- 23,36 ---- public class Main { + private static Logger logger=null; + static + { + LoggingManager.getInstance().initializeFrameworkLogging(); + logger=Logger.getLogger(Main.class.getName()); + } + private MgmtMntrManager mMgmtMntrManager; + public Main() { *************** *** 34,48 **** * <br><br> * <pre> - * import org.jmonks.batchserver.framework.Main; - * import java.util.Map; - * import java.util.HashMap(); - * * Map configMap=new HashMap(); * configMap.put("job-name","process_file_abc"); * configMap.put("config-name1","config-value1"); * configMap.put("config-name2","config-value2"); ! * Main main=new Main(); ! * int errorCode=main.process(configMap); ! * System.out.println("Job exited with return code : " + errorCode); * </pre> * <br><br> --- 43,53 ---- * <br><br> * <pre> * Map configMap=new HashMap(); * configMap.put("job-name","process_file_abc"); * configMap.put("config-name1","config-value1"); * configMap.put("config-name2","config-value2"); ! * ! * ErrorCode statusCode=Main.process(configMap); ! * System.out.println("Job exited with return code : " + errorCode.getErrorCode()); * </pre> * <br><br> *************** *** 53,65 **** * @return Returns zero for success and non-zero for failure. */ ! public org.jmonks.batchserver.framework.common.StatusCode process(Map configMap) ! ! ! { return null; - - - } --- 58,67 ---- * @return Returns zero for success and non-zero for failure. */ ! public static ErrorCode process(Map configMap) { + logger.entering(Main.class.getName(),"process"); + + logger.exiting(Main.class.getName(),"process"); return null; } *************** *** 88,93 **** **/ Main main=new Main(); ! StatusCode statusCode=main.process(configMap); ! System.exit(statusCode.getCode()); } --- 90,95 ---- **/ Main main=new Main(); ! ErrorCode errorCode=main.process(configMap); ! System.exit(errorCode.getCode()); } Index: LoggingManager.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/framework/LoggingManager.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** LoggingManager.java 4 Mar 2006 04:48:52 -0000 1.2 --- LoggingManager.java 13 Mar 2006 14:32:38 -0000 1.3 *************** *** 1,3 **** --- 1,15 ---- package org.jmonks.batchserver.framework; + import java.io.File; + import java.io.IOException; + import java.text.SimpleDateFormat; + import java.util.Calendar; + import java.util.Enumeration; + import java.util.logging.FileHandler; + import java.util.logging.Formatter; + import java.util.logging.Handler; + import java.util.logging.Level; + import java.util.logging.LogManager; + import java.util.logging.Logger; + import java.util.logging.SimpleFormatter; import org.jmonks.batchserver.framework.config.*; *************** *** 26,56 **** * @since 1.0 */ ! public class LoggingManager { ! private static LoggingManager loggingManager = new LoggingManager(); private LoggingManager() { } ! public static LoggingManager getInstance() ! { ! return null; } /** * <p> ! * This method accepts all the information available from framwork configuration ! * and job configuration and loads the required loggers appropriately. If jobLoggingConfig ! * is null, it uses frameworkLoggingConfig to initialize the logging. * * @param jobName Name of the job. ! * @param frameworkLoggingConfig Logging configuration defined at the framework. ! * @param jobLogginConfig Logging configuration defined at the job. * </p> */ ! public void initializeLogging(String jobName, org.jmonks.batchserver.framework.config.FrameworkConfig.LoggingConfig loggingConfig) { } } --- 38,184 ---- * @since 1.0 */ ! public class LoggingManager ! { private static LoggingManager loggingManager = new LoggingManager(); + + /** + * Holds the path of the file framework logging handler is using. + */ + private String frameworkLoggingFileName=null; + /** + * Holds the path of the file job logging handler is using. + */ + private String jobLoggingFileName=null; + private LoggingManager() { } ! public static LoggingManager getInstance() { ! return loggingManager; } /** * <p> ! * This method accepts all the information available from framwork logging configuration ! * and loads the required loggers appropriately. This does the follwing steps. ! * <ul> ! * <li>Create the directory <directory name configured in logging config>/jobName ! * <li>Generates the logging file name jobName_yyyymmdd_hhmmss.log ! * <li>Creates the File handler with the above created directory and logging file name. ! * <li>Looks for the framework logger and removes the existing file handler and adds the ! * above created file handler. ! * <li>Creates the logger for the base package name provided and add the above file handler. ! * <li>Make sure these two loggers have been added to the LogManager. ! * </ul> * * @param jobName Name of the job. ! * @param loggingConfig Logging configuration defined at the framework. ! * ! * @throws IllegalArgumentException If input values are null. ! * @throws ConfigurationException If logging file cannot be created at the configured directory. * </p> */ ! public void initializeJobLogging(String jobName, org.jmonks.batchserver.framework.config.FrameworkConfig.LoggingConfig loggingConfig) { + if(jobName==null) + throw new IllegalArgumentException("job name cannot be null to establish the framework logging service."); + + if(loggingConfig==null) + throw new IllegalArgumentException("logging config object cannot be null to establish the framework logging service."); + + String loggingDirectoryName=loggingConfig.getLoggingDirecotry(); + if(loggingDirectoryName!=null) + { + File loggingDirectory=new File(loggingDirectoryName+File.separator+jobName); + if(!loggingDirectory.exists()) + { + boolean created=loggingDirectory.mkdirs(); + if(!created) + throw new ConfigurationException(ConfigurationException.LOGGING_CONFIG,"Unable to create the directory " + loggingDirectoryName + "."); + } + } + else + throw new ConfigurationException(ConfigurationException.LOGGING_CONFIG,"logging directory in framework logging config cannot be null to establish the framework logging service."); + + String logFileName=jobName+"_"+new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime())+".log"; + this.jobLoggingFileName=loggingDirectoryName+File.separator+jobName+File.separator+logFileName; + Handler jobLoggingHandler=null; + try + { + jobLoggingHandler=new FileHandler(this.jobLoggingFileName); + jobLoggingHandler.setLevel(Level.ALL); + Formatter jobLoggingFormatter=new SimpleFormatter(); + jobLoggingHandler.setFormatter(jobLoggingFormatter); + } + catch(IOException exception) + { + exception.printStackTrace(); + throw new ConfigurationException(ConfigurationException.LOGGING_CONFIG, "Exception while creating the file handler with the name " + + this.jobLoggingFileName + ". Exceptions Message is : " +exception.getMessage()); + } + + Logger frameworkLogger=LogManager.getLogManager().getLogger("org.jmonks"); + if(frameworkLogger==null) + { + frameworkLogger=Logger.getLogger("org.jmonks"); + LogManager.getLogManager().addLogger(frameworkLogger); + } + else + { + Handler[] handlers=frameworkLogger.getHandlers(); + for(int i=0;i<handlers.length;i++) + frameworkLogger.removeHandler(handlers[i]); + } + frameworkLogger.addHandler(jobLoggingHandler); + frameworkLogger.setLevel(Level.parse(loggingConfig.getFrameworkLogLevel())); + + Logger jobLogger=Logger.getLogger(loggingConfig.getBasePackageName()); + jobLogger.setLevel(Level.parse(loggingConfig.getFrameworkLogLevel())); + jobLogger.addHandler(jobLoggingHandler); + LogManager.getLogManager().addLogger(jobLogger); + } + /** + * Initializes the logging of the framework in the user home directory. + * This will be used untile the job logging is enabled. + */ + public void initializeFrameworkLogging() + { + try + { + String userHomeDirectory=System.getProperty("user.home"); + this.frameworkLoggingFileName=userHomeDirectory+File.separator+"batch_framework.log"; + + Logger initialFrameworkLogger=Logger.getLogger("org.jmonks"); + initialFrameworkLogger.setLevel(Level.CONFIG); + Handler frameworkLoggignHandler=new FileHandler(this.frameworkLoggingFileName,true); + frameworkLoggignHandler.setLevel(Level.ALL); + Formatter frameworkLoggingFormatter=new SimpleFormatter(); + frameworkLoggignHandler.setFormatter(frameworkLoggingFormatter); + initialFrameworkLogger.addHandler(frameworkLoggignHandler); + LogManager.getLogManager().addLogger(initialFrameworkLogger); + } + catch(IOException exception) + { + exception.printStackTrace(); + } + } + + /** + * Changes the logging level of all the existing logger to the requested level. + * + * @param logLevel level of the logging needed. + */ + public void changeLoggingLevel(Level logLevel) + { + Enumeration loggerNameEnumeration=LogManager.getLogManager().getLoggerNames(); + while(loggerNameEnumeration.hasMoreElements()) + { + Logger logger=LogManager.getLogManager().getLogger((String)loggerNameEnumeration.nextElement()); + logger.setLevel(logLevel); + } + } } |