6. Spring Boot 2.x 最佳实践之 Apache Log4j2 集成

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
要在 Spring Cloud 中集成 Log4j2 日志框架,可以通过添加以下依赖项来实现: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-sleuth-zipkin</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-sleuth-stream</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-stream-rabbit</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-stream-binder-rabbit</artifactId> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>${log4j.version}</version> </dependency> ``` 然后在 `application.properties` 文件中添加以下配置: ``` spring.cloud.stream.bindings.output.destination=myTopic spring.sleuth.sampler.probability=1.0 logging.level.org.springframework.cloud.stream=DEBUG logging.level.org.springframework.integration=DEBUG logging.level.org.springframework.amqp=DEBUG spring.cloud.stream.bindings.input.destination=myTopic spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest spring.zipkin.baseUrl=http://localhost:9411 ``` 这里的配置包括了日志的级别以及 Sleuth 和 Zipkin 的配置。最后,可以使用以下代码来测试日志输出: ```java @Slf4j @RestController @RequestMapping("/api") public class DemoController { @GetMapping("/test") public String test() { log.debug("This is a debug message"); log.info("This is an info message"); log.warn("This is a warning message"); log.error("This is an error message"); return "Test"; } } ``` 如果一切顺利,你应该能够在控制台看到输出的日志信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

极客星云

谢谢认可,希望对你的学习有帮助

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值