简体   繁体   English

如何使logback标识logback.xml?

[英]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 logback.xml配置良好,但是我看不到配置在哪里找到xml文件的方法

Per the Logback manual chapter on Configuration : 根据有关配置Logback手册章节

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. Logback尝试在类路径中找到一个名为logback-test.xml的文件。
  2. If no such file is found, logback tries to find a file called logback.groovy in the classpath. 如果找不到此类文件,则logback会尝试在类路径中找到名为logback.groovy的文件。
  3. If no such file is found, it checks for the file logback.xml in the classpath. 如果找不到这样的文件,它将在类路径中检查文件logback.xml
  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. 如果找不到此类文件,则通过查找文件META-INF \\ services \\ ch ,使用服务提供程序加载工具(在JDK 1.6中引入)来解析com.qos.logback.classic.spi.Configurator接口的实现类路径中的qos.logback.classic.spi.Configurator Its contents should specify the fully qualified class name of the desired Configurator implementation. 它的内容应指定所需的Configurator实现的完全限定的类名。
  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. 如果以上方法均不能成功执行,则logback会使用BasicConfigurator自动进行自我配置,这会将日志输出定向到控制台。

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. 这试图告诉您的标准方法是将logback.xml放在类路径中以“正常”运行,并在类路径中包含logback-test.xml来描述您要如何在运行自动化测试时记录日志。 (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 . 例如,使用流行的Maven构建系统中的默认设置,您可以将logback.xml放在src/main/resources ,(如果需要)将logback-test.xml放在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: 另一种方法(也在Logback手册中列出):

You may specify the location of the default configuration file with a system property named " logback.configurationFile ". 您可以使用名为“ 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. 该属性的值可以是URL,类路径上的资源或应用程序外部文件的路径。

 java -Dlogback.configurationFile=/path/to/config.xml chapters.configuration.MyApp1 

Note that the file extension must be ".xml" or ".groovy". 请注意,文件扩展名必须为“ .xml”或“ .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. 这种情况并不常见,但是有时候如果您需要在具有特定日志配置的特定环境中运行,或者运行某种自动测试将输出定向到与正常测试不同的位置,则可能会很方便像这样直接配置路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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