2

My Java application uses SLF4J which I have configured to use the SimpleLogger implementation and redirect log messages to a file. That is working fine.

How do I subsequently change the name of the log file?

I have tried changing the LOG_FILE_KEY property but it seems to have no effect. The log messages continue to be output to the original log file.

This is what I've done:

System.setProperty(org.slf4j.impl.SimpleLogger.LOG_FILE_KEY, Paths.get("new-filename.txt").toString());
ksl
  • 4,519
  • 11
  • 65
  • 106

2 Answers2

2

To set the file path to your liking, one simply has to set the following property, either in a file named simplelogger.properties on the classpath (e.g. by placing such a file under the resources directory) or through a -D JVM startup option.

The property name and syntax is simply:

org.slf4j.simpleLogger.logFile=your-file-path

See in this related answer for examples of providing a property through either of the two mentioned methods.

Community
  • 1
  • 1
matanster
  • 15,072
  • 19
  • 88
  • 167
  • How can make SimpleLogger to read simplelogger.properties file from outside the classpath i.e. I want to put this properties file outside the artifact at some system path. – Jaraws Mar 09 '22 at 17:57
1

I think I figured out the answer with the help of this answer and looking at the source code for org.slf4j.impl.SimpleLogger.

The answer is you can't create a new log file as the logger properties are loaded only once - upon construction of the first logger instance. Subsequent loggers will use the same properties as the first one.

Community
  • 1
  • 1
ksl
  • 4,519
  • 11
  • 65
  • 106