SpringBoot开发【日志】指定日志文件和日志profile功能

本文详细介绍了在SpringBoot中如何指定配置不同的日志框架,包括logback.xml和logback-spring.xml的使用方法,以及如何利用springProfile标签实现环境特定的日志配置。

此文章原创作者为level_Tiller

指定配置

给类路径下放上每个日志框架自己的配置文件即可,springboot就不使用他默认的配置了:规则如下

logback.xml:直接就被日志框架识别了。

logback-spring.xml:日志框架不直接加载日志的配置项,由springboot解析日志配置,可以使用springboot的高级profile功能。可以指定某段配置只在某个环境下生效。(推荐使用xxx-spring.xml)

<springProfile name="staging">
	<!-- configuration to be enabled when the "staging" profile is active -->
</springProfile>

<springProfile name="dev | staging">
	<!-- configuration to be enabled when the "dev" or "staging" profiles are active -->
</springProfile>

<springProfile name="!production">
	<!-- configuration to be enabled when the "production" profile is not active -->
</springProfile>

(类似于if条件判断写在xml里面吧)

 

  •                     <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#csdnc-thumbsup"></use>
                        </svg><span class="name">点赞</span>
                        <span class="count"></span>
                        </a></li>
                        <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-csdnc-Collection-G"></use>
                        </svg><span class="name">收藏</span></a></li>
                        <li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-csdnc-fenxiang"></use>
                        </svg>分享</a></li>
                        <!--打赏开始-->
                                                <!--打赏结束-->
                                                <li class="tool-item tool-more">
                            <a>
                            <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                            </a>
                            <ul class="more-box">
                                <li class="item"><a class="article-report">文章举报</a></li>
                            </ul>
                        </li>
                                            </ul>
                </div>
                            </div>
            <div class="person-messagebox">
                <div class="left-message"><a href="https://blog.youkuaiyun.com/level_Tiller">
                    <img src="https://profile.csdnimg.cn/9/E/0/3_level_tiller" class="avatar_pic" username="level_Tiller">
                                            <img src="https://g.csdnimg.cn/static/user-reg-year/2x/3.png" class="user-years">
                                    </a></div>
                <div class="middle-message">
                                        <div class="title"><span class="tit"><a href="https://blog.youkuaiyun.com/level_Tiller" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">level_Tiller</a></span>
                                            </div>
                    <div class="text"><span>发布了151 篇原创文章</span> · <span>获赞 4</span> · <span>访问量 1万+</span></div>
                </div>
                                <div class="right-message">
                                            <a href="https://im.youkuaiyun.com/im/main.html?userName=level_Tiller" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                        </a>
                                                            <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                    </div>
                            </div>
                    </div>
    
<think>好的,用户问的是如何在Spring Boot中配置多个日志文件。我需要先回忆一下Spring Boot的日志配置方式,特别是关于多日志文件的部分。 首先,Spring Boot默认使用Logback作为日志框架,所以配置多日志文件应该通过Logback的配置文件来实现。用户提到的application.properties中的配置可能只能设置单个日志文件,但想要多个的话,可能需要自定义logback-spring.xml。 然后,我得考虑用户的需求。他们可能希望不同的日志信息输出到不同的文件里,比如按日志级别或者按模块分开。这时候需要在Logback里配置多个appender,每个appender向不同的文件,并且设置不同的日志规则。 接下来,示例代码部分需要展示如何编写logback-spring.xml,定义多个文件appender,并分配给不同的logger。比如,一个记录info级别到app.log,另一个记录error到error.log,或者按包名区分。 还要注意是否需要引用Spring Boot的默认配置,或者覆盖默认设置。可能需要包含<include resource="org/springframework/boot/logging/logback/defaults.xml"/>来保留Spring Boot的默认配置,比如控制台输出。 另外,用户可能希望看到如何在不同的环境中使用不同的日志配置,比如通过Spring Profile来切换。不过用户的问题主要集中在多文件,所以可能暂时不需要涉及Profile部分。 最后,要确保示例中的路径文件名正确,比如使用${LOG_PATH}${LOG_FILE}这样的变量,或者在配置中直接定路径。同时,提醒用户需要在application.properties中设置logging.file.path或logging.file.name,但多文件配置主要还是依赖logback的配置。 检查是否有遗漏,比如RollingPolicy是否配置正确,避免日志文件过大。同时,生成的相关问题要考虑用户可能的后续疑问,比如如何按模块分日志,如何设置不同级别,以及性能影响等。</think>以下是如何在Spring Boot中配置多个日志文件的详细实现方案: ### 一、配置原理 Spring Boot通过Logback实现日志管理,多日志文件需要自定义`logback-spring.xml`配置文件,通过定义多个`appender`实现分流[^2][^3]。 ### 二、具体实现步骤 1. **创建配置文件** 在`src/main/resources`目录下新建`logback-spring.xml` 2. **配置多文件输出示例** ```xml <?xml version="1.0" encoding="UTF-8"?> <configuration scan="true"> <include resource="org/springframework/boot/logging/logback/defaults.xml"/> <!--日志文件 --> <appender name="MAIN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_PATH}/application.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>${LOG_PATH}/application.%d{yyyy-MM-dd}.%i.log</fileNamePattern> <maxFileSize>50MB</maxFileSize> <maxHistory>30</maxHistory> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <!-- 错误日志文件 --> <appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_PATH}/error.log</file> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>ERROR</level> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>${LOG_PATH}/error.%d{yyyy-MM-dd}.%i.log</fileNamePattern> <maxFileSize>50MB</maxFileSize> <maxHistory>30</maxHistory> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <!-- 控制台输出 --> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5level) %clr(${PID:- }){magenta} [%15.15thread] %clr(%-40.40logger{39}){cyan} : %msg%n</pattern> </encoder> </appender> <!-- 日志分配策略 --> <root level="INFO"> <appender-ref ref="CONSOLE"/> <appender-ref ref="MAIN_FILE"/> <appender-ref ref="ERROR_FILE"/> </root> </configuration> ``` 3. **application.properties配置** ```properties # 设置日志存储目录 logging.file.path=/var/log/myapp # 启用自定义配置 logging.config=classpath:logback-spring.xml ``` ### 三、验证配置 ```java @RestController public class LogDemoController { private static final Logger logger = LoggerFactory.getLogger(LogDemoController.class); @GetMapping("/test") public String test() { logger.info("This goes to application.log"); logger.error("This goes to error.log"); return "Check log files"; } } ``` ### 四、配置说明 1. **文件分割策略**:采用`SizeAndTimeBasedRollingPolicy`实现按日期大小滚动 2. **日志分级存储**:通过`ThresholdFilter`实现ERROR级别单独存储 3. **保留策略**:`maxHistory=30`表示保留30天日志[^3] 4. **性能优化**:异步写入可通过`AsyncAppender`实现(需额外配置)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值