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
01 | package com.ho.log4j.test2; |
03 | import java.io.FileInputStream; |
04 | import java.io.InputStream; |
05 | import java.util.Properties; |
07 | import org.apache.log4j.LogManager; |
08 | import org.apache.log4j.PropertyConfigurator; |
12 | private final static String LOG4J_FILENAME = "log4jInjector.properties" ; |
13 | private final static String BUSINESS_LOG_FILE_KEY = "BUSINESS_LOG_FILE" ; |
14 | private final static String TECHNICAL_LOG_FILE_KEY = "TECHNICAL_LOG_FILE" ; |
15 | private static LogUtil logUtil = null ; |
21 | public static void main(String[] args){ |
24 | if (args== null || args.length== 0 ){ |
25 | System.out.println( "Usage : MainApp configfilepath" ); |
26 | System.out.println( "configfilepath : path to the configuration file config.properties containing the parameters" ); |
30 | Properties prop = new Properties(); |
33 | String filename = args[ 0 ]; |
34 | System.out.println( "********Loading of configuration file named " +filename+ "********" ); |
35 | input = new FileInputStream(filename); |
40 | InputStream log4ConfigStream = null ; |
42 | log4ConfigStream = LogUtil. class .getResourceAsStream(LOG4J_FILENAME); |
43 | Properties props = new Properties(); |
44 | props.load(log4ConfigStream); |
45 | props.setProperty( "log4j.appender.BUSINESSLOGFILE.File" , prop.getProperty(BUSINESS_LOG_FILE_KEY)); |
46 | props.setProperty( "log4j.appender.TECHNICALLOGFILE.File" , prop.getProperty(TECHNICAL_LOG_FILE_KEY)); |
47 | LogManager.resetConfiguration(); |
48 | PropertyConfigurator.configure(props); |
49 | } catch (Exception ex) { |
50 | ex.printStackTrace(System.out); |
51 | throw new RuntimeException( "Error in the LogUtil.init() method..." ); |
53 | if (log4ConfigStream != null ) { |
55 | log4ConfigStream.close(); |
56 | } catch (Exception ex) { |
57 | ex.printStackTrace(System.out); |
64 | logUtil = new LogUtil(); |
66 | logUtil.log( "************************** log trace 1 ************************" ); |
67 | logUtil.logError( "******** log trace 2 ERROR ********" ); |
71 | logUtil.logException(th); |
Config.properties
BUSINESS_LOG_FILE=D\:\\...\\... \\data\\results\\InjectorBusiness.log
TECHNICAL_LOG_FILE=D\:\\...\\...\\data\\results\\InjectorTechnical.log
LogUtil.java
01 | package com.ho.log4j.test2; |
03 | import org.apache.commons.lang3.exception.ExceptionUtils; |
08 | private org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogUtil. class ); |
14 | public void log(String str){ |
22 | public void logException(Throwable ex){ |
23 | Throwable th = ExceptionUtils.getRootCause(ex); |
31 | public void logError(String str){ |
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
Related