【问题标题】:Configuring Grails 3 for Log4j2为 Log4j2 配置 Grails 3
【发布时间】:2016-07-04 15:33:13
【问题描述】:

我们希望使用 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 dependencies' 以查看哪些依赖项正在拉入 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 语句放在常用类的构造函数中。

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

【问题讨论】:

  • 谢谢你。如果您添加了答案,我会投赞成票。这正是我需要让 Grails 3.1.10 使用 log4j2。

标签: grails slf4j logback log4j2 grails-3.1


【解决方案1】:

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

【讨论】:

  • 我强烈建议您更新您的问答,以将它们分隔成正确的格式.. 意思是问题只出现在问题所在的地方,而我在这里评论的您的答案包括您的答案..
猜你喜欢
  • 2020-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-06
  • 2013-04-09
  • 2017-02-15
  • 1970-01-01
  • 2014-10-18
相关资源
最近更新 更多