maven 如何测试logback是否从logback.xml中获取配置

cyvaqqii  于 2023-04-20  发布在  Maven
关注(0)|答案(1)|浏览(180)

我已经编写了一个简短的测试代码来试验Java日志框架Logback。
但是,Logback似乎没有从xml文件中获取配置。
下面是应用程序代码:

package com.myname.logbackexample;

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

public class App 
{

    private static final Logger logger = LoggerFactory.getLogger(App.class);
    private static final Logger anotherLogger = LoggerFactory.getLogger("anotherLogger");
    private static final Logger fileLogger = LoggerFactory.getLogger("filelog");

    public static void main(String[] args)
    {
        System.out.println("Hello World from System.out");

        logger.debug("Hello world from Logback");
        logger.debug("getNumber() : {}", getNumber());

        logger.warn("This is a warning");
        logger.error("This is an error");
        logger.info("This is info");
        logger.trace("This is trace");

        anotherLogger.debug("Debug from anotherLogger");

        fileLogger.debug("Debug log to file from fileLogger -> filelog");
    }

    static int getNumber() {
        return 10;
    }
}

下面是我的配置文件logback.xml

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

    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
            </Pattern>
        </layout>
    </appender>

    <appender name="simpleConsole" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %-5level %logger{36}: %msg%n
            </Pattern>
        </layout>
    </appender>

    <logger name="com.myname" level="debug" additivity="false">
        <appender-ref ref="simpleConsole"/>
    </logger>

    <logger name="com.myname.logbackexample.App" level="warn" additivity="false">
        <appender-ref ref="console"/>
    </logger>

    <logger name="anotherLogger" level="error" additivity="false">
        <appender-ref ref="simpleConsole"/>
    </logger>

    <root level="error">
        <appender-ref ref="console"/>
    </root>

</configuration>

这是我打包并运行这段代码时产生的输出:

$ java -jar target/logback-example-1.0-SNAPSHOT-jar-with-dependencies.jar
Hello World from System.out
11:13:32.504 [main] DEBUG com.myname.logbackexample.App -- Hello world from Logback
11:13:32.507 [main] DEBUG com.myname.logbackexample.App -- getNumber() : 10
11:13:32.508 [main] WARN com.myname.logbackexample.App -- This is a warning
11:13:32.508 [main] ERROR com.myname.logbackexample.App -- This is an error
11:13:32.508 [main] INFO com.myname.logbackexample.App -- This is info
11:13:32.508 [main] DEBUG anotherLogger -- Debug from anotherLogger
11:13:32.508 [main] DEBUG filelog -- Debug log to file from fileLogger -> filelog

最后,我的pom.xml

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

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.myname.logbackexmaple</groupId>
  <artifactId>logback-example</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>logback-example</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    <maven.compiler.release>17</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.4.6</version>
    </dependency>

  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
          <executions>
            <execution>
              <id>default-jar</id>
              <phase>none</phase>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>3.1.1</version>
          <executions>
            <execution>
              <phase>package</phase>
              <goals>
                <goal>single</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>

    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.0.2</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>com.myname.logbackexample.App</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>

      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <mainClass>com.myname.logbackexample.App</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>

    </plugins>
  </build>
</project>

我希望得到不同的日志级别和输出格式,这取决于哪个日志记录器正在记录。
这似乎并不像我所期望的那样发生。
例如,anotherLogger应该设置为error的日志级别,但它会在调试级别生成输出。
如何测试配置文件logback.xml在运行时是否被代码读取和处理?

cgyqldqp

cgyqldqp1#

以下是如何在运行时打印当前配置:
在代码中添加以下行。

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.util.StatusPrinter;

...

LoggerContext loggerContext = (LoggerContext)LoggerFactory.getILoggerFactory();
StatusPrinter.print(loggerContext);

例如,如果未找到配置文件,它将打印如下内容:

11:37:34,150 |-INFO in ch.qos.logback.classic.LoggerContext[default] - This is logback-classic version ?
11:37:34,173 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
11:37:34,173 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.xml]
11:37:34,174 |-INFO in ch.qos.logback.classic.BasicConfigurator@7637f22 - Setting up default configuration.

相关问题