sprinboot系列三——接入log4j2

导入依赖

log4j2依赖lmax完成异步日志,排除spring本身log包

    <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		<exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
	</dependency>
    	<!-- log4j2 -->
   <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.lmax/disruptor -->
<dependency>
    <groupId>com.lmax</groupId>
    <artifactId>disruptor</artifactId>
    <version>3.4.2</version>
</dependency>

添加配置

application.properties里面声明日志

logging.config= classpath:log4j2.xml

springboot默认扫描目录如下:

classpath:/static
classpath:/public
classpath:/resources
classpath:/META-INF/resources

根目录,添加log4j2.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
	<!-- \u53d8\u91cf\u914d\u7f6e -->
	<Properties>
		<Property name="log_path">./logs</Property>
	</Properties>

	<!-- appender\u914d\u7f6e -->
		<!-- uuid用于标识系列日志 -->
	<Appenders>
		<Console name="Console" target="SYSTEM_OUT">
			<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss} %5p %X{uuid} -- %m%n" />
		</Console>

		<RollingFile name="TEST.SYSTEM" fileName="${log_path}/system.log"
			filePattern="${log_path}/system.log.%d{yyyy-MM-dd}">
			<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss} %5p %X{uuid} -- %m%n" />
			<Policies>
		<CronTriggeringPolicy schedule="0 0 0 * * ? *" />
			</Policies>
			<Filters>
				<ThresholdFilter level="warn" onMatch="DENY"
					onMismatch="NEUTRAL" />
				<ThresholdFilter level="info" onMatch="ACCEPT"
					onMismatch="NEUTRAL" />
			</Filters>
			<DefaultRolloverStrategy max="7">
		<Delete basePath="${log_path}" maxDepth="3">
			   <IfFileName glob="*.log.*" />
         	 <IfLastModified age="7d" />
        </Delete>
			</DefaultRolloverStrategy>
		</RollingFile>


		<RollingFile name="TEST.ERROR" fileName="${log_path}/error.log"
			filePattern="${log_path}/error.log.%d{yyyy-MM-dd}">
			<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss} %5p %X{uuid} -- %m%n" />
			<Policies>
				<CronTriggeringPolicy schedule="0 0 0 * * ? *" />
			</Policies>
			<Filters>
				<ThresholdFilter level="error" onMatch="ACCEPT"
					onMismatch="NEUTRAL" />
				<ThresholdFilter level="trace" onMatch="DENY"
					onMismatch="NEUTRAL" />
			</Filters>
			<DefaultRolloverStrategy max="7"/>
		</RollingFile>

	</Appenders>
	<Loggers>

     <AsyncLogger name="com.test" level="info" additivity="false">
        <AppenderRef ref="TEST.SYSTEM" />
        <AppenderRef ref="TEST.ERROR" />
        <AppenderRef ref="Console" level="info" />
    </AsyncLogger>
     
     
		<Root level="info">
			<AppenderRef ref="Console" level="info" />
			<AppenderRef ref="TEST.SYSTEM" />
			<AppenderRef ref="TEST.ERROR" />
		</Root>
	</Loggers>
</Configuration>

接入uuid

在全局拦截类里面,将生成的uuid放入即可

	MDC.clear();
	MDC.put("uuid", StringUtils.getUUID());
### Spring Boot 整合 MQTT 实现步骤 #### 1. 添加依赖 在 `pom.xml` 文件中添加必要的 Maven 依赖项来引入 Eclipse Paho 客户端库以及 Spring Integration 的 MQTT 支持模块[^1]。 ```xml <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-mqtt</artifactId> </dependency> <dependency> <groupId>org.eclipse.paho</groupId> <artifactId>org.eclipse.paho.client.mqttv3</artifactId> </dependency> ``` #### 2. 配置 Application Properties 编辑项目的 `application.yml` 或者 `application.properties` 来定义 MQTT 连接参数,比如服务器地址、用户名密码等信息[^5]。 ```yaml spring: integration: mqtt: default-url: tcp://localhost:1883 username: user password: passw0rd ``` #### 3. 创建 MqttConfig 类 编写一个名为 `MqttConfig.java` 的 Java 类用于配置 MQTT 生产者和消费者的相关设置。这里会涉及到创建消息通道、监听器容器工厂等内容[^4]。 ```java @Configuration public class MqttConfig { @Value("${spring.integration.mqtt.default-url}") private String brokerUrl; @Bean public DefaultMqttPahoClientFactory mqttClientFactory() { DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory(); // 设置连接选项... return factory; } @Bean public MessageChannel mqttInputChannel() { return new DirectChannel(); } } ``` #### 4. 编写消息处理逻辑 对于想要响应特定主题下的消息的应用程序组件来说,可以通过实现 `MessageListenerInterface` 接口并注册至相应的监听器适配器上来完成这一操作;而对于需要主动发布数据的情况,则可以直接利用前面提到过的模板对象来进行调用[^3]。 ```java @Service @RequiredArgsConstructor @Slf4j public class MyService implements InitializingBean, ChannelAwareMessageListener<String> { private final MqttPahoMessageDrivenChannelAdapter adapter; @Override public void afterPropertiesSet() throws Exception { this.adapter.setTopic("test/topic"); log.info("Subscribed to topic 'test/topic'"); } @Override public void onMessage(Message<?> message, MessageChannel channel) { log.info("Received message: {}", message.getPayload()); } } ``` #### 5. 测试验证 最后一步就是确保一切工作正常——启动应用程序之后应该能够看到它成功订阅到了指定的主题上,并且当有新消息到来时也会触发对应的回调方法执行[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值