问题
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");
}
}
and 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
How do I force log4j2 to use my xml 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'
}
回答1:
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());
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";
EDIT:
You were not excluding the right module. On your gradle file You have to replace
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.
Hope I helped.
来源:https://stackoverflow.com/questions/57866555/log4j2-in-spring-boot-is-not-using-the-pattern-in-the-xml-file