override log4j configuration programmatically: fil

2020-02-12 04:58发布

问题:

is it possible to override the "File" property of an appender that has been configured in the log4j.properties without creating a new appender? And if so - how?

This is the situation: I have two apenders, A1 is a ConsoleAppender and A2 is a FileAppender. A2's "File" points a generic error.log:

log4j.appender.A2.File=error.csv

This appender only logs error-level events or worse through

log4j.appender.A2.Threshold=error.

Now I want those errors to be written in different files depending on which class caused the error, as there are several classes that instances are being created of. Being able to see which class created the error(s) fast would be of great help, as it is a lot more helpful then skimming through the error.log looking for the class-tags.

So my idea was to override the "File" property e.g. in the constructors of these newly created classes, so they log errors in different files.

Thanks a lot in advance!

回答1:

For changing log4j properties on runtime visit this link

http://alperkaratepe.wordpress.com/2010/01/16/how-to-change-log4j-properties-at-runtime/

 private void updateLog4jConfiguration(String logFile) { 
    Properties props = new Properties(); 
    try { 
        InputStream configStream = getClass().getResourceAsStream( "/log4j.properties"); 
        props.load(configStream); 
        configStream.close(); 
    } catch (IOException e) { 
        System.out.println("Error: Cannot laod configuration file "); 
    } 
    props.setProperty("log4j.appender.FILE.file", logFile); 
    PropertyConfigurator.configure(props); 
 }


回答2:

Old question (well indexed in google). In addition to OP's requirement, adding additional methods iv'e read about to manipulate log4j.properties


Modify loaded log4j.properties in runtime

private void updateLog4jConfiguration(String logFile) { 
    Properties props = new Properties(); 
    try { 
        InputStream configStream = getClass().getResourceAsStream( "/log4j.properties"); 
        props.load(configStream); 
        configStream.close(); 
    } catch (IOException e) { 
        System.out.println("Errornot laod configuration file "); 
    } 
    props.setProperty("log4j.appender.FILE.file", logFile); 
    LogManager.resetConfiguration(); 
    PropertyConfigurator.configure(props); 
}
  • full example is in this article

Setting log4j.properties in runtime

Can be done manually

Properties properties = new Properties();
properties.setProperty("log4j.logger.org.hibernate", "ERROR");
// ...

LogManager.resetConfiguration();
PropertyConfigurator.configure(properties);

Or by loading a different properties file

Properties properties = new Properties();
properties.load(new FileInputStream("/etc/myapp/properties/custom-log4j.properties"));
LogManager.resetConfiguration();
PropertyConfigurator.configure(properties);

VM Option

You can tell log4j to load a different file using log4j.configuration VM option

java -Dlog4j.configuration=file:///etc/myapp/properties/custom-log4j.properties
  • if you choose this option, it must be provided in the execution line