java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > springboot log 多环境配置

springboot log多环境配置方式

作者:一克小白菜

这篇文章主要介绍了springboot log多环境配置方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

前言 

bootstrap.yml(bootstrap.properties)与application.yml(application.properties)执行顺序

1.第一种方法

直接写在application.yml里面,也能适配环境修改,但是只能使用默认一些策略,比如10M切分一个文件。

#日志配置
logging:
  level:
    root: INFO
  file: /home/xxx/logs/xxx.log

2.第二种方法

logback-spring.xml

<?xml version="1.0" encoding="utf-8"?>
<!--  Level	描述
     ALL	各级包括自定义级别
     ERROR	错误事件可能仍然允许应用程序继续运行
     FATAL	指定非常严重的错误事件,这可能导致应用程序中止
     INFO	指定能够突出在粗粒度级别的应用程序运行情况的信息的消息
     OFF	这是最高等级,为了关闭日志记录
    TRACE	指定细粒度比DEBUG更低的信息事件
    WARN	指定具有潜在危害的情况
    scan:当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true。
    scanPeriod:设置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒。当scan为true时,此属性生效。默认的时间间隔为1分钟。
    debug:当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false。-->
<configuration debug="true" scan="true" scanPeriod="30 seconds">
    <!-- 存放日志文件路径  该 <springProperty> 标签允许我们从Spring中显示属性-->
    <springProperty scope="context" name="logHome" source="log.path"/>
    <!-- ch.qos.logback.core.ConsoleAppender 控制台输出 -->
    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
            <pattern>%d [%thread] %-5level %logger{36} - %msg%n</pattern>
        </layout>
    </appender>
    <!-- INFO级别 -->
    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 级别过滤器:如果只是想要INFO级别的日志,只是过滤info还是会输出ERROR日志,因为ERROR的级别高,
        所以我们使用下面的策略,可以避免输出ERROR的日志-->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <!-- 过滤ERROR -->
            <level>ERROR</level>
            <!-- 匹配到就禁止 -->
            <onMatch>DENY</onMatch>
            <!-- 没有匹配到就允许 -->
            <onMismatch>ACCEPT</onMismatch>
        </filter>
        <encoder>
            <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
            <pattern>%d [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
        <!--日志名称,如果没有File 属性,那么只会使用FileNamePattern的文件路径规则,如果同时有<File>和<FileNamePattern>,
        那么当天日志是<File>,明天会自动把今天的日志改名为今天的日期。即<File> 的日志都是当天的。-->
        <!-- 根据时间来制定滚动策略 TimeBasedRollingPolicy-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--路径-->
            <fileNamePattern>${logHome}/%d/info/info.log</fileNamePattern>
            <!--保存最大天数-->
            <maxHistory>30</maxHistory>
        </rollingPolicy>
    </appender>
    <!-- ERROR级别-->
    <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!--如果只是想要 ERROR 级别的日志,那么需要过滤一下,默认是 INFO 级别的,ThresholdFilter-->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>
        <encoder>
            <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
            <pattern>%d [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
        <!-- 基于规模和时间的滚动策略 SizeAndTimeBasedRollingPolicy-->
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!--路径-->
            <fileNamePattern>${logHome}/%d/error/error.%i.log</fileNamePattern>
            <!--保存最大天数-->
            <maxHistory>30</maxHistory>
            <!--文件最大值-->
            <maxFileSize>2MB</maxFileSize>
            <!--总大小上限-->
            <totalSizeCap>10MB</totalSizeCap>
        </rollingPolicy>
    </appender>
    <root level="info">
        <appender-ref ref="consoleLog"/>
        <appender-ref ref="fileInfoLog"/>
        <appender-ref ref="fileErrorLog"/>
    </root>
</configuration>

一开始在application.yml 配置了log路径,但是springProperty 标签里面并没有获取到,会见在jar包存放的文件夹下,新建logHome_IS_UNDEFINED/%d/info/info.log,看下启动日志(一部分):

13:44:32,739 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@992086987 - No compression will be used
13:44:32,742 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@992086987 - Will use the pattern logHome_IS_UNDEFINED/%d/info/info.log for the active file
13:44:32,747 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd' from file name pattern 'logHome_IS_UNDEFINED/%d/info/info.log'.
13:44:32,748 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.
13:44:32,752 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Tue Aug 21 13:44:32 CST 2018
13:44:32,753 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[fileInfoLog] - Active log file name: logHome_IS_UNDEFINED/2018-08-21/info/info.log
13:44:32,753 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[fileInfoLog] - File property is set to [null]
13:44:32,757 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
13:44:32,757 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [fileErrorLog]
13:44:32,758 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
13:44:32,763 |-INFO in c.q.l.core.rolling.SizeAndTimeBasedRollingPolicy@737104370 - Archive files will be limited to [2 MB] each.
13:44:32,764 |-INFO in c.q.l.core.rolling.SizeAndTimeBasedRollingPolicy@737104370 - No compression will be used
13:44:32,764 |-INFO in c.q.l.core.rolling.SizeAndTimeBasedRollingPolicy@737104370 - Will use the pattern logHome_IS_UNDEFINED/%d/error/error.%i.log for the active file
13:44:32,765 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@30f5a68a - The date pattern is 'yyyy-MM-dd' from file name pattern 'logHome_IS_UNDEFINED/%d/error/error.%i.log'.

所以我在bootstrap.yml配置log.path来解决这个问题。

log:
  path: F:/logs/local-preferential
---
spring:
  profiles: test
log:
    path: /home/xxx-test/logs
---
spring:
  profiles: prod
log:
  path: /home/xxx/logs

启动打印log配置日志(一部分)

13:43:03,375 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@224192895 - No compression will be used
13:43:03,376 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@224192895 - Will use the pattern F:/logs/local-preferential/%d/info/info.log for the active file
13:43:03,377 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd' from file name pattern 'F:/logs/local-preferential/%d/info/info.log'.
13:43:03,377 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.
13:43:03,377 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Tue Aug 21 13:43:03 CST 2018
13:43:03,377 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[fileInfoLog] - Active log file name: F:/logs/local-preferential/2018-08-21/info/info.log
13:43:03,378 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[fileInfoLog] - File property is set to [null]
13:43:03,378 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
13:43:03,378 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [fileErrorLog]
13:43:03,379 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
13:43:03,381 |-INFO in c.q.l.core.rolling.SizeAndTimeBasedRollingPolicy@1883788127 - Archive files will be limited to [2 MB] each.
13:43:03,381 |-INFO in c.q.l.core.rolling.SizeAndTimeBasedRollingPolicy@1883788127 - No compression will be used
13:43:03,382 |-INFO in c.q.l.core.rolling.SizeAndTimeBasedRollingPolicy@1883788127 - Will use the pattern F:/logs/local-preferential/%d/error/error.%i.log for the active file
13:43:03,382 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@e3994ef - The date pattern is 'yyyy-MM-dd' from file name pattern 'F:/logs/local-preferential/%d/error/error.%i.log'.

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文