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

JAVA: Configure logback in standalone apps

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

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

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

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

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

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

<?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 在“同一文件夹”中。除非您的 class 在根包中,否则这意味着 logback.xml 不在 class 路径的根目录中,因此 Logback 不会发现它。为了让 Logback 从该文件配置自身,您可以执行以下操作之一:

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