0

I have question with regard to changing the name of the log file of the slf4j logger. In the logback.xml file this is set to myApp.log. I would like to to be able to change this filename each time I run my code in Java, where the name of the file is a combination of the settings for which I run the problem. As you can imagine, I rather have some code for this in Java, instead of each time changing this manually in the logback.xml file.

Change the name of a logging output file from Java in SLF4j

I have seen the thread in the link above, however this does seem to work for me. I currently implemented this as follows

    /* Configure parameters */
    Properties properties = new Properties();
    Configuration.readFromFile(properties);
    properties.setProperty("MAXTHREADS", "1");
    properties.setProperty("EXPORT_MODEL", "false");
    properties.setProperty("log4j.appender.debugFile.File", "test.log");
    properties.setProperty("log4j.appender.infoFile.File", "test2.log");
    properties.setProperty("log4j.appender.warnFile.File", "test3.log");
    properties.setProperty("log4j.appender.errorFile.File", "test4.log");

If I run it with these settings, the output is still saved in myApp.log.

Any suggestions are welcome!

Student NL
  • 409
  • 6
  • 16
  • 1
    Does this answer your question? [Logback - set log file name programmatically](https://stackoverflow.com/questions/7824620/logback-set-log-file-name-programmatically) – Julien Feb 26 '20 at 09:59

1 Answers1

0

You could use a sifting appender and just

MDC.put("myRun", "runXXXX");

before running your code.

Something like this in your logback.xml:

  <appender name="MapiContextSplit" class="ch.qos.logback.classic.sift.SiftingAppender">
    <discriminator>
      <key>myRun</key>
      <defaultValue>default</defaultValue>
    </discriminator>
    <sift>
      <appender name="FILE-${myRun}" class="ch.qos.logback.core.rolling.RollingFileAppender">
                 <file>/var/log/test-${myRun}.log</file>
      </appender>
    </sift>
  </appender>