我正在尝试在 Vertx 项目上添加 Logback,但似乎 SLF4J 无法看到 logback,即使它本身来自 logback 依赖项。我使用了 Maven 依赖项 logback-classic,它可以传递导入 slf4j-api 和 logback-core (预期的提供程序)。
当我使用随机日志运行一个简单的主类时,出现以下错误:
SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See https://www.slf4j.org/codes.html#noProviders for further details.
我的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.example</groupId>
<artifactId>vertx-stock-broker</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<maven-shade-plugin.version>3.2.4</maven-shade-plugin.version>
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
<exec-maven-plugin.version>3.0.0</exec-maven-plugin.version>
<vertx.version>4.5.11</vertx.version>
<logback-classic.version>1.5.12</logback-classic.version>
<junit-jupiter.version>5.9.1</junit-jupiter.version>
<main.verticle>com.example.vertx_stock_broker.MainVerticle</main.verticle>
<launcher.class>io.vertx.core.Launcher</launcher.class>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-stack-depchain</artifactId>
<version>${vertx.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback-classic.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<release>17</release>
</configuration>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven-shade-plugin.version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>${launcher.class}</Main-Class>
<Main-Verticle>${main.verticle}</Main-Verticle>
</manifestEntries>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
<outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar
</outputFile>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>${exec-maven-plugin.version}</version>
<configuration>
<mainClass>${launcher.class}</mainClass>
<arguments>
<argument>run</argument>
<argument>${main.verticle}</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
跑步班:
package com.example.vertx_stock_broker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Test {
private static final Logger LOG = LoggerFactory.getLogger(Test.class);
public static void main(String[] args) {
LOG.debug("debug");
LOG.info("info");
LOG.error("error");
}
}
我使用 Intellij 运行工具运行它,该工具默默地运行(我用“...”缩短了路径):
openjdk-22.0.2\bin\java.exe "-javaagent:...\IntelliJ IDEA 2024.2.3\lib\idea_rt.jar=64366:...\IntelliJ IDEA 2024.2.3\bin"
-Dfile.encoding=UTF-8
-Dsun.stdout.encoding=UTF-8
-Dsun.stderr.encoding=UTF-8
-classpath ...\vertx-stock-broker\target\classes;
....m2\repository\io\vertx\vertx-web\4.5.11\vertx-web-4.5.11.jar;
....m2\repository\io\vertx\vertx-web-common\4.5.11\vertx-web-common-4.5.11.jar;
....m2\repository\io\vertx\vertx-auth-common\4.5.11\vertx-auth-common-4.5.11.jar;
....m2\repository\io\vertx\vertx-bridge-common\4.5.11\vertx-bridge-common-4.5.11.jar;
....m2\repository\io\vertx\vertx-core\4.5.11\vertx-core-4.5.11.jar;
....m2\repository\io\netty\netty-common\4.1.115.Final\netty-common-4.1.115.Final.jar;
....m2\repository\io\netty\netty-buffer\4.1.115.Final\netty-buffer-4.1.115.Final.jar;
....m2\repository\io\netty\netty-transport\4.1.115.Final\netty-transport-4.1.115.Final.jar;
....m2\repository\io\netty\netty-handler\4.1.115.Final\netty-handler-4.1.115.Final.jar;
....m2\repository\io\netty\netty-transport-native-unix-common\4.1.115.Final\netty-transport-native-unix-common-4.1.115.Final.jar;
....m2\repository\io\netty\netty-codec\4.1.115.Final\netty-codec-4.1.115.Final.jar;
....m2\repository\io\netty\netty-handler-proxy\4.1.115.Final\netty-handler-proxy-4.1.115.Final.jar;
....m2\repository\io\netty\netty-codec-socks\4.1.115.Final\netty-codec-socks-4.1.115.Final.jar;
....m2\repository\io\netty\netty-codec-http\4.1.115.Final\netty-codec-http-4.1.115.Final.jar;
....m2\repository\io\netty\netty-codec-http2\4.1.115.Final\netty-codec-http2-4.1.115.Final.jar;
....m2\repository\io\netty\netty-resolver\4.1.115.Final\netty-resolver-4.1.115.Final.jar;
....m2\repository\io\netty\netty-resolver-dns\4.1.115.Final\netty-resolver-dns-4.1.115.Final.jar;
....m2\repository\io\netty\netty-codec-dns\4.1.115.Final\netty-codec-dns-4.1.115.Final.jar;
....m2\repository\com\fasterxml\jackson\core\jackson-core\2.16.1\jackson-core-2.16.1.jar;
....m2\repository\ch\qos\logback\logback-core\1.5.12\logback-core-1.5.12.jar;
....m2\repository\org\slf4j\slf4j-api\2.0.7\slf4j-api-2.0.7.jar com.example.vertx_stock_broker.MainVerticle
我的 logback.xml 文件:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
我没有发现任何依赖冲突或重复。使用的是OpenJdk22。
我希望在运行时获得logback日志
仅供参考,logback-classic.jar 是 slf4j 提供者不是 logback-core.jar。