Мы хотели бы использовать Log4j2 в журнале связывания с Граалей 3.Конфигурирование Grails 3 для Log4j2
Из того, что я могу понять до сих пор. У нас есть много подчиненных зависимостей, которые используют множество регистраторов, поэтому нам нужно использовать API SLF4J. Затем вместо того, чтобы позволить grails/groovy/spring повторно перенаправить API SLF4J на привязку журнала, нам нужно перенаправить их на привязку Log4j2.
Поскольку grails 3 использует привязку журнала, я планирую пройти через каждую зависимость в build.gradle, исключить привязку журнала и включить привязку Log4j2. Будет ли это работать? Обновление: да
Нужно ли нам также подключить API Log4j2 к API SLF4j? Какая зависимость нам нужна? Обновление: см. Ниже.
Наконец, я предполагаю, что нам нужно отбросить конфигурацию grails 3 logback.groovy и просто поместить одну из конфигураций log4j2 в src/main/resources. Обновление: Да
Я буду публиковать обновления, как мы это выясним, но я уверен, кто-то сделал это раньше.
Update 2016-03-18:
Это оказалось очень просто. Я сделал «./gradlew dependencies» в моем проекте grails 3, чтобы увидеть, какие зависимости были связаны с привязкой/реализацией журнала (группа: «ch.qos.logback», модуль: «logback-classic»)
Первый , вот build.gradle по умолчанию генерируется с помощью команды «Grails создать приложение-TestIt»:
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/основные/ресурсов, мы добавили 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 заявления в конструкторах интенсивно используемых классов.
Это было. Теперь мы делаем быстрый асинхронный журнал, который не теряет сообщений журнала при изменениях конфигурации.
Благодарим вас за это. Если бы вы добавили ответ, я бы проголосовал за него. Это было именно то, что мне нужно было сделать Grails 3.1.10 для usse log4j2. – Speck