[Batchserver-cvs] batchserver/src/org/jmonks/batchserver/framework LoggingManager.java,1.3,1.4 Main.
Brought to you by:
suresh_pragada
From: Suresh <sur...@us...> - 2006-03-14 05:46:37
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/framework In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30386 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.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** Main.java 13 Mar 2006 14:32:38 -0000 1.3 --- Main.java 14 Mar 2006 05:46:33 -0000 1.4 *************** *** 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.*; --- 3,7 ---- import java.util.HashMap; import java.util.Map; ! import org.apache.log4j.Logger; import org.jmonks.batchserver.framework.mgmtmntr.*; import org.jmonks.batchserver.framework.common.*; *************** *** 23,33 **** public class Main { ! private static Logger logger=null; ! ! static ! { ! LoggingManager.getInstance().initializeFrameworkLogging(); ! logger=Logger.getLogger(Main.class.getName()); ! } private MgmtMntrManager mMgmtMntrManager; --- 23,27 ---- public class Main { ! private static Logger logger=Logger.getLogger(Main.class); private MgmtMntrManager mMgmtMntrManager; *************** *** 60,66 **** public static ErrorCode process(Map configMap) { ! logger.entering(Main.class.getName(),"process"); ! logger.exiting(Main.class.getName(),"process"); return null; } --- 54,60 ---- public static ErrorCode process(Map configMap) { ! logger.trace("Entering process"); ! logger.trace("Exiting process"); return null; } Index: LoggingManager.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/framework/LoggingManager.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** LoggingManager.java 13 Mar 2006 14:32:38 -0000 1.3 --- LoggingManager.java 14 Mar 2006 05:46:33 -0000 1.4 *************** *** 4,15 **** 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.*; --- 4,12 ---- import java.text.SimpleDateFormat; import java.util.Calendar; ! import org.apache.log4j.Level; ! import org.apache.log4j.LogManager; ! import org.apache.log4j.Logger; ! import org.apache.log4j.PatternLayout; ! import org.apache.log4j.RollingFileAppender; import org.jmonks.batchserver.framework.config.*; *************** *** 23,27 **** * The default logging mechanism defined at the framework will * work like this. It reads the directory to create the log files from ! * the framework logging configuration <logging-directory> and creates a directory * with the "job-name" and create the log files for each run. So, the format * will be --- 20,24 ---- * The default logging mechanism defined at the framework will * work like this. It reads the directory to create the log files from ! * the framework logging configuration <logging-config> and creates a directory * with the "job-name" and create the log files for each run. So, the format * will be *************** *** 31,36 **** * * If job developer would like to override this logging mechanism, they can ! * define their logging configuration in the element <job-config> in batch ! * configuration file. * </p> * @author Suresh Pragada --- 28,33 ---- * * If job developer would like to override this logging mechanism, they can ! * define their logging configuration in the element <job-logging-config> in batch ! * configuration file or defining entries in job_logging_config table. * </p> * @author Suresh Pragada *************** *** 40,53 **** 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() --- 37,48 ---- public class LoggingManager { /** ! * Framework configuration file. */ ! public static final String FRAMEWORK_LOGGING_FILE = "batch_framework.log"; /** ! * Standard pattern expression used for all the log files. ! */ ! public static final String DEFAULT_MESSAGE_PATTERN = "%-5p [%t]: %m%n"; private LoggingManager() *************** *** 55,164 **** } - 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) --- 50,151 ---- } /** * <p> * This method accepts all the information available from framwork logging configuration ! * and job 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_ms.log ! * <li>Creates the Rolling File appender with the above created directory and logging file name. ! * <li>Remove all the existing appenders from the root logger. ! * <li>Attaches the above created appender for the root logger. ! * <li>Access the defined loggers at job level and set the level for those loggers. * </ul> * ! * @param jobName Name of the job. ! * @param frameworkLoggingConfig Logging configuration defined at the framework. ! * @param jobLoggingFramework Logging configuration defined at the job level. * ! * @throws IllegalArgumentException If input values jobName and frameworkLoggingConfig are null. * @throws ConfigurationException If logging file cannot be created at the configured directory. * </p> */ ! public static void initializeJobLogging(String jobName, FrameworkConfig.LoggingConfig frameworkLoggingConfig,JobLoggingConfig jobLoggingConfig) { if(jobName==null) ! throw new IllegalArgumentException("job name cannot be null to establish the job logging service."); ! if(frameworkLoggingConfig==null) ! throw new IllegalArgumentException("framework logging config object cannot be null to establish the job logging service."); ! File loggingDirecotry=new File(frameworkLoggingConfig.getLoggingDirecotry()+File.separator+jobName); ! LoggingManager.createDirectory(loggingDirecotry); ! String completeFileName=loggingDirecotry.getAbsolutePath()+File.separator+ ! jobName+"_"+new SimpleDateFormat("yyyyMMdd_HHmmss_S").format(Calendar.getInstance().getTime())+"_.log"; ! LoggingManager.addAppender(completeFileName); ! if(jobLoggingConfig!=null) { ! JobLoggingConfig.JobLoggerConfig jobLoggerConfigs[]=jobLoggingConfig.getLoggers(); ! for(int i=0;i<jobLoggerConfigs.length;i++) { ! Logger customLogger=Logger.getLogger(jobLoggerConfigs[i].getLoggerName()); ! if(customLogger!=null) ! customLogger.setLevel(Level.toLevel(jobLoggerConfigs[i].getLoggerLevel())); } } ! } ! ! /** ! * Initializes the logging of the framework in the directory defined in framework configuration. ! * This will be used until the job logging is enabled. ! * ! * @param frameworkLoggingConfig Framework logging configuration object. ! * ! * @throws ConfigurationException If logging directory cannot be created. ! */ ! public static void initializeFrameworkLogging(FrameworkConfig.LoggingConfig frameworkLoggingConfig) ! { ! File loggingDirecotry=new File(frameworkLoggingConfig.getLoggingDirecotry()); ! LoggingManager.createDirectory(loggingDirecotry); ! String completeFileName=loggingDirecotry.getAbsolutePath()+File.separator+LoggingManager.FRAMEWORK_LOGGING_FILE; ! LoggingManager.addAppender(completeFileName); ! } ! /** ! * Method to make sure given file entry exist. If it doenst exist, it will tries to create that ! * entry. ! */ ! private static void createDirectory(File loggingDirecotry) ! { ! if(loggingDirecotry.exists() && loggingDirecotry.isFile()) ! throw new ConfigurationException(ConfigurationException.LOGGING_CONFIG, "Logging directory " + ! loggingDirecotry.getAbsolutePath() + " defined in framework configuration is a file."); ! else if(!loggingDirecotry.exists()) { ! boolean created=loggingDirecotry.mkdirs(); ! if(!created) ! throw new ConfigurationException(ConfigurationException.LOGGING_CONFIG, "Logging directory " + ! loggingDirecotry.getAbsolutePath() + " defined in framework configuration cannot be created."); } } /** ! * Common method creates the rolling file appender with the given name and attaches it ! * to the root logger after creating all the existing loggers. ! * ! * @param fileName Name of the file to be attached to the appender. */ ! private static void addAppender(String fileName) { try { ! PatternLayout layout=new PatternLayout(LoggingManager.DEFAULT_MESSAGE_PATTERN); ! RollingFileAppender fileAppender=new RollingFileAppender(layout,fileName,true); ! fileAppender.setMaxFileSize("10MB"); ! fileAppender.activateOptions(); ! Logger rootLogger=LogManager.getRootLogger(); ! rootLogger.removeAllAppenders(); ! rootLogger.addAppender(fileAppender); } catch(IOException exception) *************** *** 166,184 **** 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); ! } } } --- 153,157 ---- exception.printStackTrace(); } ! } } |