为 Log4j2 配置 Grails 3
Configuring Grails 3 for Log4j2
我们想使用 Log4j2 作为与 grails 3 的日志绑定。
据我目前所知。我们有许多使用各种记录器的从属依赖项,因此我们需要使用 SLF4J API。然后,不是让 grails / groovy / spring 将 SLF4J API 重定向到 Logback 绑定,我们需要将每个重定向到 Log4j2 绑定。
由于 grails 3 使用 Logback 绑定,我计划检查 build.gradle 中的每个依赖项,排除 Logback 绑定,并包括 Log4j2 绑定。这行得通吗?更新:是
我们是否还需要将 Log4j2 API 桥接到 SLF4j API?我们需要什么依赖?更新:见下文。
最后,我假设我们需要放弃 grails 3 logback.groovy 配置,只将其中一个 log4j2 配置放在 src/main/resources 中。更新:是
我会 post 更新,但我敢打赌以前有人这样做过。
2016-03-18 更新:
事实证明这非常简单。我在我的 grails 3 项目上做了一个 './gradlew dependencies' 来查看哪些依赖项在 Logback binding/implementation (group: 'ch.qos.logback', module: '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 语句
就是这样。现在我们正在执行快速的异步日志记录,在配置更改时不会丢失任何日志消息。
忘记post一些东西作为答案。这是您可以投票的答案,但我将有关解决方案的所有信息放在上面的评论中。
我们想使用 Log4j2 作为与 grails 3 的日志绑定。
据我目前所知。我们有许多使用各种记录器的从属依赖项,因此我们需要使用 SLF4J API。然后,不是让 grails / groovy / spring 将 SLF4J API 重定向到 Logback 绑定,我们需要将每个重定向到 Log4j2 绑定。
由于 grails 3 使用 Logback 绑定,我计划检查 build.gradle 中的每个依赖项,排除 Logback 绑定,并包括 Log4j2 绑定。这行得通吗?更新:是
我们是否还需要将 Log4j2 API 桥接到 SLF4j API?我们需要什么依赖?更新:见下文。
最后,我假设我们需要放弃 grails 3 logback.groovy 配置,只将其中一个 log4j2 配置放在 src/main/resources 中。更新:是
我会 post 更新,但我敢打赌以前有人这样做过。
2016-03-18 更新:
事实证明这非常简单。我在我的 grails 3 项目上做了一个 './gradlew dependencies' 来查看哪些依赖项在 Logback binding/implementation (group: 'ch.qos.logback', module: '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 语句就是这样。现在我们正在执行快速的异步日志记录,在配置更改时不会丢失任何日志消息。
忘记post一些东西作为答案。这是您可以投票的答案,但我将有关解决方案的所有信息放在上面的评论中。