Hi,
After my previous posts concerning Java: Log4j Concepts and Explanations and Java: Log4j in practice, I would expose a solution to overwrite the file path on runtime.
Libraries needed : commons-lang3-3.1.jar, commons-logging-1.1.1.jar and log4j-1.2.15.jar.
MainApp.java
package com.ho.log4j.test2; import java.io.FileInputStream; import java.io.InputStream; import java.util.Properties; import org.apache.log4j.LogManager; import org.apache.log4j.PropertyConfigurator; public class MainApp { private final static String LOG4J_FILENAME = "log4jInjector.properties"; private final static String BUSINESS_LOG_FILE_KEY = "BUSINESS_LOG_FILE"; private final static String TECHNICAL_LOG_FILE_KEY = "TECHNICAL_LOG_FILE"; private static LogUtil logUtil = null; /** * Main * @param args */ public static void main(String[] args){ try{ if(args==null || args.length==0){ System.out.println("Usage : MainApp configfilepath"); System.out.println("configfilepath : path to the configuration file config.properties containing the parameters"); return; } Properties prop = new Properties(); { InputStream input; String filename = args[0]; System.out.println("********Loading of configuration file named "+filename+"********"); input = new FileInputStream(filename); prop.load(input); } { InputStream log4ConfigStream = null; try { log4ConfigStream = LogUtil.class.getResourceAsStream(LOG4J_FILENAME); Properties props = new Properties(); props.load(log4ConfigStream); props.setProperty("log4j.appender.BUSINESSLOGFILE.File", prop.getProperty(BUSINESS_LOG_FILE_KEY)); props.setProperty("log4j.appender.TECHNICALLOGFILE.File", prop.getProperty(TECHNICAL_LOG_FILE_KEY)); LogManager.resetConfiguration(); PropertyConfigurator.configure(props); } catch (Exception ex) { ex.printStackTrace(System.out); throw new RuntimeException("Error in the LogUtil.init() method..."); } finally { if (log4ConfigStream != null) { try { log4ConfigStream.close(); } catch (Exception ex) { ex.printStackTrace(System.out); } } } } // Initialisation du logger logUtil = new LogUtil(); logUtil.log("************************** log trace 1 ************************"); logUtil.logError("******** log trace 2 ERROR ********"); }catch(Throwable th){ if(logUtil !=null){ logUtil.logException(th); }else{ th.printStackTrace(); } } } }
Config.properties
BUSINESS_LOG_FILE=D\:\\...\\... \\data\\results\\InjectorBusiness.log
TECHNICAL_LOG_FILE=D\:\\...\\...\\data\\results\\InjectorTechnical.log
LogUtil.java
package com.ho.log4j.test2; import org.apache.commons.lang3.exception.ExceptionUtils; public class LogUtil { private org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogUtil.class); // ----------------------------------------------------------- CONSTRUCTOR public LogUtil() { } // ------------------------------------------------------ PUBLIC FUNCTIONS public void log(String str){ if(str !=null){ log.info(str); }else{ log.info(""); } } public void logException(Throwable ex){ Throwable th = ExceptionUtils.getRootCause(ex); if(th == null){ log.error(ex); }else{ log.error(th); } } public void logError(String str){ if(str !=null){ log.error(str); }else{ log.error(""); } } }
log4jInjector.properties
log4j.rootCategory=ALL,CONSOLE,TECHNICALLOGFILE
log4j.logger.com.ho.log4j.test2.LogUtil=INFO,BUSINESSLOGFILE
#------------------- CONSOLE --------------------------
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%p] %m%n
log4j.appender.CONSOLE.Threshold=INFO
#------------------- FILE --------------------------
log4j.appender.TECHNICALLOGFILE=org.apache.log4j.RollingFileAppender
log4j.appender.TECHNICALLOGFILE.File=injectortech.log
log4j.appender.TECHNICALLOGFILE.MaxFileSize=30MB
log4j.appender.TECHNICALLOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.TECHNICALLOGFILE.layout.ConversionPattern=%d{ABSOLUTE} %5p [%t] %c - %m%n
log4j.appender.TECHNICALLOGFILE.Threshold=ALL
#------------------- BUSINESSLOGFILE --------------------------
log4j.appender.BUSINESSLOGFILE=org.apache.log4j.DailyRollingFileAppender
#log4j.appender.BUSINESSLOGFILE.File=${catalina.base}/logs/Injector.log
log4j.appender.BUSINESSLOGFILE.File=injector.log
log4j.appender.BUSINESSLOGFILE.DatePattern='.'yyy-MM-dd-HH
log4j.appender.BUSINESSLOGFILE.MaxFileSize=30MB
log4j.appender.BUSINESSLOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.BUSINESSLOGFILE.Threshold=ALL
log4j.appender.BUSINESSLOGFILE.Append=true
log4j.appender.BUSINESSLOGFILE.layout.ConversionPattern=%-d{MMM dd HH:mm:ss} %-5p %c %M %m%n
Execute the MainApp class with the following program argument “C:\…\test_log4j\src\com\ho\log4j\test2\Config.properties”.
So the resuts would be the generation of log files named:
…InjectorBusiness.log:
oct. 04 00:06:32 INFO com.ho.log4j.test2.LogUtil log ************************** log trace 1 ************************
oct. 04 00:06:32 ERROR com.ho.log4j.test2.LogUtil logError ******** log trace 2 ERROR ********
…and InjectorTechnical.log:
00:06:32,074 INFO [main] com.ho.log4j.test2.LogUtil - ************************** log trace 1 ************************
00:06:32,078 ERROR [main] com.ho.log4j.test2.LogUtil - ******** log trace 2 ERROR ********
That’s all!!
Huseyin OZVEREN