简体   繁体   English

Spring Boot中的Log4j2未使用XML文件中的模式

[英]Log4j2 in spring boot is not using the pattern in the XML file

I have a spring boot project, this is how I use the logger: 我有一个spring boot项目,这是我使用记录器的方式:

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");
  }    
}

and src/main/resources/log4j2.xml 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 ERROR 21680 --- [nio-8081-exec-2] com.ey.web.MyController : testtestBLI 2019-09-10 11:04:08.818错误21680-[[nio-8081-exec-2] com.ey.web.MyController:testtestBLI

How do I force log4j2 to use my xml file? 如何强制log4j2使用我的xml文件?

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'
}

You need to specify the log4j2 context: inside your main method (I guess in your case inside do() before the logging): 您需要在主方法中指定log4j2上下文:(我想在日志记录之前,您在do()内部):

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

Where log4j2configFilename is the path of your settings file. 其中log4j2configFilename是设置文件的路径。 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";

EDIT: 编辑:

You were not excluding the right module. 您没有排除正确的模块。 On your gradle file You have to replace 在gradle文件上,您必须替换

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

with

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. 要绝对摆脱它,您必须管理库。

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. 在IntelliJ IDEA上:是File > Project Structure... > Libraries ,在列表上查找Gradle: org.apache.logging.log4j:log4j-to-slf4j:2.12.1并单击列表上方的-图标。

Hope I helped. 希望我能帮上忙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM