Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Multi-environment configuration of Internal and external logback in Springboot

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

这篇文章给大家分享的是有关Springboot内外部logback多环境配置的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

一、概述

SpringBoot官方文档

Springboot 默认读取的是项目内的 resources 中 logback 配置文件。

如果 classpath(resources) 下有logback-test.xml会优先生效,并且会和其他logback文件同时生效。

Springboot 默认日志级别是DEBUG,所以在logback初始化之前,会有DEBUG日志输出。

二、项目内外配置

注意:logback-{xxx}.xml不能直接放在resources目录下,否则logback-test.xml会优先生效。

1.application.yml指定 activeprofiles

# Spring配置spring: profiles: active: test

2.activeprofiles指定logback-{xxx}.xml

########## logback-{xxx}.xml 文件打包在项目内部 ##########logging: config: classpath:logback/logback-test.xml########## logback-{xxx}.xml 文件打包在项目外部(去掉classpath) ##########logging: config: /Users/wangfugui/Downloads/logback/logback-test.xml

3.logback-{xxx}.xml

${log.pattern} ${log.basePath}/${log.path}/sys-info.log ${log.basePath}/${log.path}/sys-info.%d{yyyy-MM-dd}.log 60 ${log.pattern} INFO ACCEPT DENY ${log.basePath}/${log.path}/sys-error.log ${log.basePath}/${log.path}/sys-error.%d{yyyy-MM-dd}.log 60 ${log.pattern} ERROR ACCEPT DENY ${log.basePath}/${log.path}/sys-user.log ${log.basePath}/${log.path}/sys-user.%d{yyyy-MM-dd}.log 60 ${log.pattern} 三、使用自定义appender

使用Lombok

Lombok 内置 @Slf4j 、@Log4j2 两种日志注解。

@Log4j2(topic = "sys-user")@Slf4j(topic = "sys-user")

不使用Lombok

Logger logger = LoggerFactory.getLogger("sys-user");四、logback查找配置源码

org.springframework.boot.logging.logback.LogbackLoggingSystem

protected String[] getStandardConfigLocations() { return new String[]{"logback-test.groovy", "logback-test.xml", "logback.groovy", "logback.xml"}; }感谢各位的阅读!关于"Springboot内外部logback多环境配置的示例分析"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report