Grails3とのログバインディングとしてLog4j2を使用したいと思います。
私がこれまでに理解できることから。さまざまなロガーを使用する多くの従属依存関係があるため、SLF4JAPIを使用する必要があります。次に、grails / groovy / springがSLF4JAPIをLogbackバインディングにリダイレクトする代わりに、それぞれをLog4j2バインディングにリダイレクトする必要があります。
grails 3はLogbackバインディングを使用するため、build.gradleの各依存関係を調べ、Logbackバインディングを除外し、Log4j2バインディングを含めることを計画しています。これは機能しますか?更新:はい
Log4j2APIをSLF4jAPIにブリッジする必要もありますか?そのためにどのような依存関係が必要ですか?更新:以下を参照してください。
最後に、grails 3 logback.groovy構成を破棄し、log4j2構成の1つをsrc / main / resourcesに配置する必要があると想定します。更新:はい
これがわかったら更新を投稿しますが、誰かが以前にこれを行ったことがあるに違いありません。
更新2016-03-18:
これは非常に簡単であることが判明しました。grails 3プロジェクトで「./gradlew依存関係」を実行して、Logbackバインディング/実装でどの依存関係がプルされているかを確認しました(グループ:「ch.qos.logback」、モジュール:「logback-classic」)
まず、「grailscreate-apptestit」コマンドを介して生成されたデフォルトの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
}
依存関係レポートは、それらが2つの依存関係によって引き込まれていることを示しました。
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を追加しました。
グルーヴィーなコードでは、次のものを使用しました。
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ステートメントを配置します。
それだけです。現在、構成の変更時にログメッセージが失われない高速の非同期ロギングを実行しています。
答えとして何かを投稿するのを忘れました。投票できる回答は次のとおりですが、ソリューションに関するすべての情報を上記のコメントに記載します。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加