【问题标题】:Using slf4j + log4j with dropwizard使用 slf4j + log4j 和 dropwizard
【发布时间】:2018-07-09 11:57:21
【问题描述】:

我有一个用 Maven 构建的 REST java 应用程序。它由一个包含两个模块的项目组成:

  • myapp_server(父项目)
    • myapp_rest
    • myapp_logging

这是父 POM:

<?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>org.sample</groupId>
    <artifactId>myapp_server</artifactId>
    <version>0.0.1</version>
    <packaging>pom</packaging>

    <name>myapp_server</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <slf4j.version>1.7.25</slf4j.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>central</id>
            <url>http://repo1.maven.org/maven2</url>
        </repository>
    </repositories>
    <modules>
        <module>myapp_rest</module>
        <module>myapp_logging</module>
    </modules>

</project>

这是myapp_rest 的 POM(使用 dropwizard 中的 java-simple 原型创建):

<?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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <parent>
        <artifactId>myapp_server</artifactId>
        <groupId>org.sample</groupId>
        <version>0.0.1</version>
    </parent>
    <prerequisites>
        <maven>3.0.0</maven>
    </prerequisites>

    <groupId>org.sample</groupId>
    <artifactId>myapp_rest</artifactId>
    <packaging>jar</packaging>

    <name>REST layer</name>
    <description>REST layer</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <dropwizard.version>1.3.5</dropwizard.version>
        <mainClass>org.sample.rest.Application</mainClass>
    </properties>

    <dependencies>
        <dependency>
            <groupId>io.dropwizard</groupId>
            <artifactId>dropwizard-core</artifactId>
            <version>${dropwizard.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>log4j-over-slf4j</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>lo4j-slf4j-impl</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.sample</groupId>
            <artifactId>myapp_logging</artifactId>
            <version>0.0.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                    <createDependencyReducedPom>true</createDependencyReducedPom>
                    <transformers>
                        <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                        <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>${mainClass}</mainClass>
                        </transformer>
                    </transformers>
                    <!-- exclude signed Manifests -->
                    <filters>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                        </filter>
                    </filters>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>${mainClass}</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.0.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.0.0-M1</version>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <reporting>
        <plugins>
            <plugin>
                <artifactId>maven-project-info-reports-plugin</artifactId>
                <version>2.8.1</version>
                <configuration>
                    <dependencyLocationsEnabled>false</dependencyLocationsEnabled>
                    <dependencyDetailsEnabled>false</dependencyDetailsEnabled>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.10.3</version>
            </plugin>
        </plugins>
    </reporting>
</project>

这是myapp_logging 的 POM:

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.sample</groupId>
        <artifactId>myapp_server</artifactId>
        <version>0.0.1</version>
    </parent>

    <groupId>org.sample</groupId>
    <artifactId>myapp_logging</artifactId>

    <name>myapp_logging</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <log4j.version>2.11.0</log4j.version>
    </properties>
    <dependencies>
        <dependency> <!-- slf4j -> log4j bridge
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j.version}</version>
        </dependency>
    </dependencies>
</project>

我想让所有日志都经过slf4j,以便使用log4j 进行日志记录。 Dropwizard 在内部使用 logback,所以我试图让它使用 slf4j。

如果我运行这个项目,我会得到这个输出:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/user/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/user/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.11.0/log4j-slf4j-impl-2.11.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]

似乎 slf4j 从日志模块中正确看到了 log4j,但它也在考虑来自 logback-classic 的绑定器,这是 dropwizard 中的一个依赖项。

如果我排除 logback-classic 包,我会收到此错误:

Exception in thread "main" java.lang.NoClassDefFoundError: ch/qos/logback/classic/Level
    at io.dropwizard.Application.bootstrapLogLevel(Application.java:33)
    at io.dropwizard.Application.bootstrapLogging(Application.java:38)
    at io.dropwizard.Application.<init>(Application.java:26)
    at org.sample.rest.Application.<init>(Application.java:10)
    at org.sample.rest.Application.main(Application.java:15)
Caused by: java.lang.ClassNotFoundException: ch.qos.logback.classic.Level
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 5 more

通过单击类名,它会将我重定向到 io.dropwizard.Application 类,该类包含对 ch.qos.logback.classic.Level 的引用,因此无法安全删除 logback 包。

我已经看到 log4j 有一个“反向桥”,可以让您代理对 slf4j (log4j-over-slf4j) 的所有调用,但似乎没有 logback 的等效项。

有没有办法摆脱 logback 并通过带有 dropwizard 的 slf4j 使用任何其他框架?

【问题讨论】:

    标签: java maven log4j slf4j dropwizard


    【解决方案1】:

    实际上有一个关于如何摆脱 Logback 的 Dropwizard 文档: https://dropwizard.readthedocs.io/en/stable/manual/core.html#logging

    阅读黄色注释(错误级别正下方),它会告诉您该怎么做。希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-13
      • 2012-02-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2023-03-31
      • 2011-11-20
      • 2016-04-23
      相关资源
      最近更新 更多