gpt4 book ai didi

java - 无法找到 logback.xml

转载 作者:数据小太阳 更新时间:2023-10-29 02:29:59 28 4
gpt4 key购买 nike

我试图在我的简单程序中使用 logback 作为我的记录器,但它不能正常工作!我把logback/logback.xml和logback/Logback.java放在源码目录logback下,通过这个命令行运行

  • \logback>java -cp .;%CLASSPATH% 登录

其中 %CLASSPATH% 是一个环境变量,它具有 logback 需要的 .jar 文件的路径,例如:

  • logback-access-1.1.2.jar
  • logback-classic-1.1.2.jar
  • logback-core-1.1.2.jar
  • slf4j-api-1.7.6.jar

这是我的 logback.xml 文件

<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">

<file>test.log</file>

<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>tests.%i.log</fileNamePattern>
<minIndex>1</minIndex>
</rollingPolicy>

<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>2MB</maxFileSize>
</triggeringPolicy>

<encoder>
<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
</encoder>

</appender>

<root level="debug">
<appender-ref ref="FILE" />
</root>
</configuration>

还有我的简单程序

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Logback{
private final static Logger logger = LoggerFactory.getLogger(Logback.class);

public static void main(String[] args){
for(int i=0;i<1000000;i++)
logger.debug("hello");
}
}

但不幸的是,我只是在控制台中收到日志,而不是 test.log 文件。看起来记录器对象只使用默认配置!!!

如果我如下设置 -Dlogback.configurationFile=logback.xml 变量,它会正常工作。但是没有这个变量如何运行呢?

  • \logback>java -cp .;%CLASSPATH% -Dlogback.configurationFile=logback.xml 登录

最佳答案

来自 logback 文档:

1. Logback tries to find a file called logback.groovy in the classpath.
2. If no such file is found, logback tries to find a file called logback-test.xml in the classpath.
3. If no such file is found, it checks for the file logback.xml in the classpath..
4. If neither file is found, logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console.

logback.groovy、logback-test.xml 或 logback.xml 等配置文件应该放在类路径的什么位置?

Configuration files such as logback.groovy, logback-test.xml or logback.xml can be located directly under any folder declared in the class path. For example, if the class path reads "c:/java/jdk15/lib/rt.jar;c:/mylibs/" then the logback.xml file should be located directly under "c:/mylibs/", that is as "c:/mylibs/logback.xml". Placing it under a sub-folder of c:/mylibs/, say, c:/mylibs/other/, will not work.

For web-applications, configuration files can be placed directly under WEB-INF/classes/.

所以需要把logback.xml放在classpath中。在一个项目中,我们遇到了类似的问题,尽管 logback.xml 位于正确的位置。将其重命名为 logback-test.xml 有帮助。

关于java - 无法找到 logback.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26354163/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com