logback整合rabbitmq实现消息记录日志的配置
更新时间:2023年12月13日 14:31:52 作者:asartear
这篇文章主要介绍了logback整合rabbitmq实现消息记录日志的配置,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
logback.xml文件配置
<!-- 配置rabbitmq的信息,数据取值yml --> <springProperty name="rabbitmqHost" source="spring.rabbitmq.host"/> <springProperty name="rabbitmqPort" source="spring.rabbitmq.port"/> <springProperty name="rabbitmqUsername" source="spring.rabbitmq.username"/> <springProperty name="rabbitmqPassword" source="spring.rabbitmq.password"/> <springProperty name="rabbitmqvirtualHost" source="spring.rabbitmq.virtual-host"/> <!-- 配置rabbitmq的日志记录appender --> <appender name="LogAmqpAppender" class="org.springframework.amqp.rabbit.logback.AmqpAppender"> <!--Layout(纯文本)而不是格式化的JSON --> <layout> <pattern> {"source":"%c-%L","createtime":"%d{yyyy-MM-dd HH:mm:ss}","thread":"%thread","level":"%-5level", "logTrackId": "%X{logTrackId}", "requestUri":"%X{requestUri}", "msg":"%msg"} </pattern> </layout> <host>${rabbitmqHost}</host> <port>${rabbitmqPort}</port> <username>${rabbitmqUsername}</username> <password>${rabbitmqPassword}</password> <virtualHost>${rabbitmqvirtualHost}</virtualHost> <declareExchange>true</declareExchange> <exchangeType>direct</exchangeType> <exchangeName>log.exchange.direct</exchangeName> <routingKeyPattern>logDirectQueue</routingKeyPattern> <generateId>true</generateId> <charset>UTF-8</charset> <durable>false</durable> <autoDelete>false</autoDelete> <deliveryMode>NON_PERSISTENT</deliveryMode> </appender> <!-- root 级别的配置 --> <root level="INFO"> <appender-ref ref="LogAmqpAppender" /> </root>
yml文件配置
# rabbitmq rabbitmq: host: 132.33.228.149 username: admin password: admin@2023$ port: 5673 #虚拟host 可以不设置,使用server默认host virtual-host: / connection-timeout: 10000 listener: simple: prefetch: 1 # 每次只能获取一条,处理完成才能获取下一条 acknowledge-mode: manual #一定改为要手动确认模式
代码配置rabbitmq信息
package com.xx.weixin.rabbitmq; import java.util.HashMap; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.DirectExchange; import org.springframework.amqp.core.Queue; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.core.RabbitAdmin; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author 作者 srp * @version 创建时间:2023-12-1 16:42:41 * */ @Configuration public class RabbitMqLogConfig { @Autowired private RabbitAdmin rabbitAdmin; //强制用此方法生成自己希望的队列 @Bean public Queue logDirectQueue() { //设置过期时间,以毫秒为单位 HashMap<String, Object> map = new HashMap<>(); map.put("x-message-ttl", 60000); //true,false跟logback.xml对应 Queue queue = new Queue( "logDirectQueue", true,false,false,map); rabbitAdmin.declareQueue(queue); return queue; } @Bean public DirectExchange logDirectExchange() { //true,false跟logback.xml对应 return new DirectExchange("log.exchange.direct", false, false); } /** * 根据路由键绑定队列到交换器上 * * @return */ @Bean public Binding logDirectBinding() { return BindingBuilder.bind(logDirectQueue()).to(logDirectExchange()).with("logDirectQueue"); } @Bean public RabbitAdmin rabbitAdmin(ConnectionFactory defaultConnectionFactory){ return new RabbitAdmin(defaultConnectionFactory); } }
测试客户端
package com.xx.pyt.confg; import java.io.IOException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.rabbit.annotation.Exchange; import org.springframework.amqp.rabbit.annotation.Queue; import org.springframework.amqp.rabbit.annotation.QueueBinding; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; import org.springframework.amqp.core.Message; import com.rabbitmq.client.Channel; ///** //* @author 作者 srp //* @version 创建时间:2023-12-1 16:45:44 //* //*/ @Component public class DirectConsumer { protected final Logger log = LoggerFactory.getLogger(this.getClass()); @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "logDirectQueue", durable = "false"), exchange = @Exchange(name = "log.exchange.direct", durable = "false", type = "direct"),key ="logDirectQueue")) @RabbitHandler public void handleMessage(String msg, Channel channel, Message message) throws IOException { try { // 处理消息逻辑 processMessage(msg); // 手动ack确认 channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); } catch (Exception e) { // 手动nack拒绝,并要求重新投递 channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true); log.error(e.getMessage(), e); } } private void processMessage(String message) { // 模拟处理消息过程 log.info("Processing message: " + message); } }
此外,如果要对日志进行链路标记,可以是用MDC
package com.xxm.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import org.slf4j.MDC; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Component; import com.ylkh.admin.entity.MyUser; import com.ylkh.security.dto.JwtUserDto; import cn.hutool.core.lang.UUID; import lombok.extern.slf4j.Slf4j; /** * @author 作者 srp * @version 创建时间:2022年1月4日 下午12:52:43 * */ @Slf4j @Component @WebFilter(urlPatterns = "/**",filterName = "tlFilter") public class TraceLogLocalFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { log.debug("链路过滤器初始化"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; if(SecurityContextHolder.getContext().getAuthentication()!=null&&!"anonymousUser".equals(SecurityContextHolder.getContext().getAuthentication().getPrincipal())) { //log.info(""+SecurityContextHolder.getContext().getAuthentication().getPrincipal()); JwtUserDto jwtUserDto = (JwtUserDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); MyUser myUser = jwtUserDto.getMyUser(); MDC.put("user", myUser.getUserName()); } String logtrackId = UUID.randomUUID(true).toString(); if(!request.getRequestURI().contains(".css")&&!request.getRequestURI().contains(".js")&&!request.getRequestURI().contains(".html")) //添加MDC日志 { MDC.put("logTrackId",logtrackId); MDC.put("requestUri", request.getRequestURI()); } try { filterChain.doFilter(servletRequest, servletResponse); }finally{ //移除MDC日志 MDC.remove(logtrackId); } // log.info("过滤器执行完成"); } @Override public void destroy() { log.warn("过滤器销毁"); } }
到此这篇关于logback整合rabbitmq实现消息记录日志的文章就介绍到这了,更多相关logback整合rabbitmq内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
基于spring data jpa @query返回map的踩坑记录
这篇文章主要介绍了基于spring data jpa @query返回map的踩坑记录,具有很好的参考价值,如有错误或未考虑完全的地方,望不吝赐教2021-11-11SpringBoot@Profile注解和Spring EL(多环境注入)
为了方便, Spring还提供了 Profile机制, 使我们可以很方便地实现各个环境之间的切换,在使用DI来依赖注入的时候,能够根据@profile标明的环境,将注入符合当前运行环境的相应的bean,本文通过示例代码介绍SpringBoot@Profile注解和Spring EL,需要的朋友可以参考下2024-02-02如何解决redis的NOAUTH Authentication required异常
这篇文章主要介绍了Jedis异常解决:NOAUTH Authentication required,,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值2019-07-07java Beanutils.copyProperties( )用法详解
这篇文章主要介绍了java Beanutils.copyProperties( )用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-05-05Hibernate Validator实现更简洁的参数校验及一个util
这篇文章主要介绍了Hibernate Validator实现更简洁的参数校验及一个util,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2019-05-05mybatis教程之resultmap_动力节点Java学院整理
这篇文章主要介绍了mybatis教程之resultmap,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-09-09
最新评论