简体   繁体   中英

how to make logback identify logback.xml?

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

Per the Logback manual chapter on Configuration :

Let us begin by discussing the initialization steps that logback follows to try to configure itself:

  1. Logback tries to find a file called logback-test.xml in the classpath.
  2. If no such file is found, logback tries to find a file called logback.groovy in the classpath.
  3. If no such file is found, it checks for the file logback.xml in the classpath.
  4. 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 desired Configurator implementation.
  5. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM