简体   繁体   中英

log4j.properties inside a jar file

I've made an component (lets Say SUBSCRIPTION jar) and exported it as jar file. This components includes log4j implementation and I am successfully able to generate the log file. (if ran independently).

But, when this jar (SUBSCRIPTION JAR) is used by other project/component, the log file that I mentioned (in log4j.properties of my component) is NOT getting generated.

How to do I make sure that log file generated, if my component is used by other project/component? (even if they implement log4j or not)

Here is my log4j.properties

log4j.rootLogger=INFO, stdout, logfile
log4j.logger.stdout=DEBUG, stdout
log4j.logger.logfile=DEBUG, logfile

log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.Threshold=INFO
log4j.appender.logfile.File=logs/subscritionLogFile.log
log4j.appender.logfile.DatePattern='.'yyyy-MM-dd
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5p] %c:%M:%L -%X{userLoginId} %m%n

Note: Using Apache Log4j framework

Use a custom named log4j.properties (subscriptionlog4j.properties) file in the component and make sure the component is loaded with custom log4j.properties file.

        try {
        InputStream input = SubscriptionController.class.getClassLoader().getResourceAsStream("subscriptionlog4j.properties");
        Properties prop = new Properties();
        prop.load(input);
        PropertyConfigurator.configure(prop);
    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("ERROR: Unable to load subscriptionlog4j.properties");
    }

Note: I placed the above code in static block, so that when ever my component loaded, my custom log4j properties is loaded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM