8

我们想使用 Log4j2 作为与 grails 3 的日志绑定。

据我目前所知。我们有许多使用各种记录器的从属依赖项,因此我们需要使用 SLF4J API。然后,我们不需要让 grails / groovy / spring 将 SLF4J API 重新定向到 Logback 绑定,而是需要将每个 API 重新定向到 Log4j2 绑定。

由于 grails 3 使用 Logback 绑定,我计划遍历 build.gradle 中的每个依赖项,排除 Logback 绑定,并包含 Log4j2 绑定。这行得通吗?更新:是的

我们是否还需要将 Log4j2 API 桥接到 SLF4j API?我们需要什么依赖?更新:见下文。

最后,我假设我们需要放弃 grails 3 logback.groovy 配置,只需将其中一个 log4j2 配置放在 src/main/resources 中。更新:是的

当我们弄清楚这一点时,我会发布更新,但我敢打赌以前有人这样做过。

2016 年 3 月 18 日更新:

事实证明这是非常直接的。我在我的 grails 3 项目上做了一个“./gradlew 依赖项”,以查看哪些依赖项正在拉入 Logback 绑定/实现(组:'ch.qos.logback',模块:'logback-classic')

首先,这是通过“grails create-app testit”命令生成的默认 build.gradle:

buildscript {
    ext {
        grailsVersion = project.grailsVersion
    }
    repositories {
        mavenLocal()
        maven { url "https://repo.grails.org/grails/core" }
    }
    dependencies {
        classpath "org.grails:grails-gradle-plugin:$grailsVersion"
        classpath "com.bertramlabs.plugins:asset-pipeline-gradle:2.5.0"
        classpath "org.grails.plugins:hibernate4:5.0.2"
    }
}

version "0.1"
group "testit"

apply plugin:"eclipse"
apply plugin:"idea"
apply plugin:"war"
apply plugin:"org.grails.grails-web"
apply plugin:"org.grails.grails-gsp"
apply plugin:"asset-pipeline"

ext {
    grailsVersion = project.grailsVersion
    gradleWrapperVersion = project.gradleWrapperVersion
}

repositories {
    mavenLocal()
    maven { url "https://repo.grails.org/grails/core" }
}

dependencyManagement {
    imports {
        mavenBom "org.grails:grails-bom:$grailsVersion"
    }
    applyMavenExclusions false
}

dependencies {
    compile "org.springframework.boot:spring-boot-starter-logging"
    compile "org.springframework.boot:spring-boot-autoconfigure"
    compile "org.grails:grails-core"
    compile "org.springframework.boot:spring-boot-starter-actuator"
    compile "org.springframework.boot:spring-boot-starter-tomcat"
    compile "org.grails:grails-dependencies"
    compile "org.grails:grails-web-boot"
    compile "org.grails.plugins:cache"
    compile "org.grails.plugins:scaffolding"
    compile "org.grails.plugins:hibernate4"
    compile "org.hibernate:hibernate-ehcache"
    console "org.grails:grails-console"
    profile "org.grails.profiles:web:3.1.4"
    runtime "org.grails.plugins:asset-pipeline"
    runtime "com.h2database:h2"
    testCompile "org.grails:grails-plugin-testing"
    testCompile "org.grails.plugins:geb"
    testRuntime "org.seleniumhq.selenium:selenium-htmlunit-driver:2.47.1"
    testRuntime "net.sourceforge.htmlunit:htmlunit:2.18"
}

task wrapper(type: Wrapper) {
    gradleVersion = gradleWrapperVersion
}

assets {
    minifyJs = true
    minifyCss = true
}

依赖项报告显示它们被两个依赖项拉入:

    compile "org.springframework.boot:spring-boot-starter-logging"

    compile "org.springframework.boot:spring-boot-starter-actuator"

因此,我只需要对 build.gradle 的依赖项部分进行一些更改:

dependencies {
    // commented out the original way using Logback    
    //compile "org.springframework.boot:spring-boot-starter-logging"

    // added the new way using Log4j2, yes, spring makes it easy
    compile "org.springframework.boot:spring-boot-starter-log4j2"

    // changed spring-boot-autoconfigure so that it would not
    // pull in the logback binding/implementation
    compile ('org.springframework.boot:spring-boot-autoconfigure') {
       exclude group: 'ch.qos.logback', module: 'logback-classic'
    }

    // and finally, added the log4j2 binding/implementation
    compile "org.apache.logging.log4j:log4j-api:2.5"
    compile "org.apache.logging.log4j:log4j-core:2.5"

    // the rest is unchanged
    compile "org.grails:grails-core"
    compile "org.springframework.boot:spring-boot-starter-actuator"
    compile "org.springframework.boot:spring-boot-starter-tomcat"
    compile "org.grails:grails-dependencies"
    compile "org.grails:grails-web-boot"
    compile "org.grails.plugins:cache"
    compile "org.grails.plugins:scaffolding"
    compile "org.grails.plugins:hibernate4"
    compile "org.hibernate:hibernate-ehcache"
    console "org.grails:grails-console"
    profile "org.grails.profiles:web:3.1.4"
    runtime "org.grails.plugins:asset-pipeline"
    runtime "com.h2database:h2"
    testCompile "org.grails:grails-plugin-testing"
    testCompile "org.grails.plugins:geb"
    testRuntime "org.seleniumhq.selenium:selenium-htmlunit-driver:2.47.1"
    testRuntime "net.sourceforge.htmlunit:htmlunit:2.18"
}

在 src/main/resources 中,我们添加了一个 log4j2.xml。

在 groovy 代码中,我们使用了:

import org.apache.logging.log4j.Logger
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.ThreadContext

private static final Logger log = LogManager.getLogger(getClass())

log.info('Hello World')

我们还将 ThreadContext 语句放在频繁使用的类的构造函数中。

就是这样。现在我们正在做快速的异步日志记录,在配置更改时不会丢失任何日志消息。

4

1 回答 1

6

忘记发布一些东西作为答案。这是您可以投票的答案,但我将有关该解决方案的所有信息都放在了上面的评论中。

于 2016-09-11T15:54:02.873 回答