为Log4j2配置Grails 3 [英] Configuring Grails 3 for Log4j2

查看:99
本文介绍了为Log4j2配置Grails 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们想将Log4j2用作grails 3的日志绑定.

We would like to use Log4j2 as the log binding with grails 3.

到目前为止我能弄清楚的是.我们有许多使用各种记录器的从属依赖项,因此我们需要使用SLF4J API.然后,我们无需让grails/groovy/spring将SLF4J API重定向到Logback绑定,而是需要将每个SLF4J API重定向到Log4j2绑定.

From what I can figure out so far. We have many subordinate dependencies that use a variety of loggers, so we need to use the SLF4J API. Then, instead of letting grails / groovy / spring re-direct the SLF4J API to the Logback binding, we need to re-direct each to the Log4j2 binding.

由于grails 3使用Logback绑定,因此我打算遍历build.gradle中的每个依赖项,排除Logback绑定,并包括Log4j2绑定.这会工作吗?更新:是

Since grails 3 uses the Logback binding, I am planning to go through each dependency in the build.gradle, exclude the Logback binding, and include the Log4j2 binding. Will this work? Update: Yes

我们还需要将Log4j2 API桥接到SLF4j API吗?我们需要什么依赖?更新:请参见下文.

Do we also need to bridge the Log4j2 API to the SLF4j API? What dependency do we need for that? Update: See below.

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

Finally, I assume we need to ditch the grails 3 logback.groovy configuration and just put one of the log4j2 configs in src/main/resources. Update: Yes

我们将在解决此问题时发布更新,但我敢打赌有人之前已经这样做了.

I'll post updates as we figure this out, but I bet someone has done this before.

更新2016-03-18:

Update 2016-03-18:

事实证明这很简单.我在grails 3项目上做了一个'./gradlew依赖项',以查看哪些依赖项正在Logback绑定/实现中被提取(组:'ch.qos.logback',模块:'logback-classic')

This turned out to be very straight-forward. I did a './gradlew dependencies' on my grails 3 project to see which dependencies were pulling in the Logback binding/implementation (group: 'ch.qos.logback', module: 'logback-classic')

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

First, here's the default build.gradle generated via a 'grails create-app testit' command:

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
}

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

The dependency report showed that they were being pulled in by two dependencies:

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

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

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

So, I only had to make a couple changes to the dependencies section of the 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.

In the src/main/resources, we added a log4j2.xml.

在常规代码中,我们使用:

In the groovy code, we used:

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语句放入频繁使用的类的构造函数中.

We also put ThreadContext statements in the constructors of heavily used classes.

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

That was it. Now we're doing fast, async logging that doesn't lose any log messages upon config changes.

推荐答案

忘记发布内容作为答案.这是您可以投票的答案,但是我在上面的评论中放置了有关该解决方案的所有信息.

Forgot to post something as an answer. Here's an answer you can vote for, but I put all the information about the solution in the comments above.

这篇关于为Log4j2配置Grails 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆