java日志记录问题

nafvub8i  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(199)

给我一个简单的解释。我有一个java模块,它被构建成一个库,可以跨多个其他项目使用。这是为了简化开发,所以写一次,用很多地方。
这座图书馆建得像一座大教堂。当与另一个用gradle构建的较新项目一起使用时,没有问题。但是当在一个用maven构建的老项目中使用时,我会收到错误,而且不会从库中记录日志。
fatjar lib:gradle,Lombok岛slf4j
更新的项目:gradle,lombok slf4j,fatjar lib
老项目:maven、lombok slf4j、fatjar lib
以前的项目相当大,但下面是pom.xml中的相关行:

<!-- Logging -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.13.1</version>
    <exclusions>
        <exclusion>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
        </exclusion>
        <exclusion>
            <groupId>javax.jms</groupId>
            <artifactId>jms</artifactId>
        </exclusion>
        <exclusion>
            <groupId>com.sun.jdmk</groupId>
            <artifactId>jmxtools</artifactId>
        </exclusion>
        <exclusion>
            <groupId>com.sun.jmx</groupId>
            <artifactId>jmxri</artifactId>
        </exclusion>
    </exclusions>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.13.1</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.13.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.7</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>1.7.7</version>
    <exclusions>
        <exclusion>
            <artifactId>slf4j-api</artifactId>
            <groupId>org.slf4j</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.jboss.logging</groupId>
    <artifactId>jboss-logging</artifactId>
    <version>3.1.4.GA</version>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.16</version>
    <scope>provided</scope>
</dependency>

有相当多的排除项,因为过去需要这些排除项来解决令人讨厌的“slf4j:class path contains multiple slf4j bindings”错误。
现在我看到的一个我无法理解的问题是,当与旧项目一起使用时,来自fatjar库的日志记录不起作用。我在第一次尝试写入日志时得到以下输出:

ERROR StatusLogger Unrecognized format specifier [d]
ERROR StatusLogger Unrecognized conversion specifier [d] starting at position 16 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [thread]
ERROR StatusLogger Unrecognized conversion specifier [thread] starting at position 25 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [level]
ERROR StatusLogger Unrecognized conversion specifier [level] starting at position 35 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [logger]
ERROR StatusLogger Unrecognized conversion specifier [logger] starting at position 47 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [msg]
ERROR StatusLogger Unrecognized conversion specifier [msg] starting at position 54 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [n]
ERROR StatusLogger Unrecognized conversion specifier [n] starting at position 56 in conversion pattern.

然而,在新的项目中,日志记录工作得很好。
我试过使用shadowjar插件,但在使用时遇到了问题。我尝试在fatjar库中强制logback,但没有成功。我尝试过在这两个项目中重新组织各种日志插件,但是我对改变它们的方式太过谨慎,因为独立地日志工作很好,只是这两个项目的结合是一个问题。
下面是fatjar库的build.gradle:

plugins {
    id 'java'
    id 'idea'
    id 'io.spring.dependency-management' version '1.0.1.RELEASE'
    id 'application'
}

group 'com.fatjar-lib'
version '1.0'
sourceCompatibility = '11'
apply plugin: 'idea'
apply plugin: 'java'

repositories {
    mavenCentral()
    flatDir {
        dirs 'lib'
    }
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'

    // Logger
    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.13.1'
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.13.1'
    compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.13.1'
    compile group: 'org.slf4j', name: 'log4j-over-slf4j', version: '1.7.30'
    compile group: 'org.slf4j', name: 'slf4j-ext', version: '1.7.30'

    // Fast.com library
    compile group: 'it.unimi.dsi', name: 'fastutil', version: '8.3.0'

    // Lombok
    implementation 'org.projectlombok:lombok:1.18.16'
    annotationProcessor 'org.projectlombok:lombok:1.18.16'
}

//create a single Jar with all dependencies
task fatJar(type: Jar) {
    manifest {
        attributes 'Implementation-Title': 'FatJar Lib',
                'Implementation-Version': version
    }
    baseName = project.name
    project.version=""
    from {
        configurations.compile.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
    with jar
}

要清楚的是,除了在fatjar库中进行日志记录之外,所有的应用程序功能都可以工作。这一点非常关键。
感谢所有的帮助。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题