Override log4j configuration programmatically: file location for FileAppender

Can I override the File property of an application configured in log4j.properties without creating a new application? And if so - how?

This is the situation: I have two attributes, A1 is ConsoleAppender, and A2 is FileAppender. A2 "File" indicates a common 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 these errors to be written to different files depending on which class caused the error, since there are several classes from which instances are created. Being able to see which class generated the error (s) quickly will be very useful, as it is much more useful, and then slips through error.log, which searches for class tags.

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

Many thanks!

+7
source share
2 answers

To change log4j properties at 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); } 
+12
source

Old question (well indexed on google). In addition to the OP requirement, adding additional methods that you read to manipulate log4j.properties


Change loaded 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("Errornot laod configuration file "); } props.setProperty("log4j.appender.FILE.file", logFile); LogManager.resetConfiguration(); PropertyConfigurator.configure(props); } 

Configuring log4j.properties at runtime

Can be done manually

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

Or by loading another 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 another file using the log4j.configuration VM option

 java -Dlog4j.configuration=file:///etc/myapp/properties/custom-log4j.properties 
  • if you select this option, it must be specified in the execution line
+8
source

All Articles