我们想使用 Log4j2 作为与 grails 3 的日志绑定。
从目前我能了解到的情况来看。我们有许多使用各种记录器的从属依赖项,因此我们需要使用 SLF4J API。然后,我们需要将每个 API 重新定向到 Log4j2 绑定,而不是让 grails/groovy/spring 将 SLF4J API 重新定向到 Logback 绑定。
由于 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"
and
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 语句放入频繁使用的类的构造函数中。
就是这样。现在我们正在进行快速的异步日志记录,在配置更改时不会丢失任何日志消息。