为Log4j2配置Grails 3

时间:2016-03-18 02:06:37

标签: grails slf4j logback log4j2 grails-3.1

我们希望使用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-03-18更新:

事实证明这是非常直截了当的。我做了一个' ./ gradlew依赖'在我的grails 3项目中查看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语句放在频繁使用的类的构造函数中。

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

1 个答案:

答案 0 :(得分:6)

忘记发帖作为答案。以下是您可以投票的答案,但我在上述评论中提供了有关该解决方案的所有信息。