首页 > 其他分享 >log4j2.xml 使用 application.yml 配置的属性

log4j2.xml 使用 application.yml 配置的属性

时间:2022-11-16 14:35:51浏览次数:89  
标签:xml spring application org import 日志 log4j2 yml

转自:https://blog.csdn.net/xiaokanfuchen86/article/details/126695797

 

log4j2.xml 是不归 spring 管理的,所以也就没法读取到 application.yml 里面的配置了。 解决方式: 通过 spring 的 监听器(Listener)功能,将我们读取到的 application.yml 的日志路径设置到系统属性,然后在日志文件里面读取对应的系统属性就行了。

LoggingListener.java

通过 spring 的 监听器(Listener)功能,将我们读取到的 application.yml 的日志路径设置到系统属性,或者使用MDC

 1 import org.apache.commons.lang3.StringUtils;
 2 import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
 3 import org.springframework.boot.context.logging.LoggingApplicationListener;
 4 import org.springframework.context.ApplicationEvent;
 5 import org.springframework.context.ApplicationListener;
 6 import org.springframework.core.Ordered;
 7 import org.springframework.core.env.ConfigurableEnvironment;
 8 import org.springframework.stereotype.Component;
 9  
10 /**
11  * 读取yml配置传递到log4jXml中
12  *
13  * @author Clay
14  */
15 @Component
16 public class LoggingListener implements ApplicationListener<ApplicationEvent>, Ordered {
17     /**
18      * 提供给日志文件读取配置的key,使用时需要在前面加上 sys:
19      */
20     private final static String LOG_PATH = "log.path";
21  
22     /**
23      * spring 内部设置的日志文件的配置key
24      */
25     private final static String SPRING_LOG_PATH_PROP = "spring.log-file-path";
26  
27     @Override
28     public void onApplicationEvent(ApplicationEvent applicationEvent) {
29  
30         if (applicationEvent instanceof ApplicationEnvironmentPreparedEvent) {
31             ConfigurableEnvironment environment = ((ApplicationEnvironmentPreparedEvent) applicationEvent).getEnvironment();
32             String filePath = environment.getProperty(SPRING_LOG_PATH_PROP);
33             if (StringUtils.isNotEmpty(filePath)) {
34                 System.err.println("=================" + filePath);
35                 System.setProperty(LOG_PATH, filePath);
36             }
37         }
38     }
39  
40     @Override
41     public int getOrder() {
42         // 当前监听器的启动顺序需要在日志配置监听器的前面,所以此处减 1
43         return LoggingApplicationListener.DEFAULT_ORDER - 1;
44     }
45  
46 }

application.yml

spring: 
  log-file-path: "F:/logs/"

log4j2.xml

<Property name="log-path">${sys:log.path}</Property>

Application.java

这里没有贴出注解,关键于.addListeners(new LoggingListener())

内置Tomcat

 1 public class Application {
 2  
 3     public static void main(String[] args) {
 4         SpringApplication application = new SpringApplication(Application.class);
 5         // 添加 日志监听器,使 log4j2-spring.xml 可以间接读取到配置文件的属性
 6         application.addListeners(new LoggingListener());
 7         application.run(args);
 8     }
 9  
10 }

外置Tomcat

 1 public class TomcatApplication extends SpringBootServletInitializer {
 2  
 3     @Override
 4     protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
 5         // 添加 日志监听器,使 log4j2-spring.xml 可以间接读取到配置文件的属性
 6         builder.application().addListeners(new LoggingListener());
 7         return builder.sources(Application.class);
 8     }
 9  
10 }

 

标签:xml,spring,application,org,import,日志,log4j2,yml
From: https://www.cnblogs.com/fnlingnzb-learner/p/16895809.html

相关文章

  • mybatis XML 使用 case when 代替多个if
    不建议使用<selectid="findActiveBlogLike"resultType="Blog">SELECT*FROMBLOGWHEREstate=‘ACTIVE’<iftest="title!=null">AND......
  • pom.xml图标变成蜘蛛怎么办
    IDEA的pom.xml文件上面有蜘蛛标志,这种情况是由于不小心点击AddasAntBuildFile,然后pom.xml就成蜘蛛标志了,点开Ant,一般右侧会有Ant,若没有就点上方工具栏的Views—>Tool......
  • day31 1 tomcat介绍与创建web项目 & 2 继承HttpServlet类、配置webxml全局配置文件 &
    ServletJavaServlet是运行在Web服务器或应用服务器上的程序,作为客户端(Web浏览器或其他HTTP客户端)和服务端(HTTP服务器上的数据库或应用程序)之间的中间层。使用Servlet可......
  • Protocol Buffer&Json&Xml
    ProtocolBuffer、Json、Xml都是一种数据交换格式(对通信双方要交换信息的组织方式进行了定义),都独立于语言及平台。ProtocolBuffer是基于二进制的,message对象序列化后......
  • sqlserver xml 操作:1、使用for xml
    sqlserverxml操作:1、使用forxml一个是侧重介绍forxml模式的,一个是侧重语法及细节参数的forxml(SqlServer)forxml子句的基本语法说实话,老顾觉得其......
  • sqlserver xml 操作:2、对确定结构的xml进行操作
    sqlserverxml操作:2、对确定结构的xml进行操作我们使用的xml有着明确的定义结构,该啥节点,该啥属性,该啥层级,基本在设计之初就已经确定了,很少有确定了结构的xml格式再......
  • sqlserver xml 操作:3、对不确定结构、属性的xml进行处理
    sqlserverxml操作:3、对不确定结构、属性的xml进行处理在平时使用过程中,xml的结构一般都是已经确定了的,所以我们读取并不是很麻烦的事,但如果一旦结构或数据不确定,......
  • sqlserver FOR XML查询参数path的实例
     SQLSERVER中XML查询:FORXML指定PATH前言在SQLSERVER中,XML查询可以指定RAW,AUTO,EXPLICIT,PATH。本文用一些实例介绍SQLSERVER中指定PATH的XML查询。PA......
  • SQL中的OpenXML使用案例
    DECLARE@idocintDECLARE@docvarchar(1000)SET@doc='<ROOT><CustomerCustomerID="VINET"ContactName="PaulHenriot"><OrderOrderID="10248"Customer......
  • SQL 存储过程 解析XML
    第一种说明:我看过这样一篇文章,如下 在SQL Server2005中,微软延续了 2000中一个特性(即支持XML类型的数据),并加强了对XML 数据列、XML变量以及XML索引的支持。 ......