问题描述
我使用log4j2使用弹簧引导2.0.1使用以下pom.战争文件托管在Tomcat 8中(而不是使用嵌入式tomcat).应用程序日志写入控制台,而不是写入日志文件.显然,Spring Boot的XML配置被覆盖在应用程序中配置的XML配置.
pom:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-web</artifactId> </dependency>
log4j2使用复合配置,其中配置在2个XML文件中分配:log4j2-base.xml和log4j2-env.xml
Resources目录包含log4j2.component.properties带有以下内容:
log4j.configurationFile=log4j2-base.xml,log4j2-env.xml
资源目录还包含log4j2-base.xml.文件log4j2-env.xml是已放置在类路径的环境特定JAR文件的一部分.
日志:
DEBUG StatusLogger Configuration XmlConfiguration[location=jar:file:/usr/mware/jwsAppDetails/app1/webapps/myProject/WEB-INF/lib/spring-boot-2.0.1.RELEASE.jar!/org/springframework/boot/logging/log4j2/log4j2.xml] initialized DEBUG StatusLogger Starting configuration XmlConfiguration[location=jar:file:/usr/mware/jwsAppDetails/app1/webapps/myProject/WEB-INF/lib/spring-boot-2.0.1.RELEASE.jar!/org/springframework/boot/logging/log4j2/log4j2.xml] DEBUG StatusLogger Started configuration XmlConfiguration[location=jar:file:/usr/mware/jwsAppDetails/app1/webapps/myProject/WEB-INF/lib/spring-boot-2.0.1.RELEASE.jar!/org/springframework/boot/logging/log4j2/log4j2.xml] OK. TRACE StatusLogger Stopping org.apache.logging.log4j.core.config.composite.CompositeConfiguration@40575bb9 [configurations=[XmlConfiguration[location=/usr/mware/jwsAppDetails/app1/webapps/myProject/WEB-INF/classes/log4j2-base.xml], XmlConfiguration[location=jar:file:/usr/mware/jwsAppDetails/app1/lib/myProject.jar!/log4j2-env.xml]], mergeStrategy=org.apache.logging.log4j.core.config.composite.DefaultMergeStrategy@583fc0cb, rootNode=null, listeners=[], pluginPackages=[], pluginManager=org.apache.logging.log4j.core.config.plugins.util.PluginManager@278307b, isShutdownHookEnabled=true, shutdownTimeoutMillis=0, scriptManager=org.apache.logging.log4j.core.script.ScriptManager@185463aa]... TRACE StatusLogger CompositeConfiguration notified 8 ReliabilityStrategies that config will be stopped. TRACE StatusLogger CompositeConfiguration stopping 7 LoggerConfigs. TRACE StatusLogger CompositeConfiguration stopping root LoggerConfig. TRACE StatusLogger CompositeConfiguration notifying ReliabilityStrategies that appenders will be stopped. TRACE StatusLogger CompositeConfiguration stopping remaining Appenders. DEBUG StatusLogger Shutting down RollingFileManager null DEBUG StatusLogger Shutting down RollingFileManager {}null DEBUG StatusLogger All asynchronous threads have terminated DEBUG StatusLogger RollingFileManager shutdown completed with status true DEBUG StatusLogger Shut down RollingFileManager null, all resources released: true DEBUG StatusLogger Appender RollingFile stopped with status true TRACE StatusLogger CompositeConfiguration stopped 1 remaining Appenders. TRACE StatusLogger CompositeConfiguration cleaning Appenders from 8 LoggerConfigs. DEBUG StatusLogger Stopped org.apache.logging.log4j.core.config.composite.CompositeConfiguration@40575bb9 [configurations=[XmlConfiguration[location=/usr/mware/jwsAppDetails/app1/webapps/myProject/WEB-INF/classes/log4j2-base.xml], XmlConfiguration[location=jar:file:/usr/mware/jwsAppDetails/app1/lib/myProject.jar!/log4j2-env.xml]], mergeStrategy=org.apache.logging.log4j.core.config.composite.DefaultMergeStrategy@583fc0cb, rootNode=null, listeners=[], pluginPackages=[], pluginManager=org.apache.logging.log4j.core.config.plugins.util.PluginManager@278307b, isShutdownHookEnabled=true, shutdownTimeoutMillis=0, scriptManager=org.apache.logging.log4j.core.script.ScriptManager@185463aa] OK TRACE StatusLogger Reregistering MBeans after reconfigure. Selector=org.apache.logging.log4j.core.selector.ClassLoaderContextSelector@77aa3299 TRACE StatusLogger Reregistering context (1/1): '2190fd23' org.apache.logging.log4j.core.LoggerContext@64150381
推荐答案
我们尝试了以下操作,并起作用:
- 将文件log4j-base.xml重命名为log4j2.xml
-
在log4j2.component.properties中,重命名log4j-base.xml为log4j2.xml 如下:
log4j.configurationfile = log4j2.xml,log4j2-env.xml
问题描述
I am using Spring Boot 2.0.1 starter with log4j2 using following POM. WAR file is hosted in Tomcat 8 (instead of using embedded tomcat). Application logs are written to console instead of writing to log file. Apparently Spring Boot's XML configuration is overwrites the one configured in application.
POM:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-web</artifactId> </dependency>
Log4j2 uses composite configuration where configuration is split in 2 xml files: log4j2-base.xml and log4j2-env.xml
Resources directory contains log4j2.component.properties with following content:
log4j.configurationFile=log4j2-base.xml,log4j2-env.xml
Resources directory also contains log4j2-base.xml. File log4j2-env.xml is part of environment specific JAR file that is already placed into class path.
Log:
DEBUG StatusLogger Configuration XmlConfiguration[location=jar:file:/usr/mware/jwsAppDetails/app1/webapps/myProject/WEB-INF/lib/spring-boot-2.0.1.RELEASE.jar!/org/springframework/boot/logging/log4j2/log4j2.xml] initialized DEBUG StatusLogger Starting configuration XmlConfiguration[location=jar:file:/usr/mware/jwsAppDetails/app1/webapps/myProject/WEB-INF/lib/spring-boot-2.0.1.RELEASE.jar!/org/springframework/boot/logging/log4j2/log4j2.xml] DEBUG StatusLogger Started configuration XmlConfiguration[location=jar:file:/usr/mware/jwsAppDetails/app1/webapps/myProject/WEB-INF/lib/spring-boot-2.0.1.RELEASE.jar!/org/springframework/boot/logging/log4j2/log4j2.xml] OK. TRACE StatusLogger Stopping org.apache.logging.log4j.core.config.composite.CompositeConfiguration@40575bb9 [configurations=[XmlConfiguration[location=/usr/mware/jwsAppDetails/app1/webapps/myProject/WEB-INF/classes/log4j2-base.xml], XmlConfiguration[location=jar:file:/usr/mware/jwsAppDetails/app1/lib/myProject.jar!/log4j2-env.xml]], mergeStrategy=org.apache.logging.log4j.core.config.composite.DefaultMergeStrategy@583fc0cb, rootNode=null, listeners=[], pluginPackages=[], pluginManager=org.apache.logging.log4j.core.config.plugins.util.PluginManager@278307b, isShutdownHookEnabled=true, shutdownTimeoutMillis=0, scriptManager=org.apache.logging.log4j.core.script.ScriptManager@185463aa]... TRACE StatusLogger CompositeConfiguration notified 8 ReliabilityStrategies that config will be stopped. TRACE StatusLogger CompositeConfiguration stopping 7 LoggerConfigs. TRACE StatusLogger CompositeConfiguration stopping root LoggerConfig. TRACE StatusLogger CompositeConfiguration notifying ReliabilityStrategies that appenders will be stopped. TRACE StatusLogger CompositeConfiguration stopping remaining Appenders. DEBUG StatusLogger Shutting down RollingFileManager null DEBUG StatusLogger Shutting down RollingFileManager {}null DEBUG StatusLogger All asynchronous threads have terminated DEBUG StatusLogger RollingFileManager shutdown completed with status true DEBUG StatusLogger Shut down RollingFileManager null, all resources released: true DEBUG StatusLogger Appender RollingFile stopped with status true TRACE StatusLogger CompositeConfiguration stopped 1 remaining Appenders. TRACE StatusLogger CompositeConfiguration cleaning Appenders from 8 LoggerConfigs. DEBUG StatusLogger Stopped org.apache.logging.log4j.core.config.composite.CompositeConfiguration@40575bb9 [configurations=[XmlConfiguration[location=/usr/mware/jwsAppDetails/app1/webapps/myProject/WEB-INF/classes/log4j2-base.xml], XmlConfiguration[location=jar:file:/usr/mware/jwsAppDetails/app1/lib/myProject.jar!/log4j2-env.xml]], mergeStrategy=org.apache.logging.log4j.core.config.composite.DefaultMergeStrategy@583fc0cb, rootNode=null, listeners=[], pluginPackages=[], pluginManager=org.apache.logging.log4j.core.config.plugins.util.PluginManager@278307b, isShutdownHookEnabled=true, shutdownTimeoutMillis=0, scriptManager=org.apache.logging.log4j.core.script.ScriptManager@185463aa] OK TRACE StatusLogger Reregistering MBeans after reconfigure. Selector=org.apache.logging.log4j.core.selector.ClassLoaderContextSelector@77aa3299 TRACE StatusLogger Reregistering context (1/1): '2190fd23' org.apache.logging.log4j.core.LoggerContext@64150381
推荐答案
We tried the following and it worked:
- Rename the file log4j-base.xml to log4j2.xml
In log4j2.component.properties, rename log4j-base.xml to log4j2.xml like below:
log4j.configurationFile=log4j2.xml,log4j2-env.xml