Spring Boot中的Log4j2未使用XML文件中的模式 [英] Log4j2 in spring boot is not using the pattern in the XML file

查看:117
本文介绍了Spring Boot中的Log4j2未使用XML文件中的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个spring boot项目,这就是我使用记录器的方式:

I have a spring boot project, this is how I use the logger:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

class MyController{    
  private static final Logger log = LogManager.getLogger(MyController.class);

  public void do(){
   log.error("test");
  }    
}

src/main/resources/log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="-- %msg%n --" />
        </Console>
    </Appenders>
    <Loggers>
        <Root level="debug" additivity="false">
            <AppenderRef ref="console" />
        </Root>
    </Loggers>
</Configuration>

这是我在控制台中看到的:

this is how I see it in the console:

2019-09-10 11:04:08.818错误21680 --- [nio-8081-exec-2]com.ey.web.MyController:testtestBLI

2019-09-10 11:04:08.818 ERROR 21680 --- [nio-8081-exec-2] com.ey.web.MyController : testtestBLI

如何强制log4j2使用我的xml文件?

How do I force log4j2 to use my xml file?

GRADLE FILE:

GRADLE FILE:

// hot deploy config https://stackoverflow.com/a/52070831/982234
plugins {
    id 'org.springframework.boot' version '2.1.5.RELEASE'
    id 'java'
    id 'io.spring.dependency-management' version '1.0.1.RELEASE'

}

apply plugin: 'io.spring.dependency-management'
apply plugin: 'idea'

group = 'com.tt'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}
sourceSets.main.java.srcDirs += "${buildDir}/generated"

compileJava {
    options.annotationProcessorGeneratedSourcesDirectory = file("${buildDir}/generated")
}

dependencyManagement {
    imports {
        mavenBom 'org.apache.logging.log4j:log4j-bom:2.12.1'
    }
}
configurations.all {
    exclude group: 'org.slf4j', module: 'slf4j-log4j12'
}
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-rest'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'

    compile group: 'org.springframework.security.oauth', name: 'spring-security-oauth2', version: '2.3.6.RELEASE'
    compile 'io.jsonwebtoken:jjwt-api:0.10.5'
    runtime 'io.jsonwebtoken:jjwt-impl:0.10.5'
    runtime 'io.jsonwebtoken:jjwt-jackson:0.10.5'

    compile group: 'com.pipl.api', name: 'piplapis', version: '5.0.11'
    compile project('system')
    compile 'org.apache.httpcomponents:httpclient:4.5'
    compile("org.springframework.boot:spring-boot-devtools")

    compile "org.springframework.boot:spring-boot-starter-data-jpa:2.0.5.RELEASE"
    compile "mysql:mysql-connector-java:5.1.47"
    compile group: 'org.hibernate', name: 'hibernate-core', version: '5.4.4.Final'

    annotationProcessor("javax.xml.bind:jaxb-api")
    annotationProcessor("org.hibernate:hibernate-jpamodelgen")

    compile group: 'org.jboss.logging', name: 'jboss-logging', version: '3.4.1.Final'

    compile 'com.google.cloud:google-cloud-storage:1.89.0'



    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.12.1'
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.12.1'


    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.9'
}

推荐答案

您需要指定log4j2上下文:在您的主要方法中(我想在您登录之前在 do()中):

You need to specify the log4j2 context: inside your main method (I guess in your case inside do() before the logging):

LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
File log4configFile = new File(log4j2configFilename);  
context.setConfigLocation(log4configFile.toURI());

其中 log4j2configFilename 是设置文件的路径.如果您将该文件夹正确标记为资源文件夹(默认情况下),则应为

Where log4j2configFilename is the path of your settings file. If you properly marked that folder as resource folder (as default) it should just be

String log4j2configFilename = "log4j2.xml";

如果它不起作用,请尝试:

if it doesn't work try with:

String log4j2configFilename = "src/main/resources/log4j2.xml";

您没有排除正确的模块.在gradle文件中,您必须替换

You were not excluding the right module. On your gradle file You have to replace

exclude group: 'org.slf4j', module: 'slf4j-log4j12'

使用

exclude group: 'org.apache.logging.log4j', module: 'log4j-to-slf4j'

由于以前已包含该模块,因此可能仍处于等待状态.要绝对摆脱它,您必须管理该库.

It may happen that the module is still in the depencies because it was included before. To definitely get rid of it you have to manage the library.

在IntelliJ IDEA上:是 File>项目结构...库,在列表中查找 Gradle:org.apache.logging.log4j:log4j-to-slf4j:2.12.1 并单击-列表上方的图标.

On IntelliJ IDEA: is File > Project Structure... > Libraries, on the list look up for Gradle: org.apache.logging.log4j:log4j-to-slf4j:2.12.1 and click on the - icon above the list.

希望我能帮上忙.

这篇关于Spring Boot中的Log4j2未使用XML文件中的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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