Currently when I execute my code I it doesn't create any log file. the logback.xml is configured fine, however I don't see a way to configure where to find the xml file
-
-Dlogback.configurationFile=./logback.xml **./logback.xml** this should be replaced by your path – Saran Nov 21 '17 at 13:43
-
1You don't **need** to configure it. Just make sure it's on the classpath. – Kayaman Nov 21 '17 at 13:44
-
this (-Dlogback.configurationFile=./logback.xml ./logback.xml) doesn't work for me, regarding the classpath: is this the path were the JDK is installed? – Dotan Raz Nov 21 '17 at 13:50
-
No. If you intend to keep programming Java, you should make sure you understand what the classpath is. It's a pretty important part of Java programming. – Kayaman Nov 21 '17 at 13:53
1 Answers
Per the Logback manual chapter on Configuration:
Let us begin by discussing the initialization steps that logback follows to try to configure itself:
- Logback tries to find a file called logback-test.xml in the classpath.
- If no such file is found, logback tries to find a file called logback.groovy in the classpath.
- If no such file is found, it checks for the file logback.xml in the classpath.
- If no such file is found, service-provider loading facility (introduced in JDK 1.6) is used to resolve the implementation of
com.qos.logback.classic.spi.Configurator
interface by looking up the file META-INF\services\ch.qos.logback.classic.spi.Configurator in the class path. Its contents should specify the fully qualified class name of the desiredConfigurator
implementation.- If none of the above succeeds, logback configures itself automatically using the
BasicConfigurator
which will cause logging output to be directed to the console.
The standard approach that this is trying to tell you about would be to have logback.xml be in the classpath for "normal" running, and have a logback-test.xml in the classpath to describe how you want to log when running automated tests. (For example, you may want to log to a file in your regular application, but have your automated unit tests just log to the console.) The exact process of putting a file into the classpath depends on what build system you're using. For example, with the default settings in the popular Maven build system, you would put logback.xml inside src/main/resources
, and (if desired) a logback-test.xml inside src/test/resources
. If you're having trouble with this step, you may want to search for or ask another question with more details about the build toolchain you're using. Also be sure to read "What is a classpath?"
Another approach, also listed in the Logback manual:
You may specify the location of the default configuration file with a system property named "
logback.configurationFile
". The value of this property can be a URL, a resource on the class path or a path to a file external to the application.java -Dlogback.configurationFile=/path/to/config.xml chapters.configuration.MyApp1
Note that the file extension must be ".xml" or ".groovy".
This wouldn't be as common, but sometimes if you need to run in a particular environment with a certain logging configuration, or run some sort of automated test directing output to a different place than your normal tests, it can come in handy to just outright configure the path like that.