参考文章:SpringBoot+log4j2.xml使用application.yml属性值

1.创建Listener

package com.cloud.config;import org.apache.commons.lang.StringUtils;
import org.jboss.logging.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.GenericApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.ResolvableType;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;/*** @author langpf 2019/1/2*/
@Component
public class ApplicationStartedEventListener implements GenericApplicationListener {private static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 10;private static Class<?>[] EVENT_TYPES = {ApplicationStartingEvent.class,ApplicationEnvironmentPreparedEvent.class, ApplicationPreparedEvent.class,ContextClosedEvent.class, ApplicationFailedEvent.class};private static Class<?>[] SOURCE_TYPES = {SpringApplication.class,ApplicationContext.class};@Autowiredprivate Environment env;@Overridepublic void onApplicationEvent(ApplicationEvent event) {String appName = env.getProperty("spring.application.name");if (StringUtils.isNotBlank(appName)) {MDC.put("appName", appName);}String port = env.getProperty("server.port");if (StringUtils.isNotBlank(port)) {MDC.put("port", port);}String ip =  getIp();if (StringUtils.isNotBlank(ip)) {MDC.put("ip", ip);}}private String getIp() {InetAddress ip;try {ip = InetAddress.getLocalHost();} catch (UnknownHostException e) {e.printStackTrace();return "";}return ip.getHostAddress();}......
}

2.log4j2.xml读取配置示例

<?xml version="1.0" encoding="UTF-8"?><configuration status="warn" monitorInterval="1000"><Properties><Property name="APP_NAME" value="${ctx:appName}"/><Property name="IP" value="${ctx:ip}"/><Property name="PORT" value="${ctx:port}"/><!-- 配置日志文件输出目录 --><Property name="LOG_HOME">/data/logs/${APP_NAME}</Property><Property name="LOG_NAME">${APP_NAME}</Property></Properties>......
</configuration>

SpringBoot+log4j2.xml读取application.yml属性值相关推荐

  1. springboot log4j2.xml读取application.yml中的属性值

    注意:部份代码太长,可以通过文末的图片快速查看对应位置 项目需求 用户想自己配置日志存放的位置,因此我们需要满足提供可以配置的文件,用以满足用户的需求. 因此,我们主要通过 log4j2.xml 来读 ...

  2. SpringBoot使用@Value读取Application.yml为null(疯狂踩坑)

    项目需求:在springBoot中有一个写死的url,发送http请求获取一个视频地址. 一开始的思路是这样的,然后考虑到这样写不够灵活,因为是springBoot项目,所以直接在配置文件中获取url ...

  3. SpringBoot项目使用@Value读取配置文件application.yml的值

    SpringBoot项目使用@Value读取配置文件application.yml的值 前景:因为之前用@Value注解都是在Service里面,而Service的调用都是用的@Autowired等注 ...

  4. Spring Boot读取application.yaml属性

    Spring Boot读取application.yaml属性 一.访问实体属性 1.pom.xml文件里添加snakeyaml依赖 <dependency><groupId> ...

  5. Springboot默认加载application.yml原理

    Springboot默认加载application.yml原理以及扩展 SpringApplication.run(-)默认会加载classpath下的application.yml或applicat ...

  6. applicationContext.xml读取properties中属性

    applicationContext.xml读取properties中属性 applicationContext.xml读取properties中属性两种方法: 原文:[https://www.ite ...

  7. SpringBoot中通过ConfigurationProperties注解的方式读取application.yml中配置的属性值

    场景 在SpringBoot后台项目中,某些固定的属性需要配置在配置文件application.yml中. 比如上传到服务器的文件路径. 然后在其他业务代码中比如上传文件接口中需要或者到配置的这个属性 ...

  8. log4j2.xml 获取当前系统属性

    官方文档: Log4j – Apache Log4j 2 获取微服务名称 Log4j Spring Boot Integration – Log4j Spring Boot Support <p ...

  9. logback-spring.xml读取spring的属性

    1. application.yml info:name: file-uploader-service # 日志目录(已过期,不建议使用) logging:path: home/logsfile: f ...

最新文章

  1. Windows 上看端口 找PID
  2. Android 四大组件之——Acitivity(四) Activity是如何加载显示内容的?
  3. Beta 冲刺 (5/7)
  4. wxWidgets:减少可执行文件大小
  5. VC读取注册表中二进制类型REG_BINARY的代码
  6. leetcode 485,487,1004. Max Consecutive Ones I ,II, III(最大连续1的个数问题合集)
  7. mysql开启慢查询方法(转)
  8. 负基础学python编程_【数据科学系统学习】Python # 编程基础[二]
  9. C/C++输入输出流
  10. ionic 支持float吗_机器人协会 | 一起拼机器人吗?
  11. php mysql 查询数据出现连接重置_php使用mysql和mysqli连接查询数据
  12. flask实现mysql连接池_mysql-Flask-SQLAlchemy-快速连接到多个数据库
  13. CTF逆向总结(二)
  14. Kubernets k8s中yml格式与pod yml格式
  15. Reasoning-RCNN 论文笔记
  16. Chrome扩展、装逼神器:Vimium
  17. 刚从阿里、头条面试回来,java处理excel文件数据
  18. 计算机作业我家乡的变化英语作文,我的变化英语作文3篇
  19. NS3中“E: package ‘gnuplot‘ has no installation candidate”问题解决方案
  20. 毕业一年,我是如何实现每月两千副业之路

热门文章

  1. 三分钟学会快速排序(图示讲解,附代码,通俗易懂)
  2. linux ubuntu 16.04 flash插件安装
  3. 中兴通讯联合SWAN Mobile打通斯洛伐克首个5G视频电话
  4. 家用计算机的ram怎么清理,计算机内存怎么清理
  5. 腾达无线路由器网关和域名服务器,腾达(Tenda)FH329路由器固定(静态)IP设置上网...
  6. oracle多表关联多字段update
  7. Linux命令大全总结(看这一篇就够了)
  8. | 名师博客 | 地址 |
  9. python读取股票数据_python2自动读取股票数据示例
  10. 近10年的网盘发展史(那些年你的网盘存了多少资料)