0

I am using slf4j to manage my application logging.

I want to change the name of the output file after configuring the log4j.properties. I mean, the file name is a parameter that enters into my program and is variable. I want to change this slf4j property from java.

Does anyone know if it's possible?

Thanks in advance!

gcotis
  • 107
  • 2
  • 15
  • Have you looked for property substitution, http://logging.apache.org/log4j/2.0/manual/configuration.html " Log4j 2 supports the ability to specify tokens in the configuration as references to properties defined elsewhere. Some of these properties will be resolved when the configuration file is interpreted while others may be passed to components where they will be evaluated at runtime. To accomplish this, Log4j uses variations of Apache Commons Lang's StrSubstitutor and StrLookup classes".... – mawia Sep 09 '13 at 08:59

1 Answers1

3

Finally I solved the problem:

public void updateFichierLogNames(String warnFileName, String infoFileName, String errorFileName) {
        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.infoFile.File", infoFileName); 
    props.setProperty("log4j.appender.warnFile.File", warnFileName); 
    props.setProperty("log4j.appender.errorFile.File", errorFileName); 

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

    }
}
gcotis
  • 107
  • 2
  • 15