gpt4 book ai didi

JAVA:在独立应用程序中配置 logback

转载 作者:行者123 更新时间:2023-12-02 12:17:21 26 4
gpt4 key购买 nike

我有一个 Java 独立应用程序。使用 main 方法,使用这 2 个导入:

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

private static final Logger log = LoggerFactory.getLogger(PecadorDeLaPradera.class);

在同一个文件夹中,我还有一个 logback.xml 但我不知道如何告诉使用 logback.xml 的程序来配置日志

我找不到任何类似于org.apache.log4j.PropertyConfigurator的类

我在运行的 Java 类的同一文件夹中有这个 logback.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<!-- trace, debug, info, warn, error, fatal -->
<timestamp key="myTimestamp" datePattern="yyyy-MM-dd'_'HH-mm-ss.SSS"/>


<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
<resetJUL>true</resetJUL>
</contextListener>

<!-- To enable JMX Management -->
<jmxConfigurator/>

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{"yyyy-MM-dd HH:mm"} [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>pecador.log</file>

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

<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>1MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%d{"yyyy-MM-dd HH:mm"} [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>



<!-- <logger name="org.springframework.orm.jpa" level="debug" /> -->
<logger name="com.calzada.pecador" level="debug" />


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

我还运行了该应用程序。带有程序参数:

-Dlogback.configurationFile=/Users/calzada/Dev/J2EE/eclipseWSJ2EE/myApp/src/com/calzada/pecador/logback.xml

我得到了这个错误:

java.util.MissingResourceException: Can't find bundle for base name -Dlogback.configurationFile=/Users/nullpointer/Development/J2EE/eclipseWSJ2EE/myApp/src/com/calzada/pecador/logback.xml, locale en_ES

使用的库是logback-classic-1.2.3.jar

最佳答案

这是 logback 如何配置自身的:

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 desired Configurator 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.

您还可以使用系统参数 logback.configurationFile 将 Logback 指向特定配置文件。 。来自文档:

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

以上内容均取自docs .

根据您的问题,您有 logback.xml但该文件与您的类(class)位于“同一文件夹”中。除非您的类位于根包中,否则这意味着 logback.xml不在类路径的根目录中,因此 Logback 不会发现它。为了让 Logback 通过该文件配置自身,您可以执行以下操作之一:

  • 将 logback.xml 放在类路径的根目录中(对于 Maven 项目,这就像将 logback.xml 复制到 src/main/resources 一样简单)
  • 使用 -Dlogback.configurationFile=/path/to/logback.xml 运行您的 Java 程序

关于JAVA:在独立应用程序中配置 logback,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46057356/

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