logback读取application.yml中的值实现多环境配置

 

application.ym的配置:

# 日志配置  为空为项目跟目录下的logs  或者指定已经存在的目录
log:
  path:

 

logback配置

<?xml version="1.0" encoding="UTF-8"?>
<!--
说明:
    1. 文件的命名和加载顺序有关
       logback.xml早于application.yml加载,logback-spring.xml晚于application.yml加载
       如果logback配置需要使用application.yml中的属性,需要命名为logback-spring.xml
    2. logback使用application.yml中的属性
       使用springProperty才可使用application.yml中的值 可以设置默认值

-->
<configuration scan="true" scanPeriod="60 seconds">

    <!-- log base path -->
    <springProperty scope="context" name="logPath" source="log.path" defaultValue="logs"/>
    <!-- log name -->
    <property name="LOG_HOME" value="${logPath}"/>
    <!-- back log base path -->
    <property name="LOG_BACK_HOME" value="${logPath}/backup"/>

    <property name="SRVNAME" value="clsapi-console"/>
    <!-- 文件切割大小 -->
    <property name="maxFileSize" value="100MB" />
    <!-- 文档保留天数 -->
    <property name="maxHistory" value="60" />
    <!-- 文档保留总大小 -->
    <property name="totalSizeCap" value="10GB" />


    <!-- 系统服务日志 -->
    <appender name="FILE"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_HOME}/${SRVNAME}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>${LOG_BACK_HOME}/%d{yyyy-MM-dd}/${SRVNAME}.%d{HH}.%i.log.gz</fileNamePattern>
            <!-- 单个日志文件最多 100MB, 60天的日志周期,最大不能超过10GB -->
            <maxFileSize>${maxFileSize}</maxFileSize>
            <maxHistory>${maxHistory}</maxHistory>
            <totalSizeCap>${totalSizeCap}</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyyMMdd HH:mm:ss.SSS} %X{LOG_ID} [%thread] %-5level %logger{100}.%method\(\):%L - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- On Windows machines setting withJansi to true enables ANSI
         color code interpretation by the Jansi library. This requires
         org.fusesource.jansi:jansi:1.8 on the class path.  Note that
         Unix-based operating systems such as Linux and Mac OS X
         support ANSI color codes by default.
          recognizes "%black", "%red", "%green","%yellow","%blue",
          "%magenta","%cyan", "%white", "%gray", "%boldRed","%boldGreen",
          "%boldYellow", "%boldBlue", "%boldMagenta""%boldCyan",
          "%boldWhite" and "%highlight"
          -->
        <!--withJansi>true</withJansi-->
        <encoder>
            <!--%d{yyyy-MM-dd HH:mm:ss.SSS} -%5p ${PID:-} [%15.15t] %-40.40logger{39} : %m%n-->
            <pattern>%boldCyan(%d{yyyy-MM-dd HH:mm:ss.SSS}) - %boldRed(%5p) %blue([%10.10t]) %magenta(%-35.35logger{20}) %yellow(%2M) %green(%2L) : %msg%n</pattern>
            <!--<pattern>%d{yyyyMMddHHmmss} [%thread] [%c %2M %2L] %-3p - %m%n</pattern>-->
        </encoder>
    </appender>

    <root level="info">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="FILE"/>
    </root>

    <logger name="com.hopebank.clsapi.aspect.LogsAspect" level="debug">
        <appender-ref ref="api_call_file"/>
    </logger>
    <logger name="org.springframework.web.servlet" level="info"/>   

</configuration>

转载于:https://my.oschina.net/skyzwg/blog/915527

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot应用,可以通过在application.yml文件配置,实现打印JDBC的SQL语句的功能。 具体的实现步骤如下: 1. 在application.yml文件,配置以下信息: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/test username: root password: root driver-class-name: com.mysql.jdbc.Driver # 打印SQL语句 # 可选的:NONE、CONSOLE、LOG_FILE、LOG_FILE_AND_CONSOLE # 默认:NONE # 可以使用logging.level.org.springframework.jdbc.core.JdbcTemplate=DEBUG代替 #logging: # level: # org: # springframework: # jdbc: # core: # JdbcTemplate: DEBUG datasource: type: com.alibaba.druid.pool.DruidDataSource # 配置Druid数据源,可以省略 druid: initial-size: 5 max-active: 20 min-idle: 5 max-wait: 60000 pool-prepared-statements: true max-pool-prepared-statement-per-connection-size: 20 validation-query: SELECT 1 test-on-borrow: true test-while-idle: true time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 300000 filters: stat,wall,log4j connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 ``` 其,logging.level.org.springframework.jdbc.core.JdbcTemplate=DEBUG可以在application.yml配置,也可以在log4j或logback等日志框架配置。 2. 在代码使用JdbcTemplate时,即可在日志看到打印的SQL语句,例如: ```java @Autowired private JdbcTemplate jdbcTemplate; public void query() { List<Map<String, Object>> result = jdbcTemplate.queryForList("SELECT * FROM user"); //... } ``` 在日志会打印出类似以下的SQL语句: ```java DEBUG [main] o.s.j.c.JdbcTemplate - Executing SQL query [SELECT * FROM user] ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值