hikari配置(SpringBoot自带)

使用的时候需要将注释“#”去掉。

#hikari数据库连接池配置
#spring:
  #datasource:
    #driver-class-name: com.mysql.cj.jdbc.Driver
    #url: jdbc:mysql://localhost:3306/asset?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    #username: root
    #password: 123456
    #数据库连接池配置
    #hikari:
      #连接池名
      #pool-name: DateHikariCP
      # 最小空闲数
      #minimum-idle: 5
      # 空闲超时时间;默认是十分钟;空闲时间超过设定时间则会被回收
      #idle-timeout: 600000
      # 最大连接数
      #maximum-pool-size: 10
      # 是否自动提交
      #auto-commit: true
      # 最大存活时间,默认30分钟
      #max-lifetime: 1800000
      #连接超时时间
      #connection-timeout: 30000
      # 连接数据库后测试语句
      #connection-test-query: SELECT 1

使用SpringBoot自带的logback记录日志

需要在resources目录下新建logback-spring.xml文件,于yml文件处于同级。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!-- 日志保存目录 -->
    <!-- 这里经过测试“\\”才能到指定位置创建日志文件,这里大家可以测试一下“\”和“/”都不行  -->
    <property name="LOG_PATH" value="D:\\WorkPlace\\exercise_asset_management_platform_backend\\logs"></property>

    <!-- 配置日志输出格式,调用方式:${LOG_PATTERN} -->
    <property name="LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n"></property>

    <!-- 配置系统日志输出 -->
    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>debug</level>
        </filter>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%d - %c: %L - %msg%n</pattern>
        </layout>
    </appender>

    <!-- 指定项目中某个类的日志输出到指定文件-->
    <!--DEBUG日志配置-->
    <appender name="fileDebugLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印info日志 -->
            <level>DEBUG</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <append>true</append>
        <!-- 日志文件的记录格式 -->
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %m%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
        <!-- 日志文件的命名格式 -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH}/asset.debug.%d{yyyy-MM-dd}.log</fileNamePattern>
            <!--日志回滚,超过多少个文件删除历史日志文件 -->
            <!--<maxHistory>15</maxHistory>-->
        </rollingPolicy>
        <!-- 日志文件的大小 -->
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>10MB</maxFileSize>
        </triggeringPolicy>
    </appender>

    <!--根据日志级别分离日志,分别输出到不同的文件-->
    <!-- info日志配置 -->
    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter"> <!-- 只打印info日志 -->
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <append>true</append>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %m%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH}/asset.info.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>5</maxHistory>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>10MB</maxFileSize>
        </triggeringPolicy>
    </appender>

    <!-- warn日志配置 -->
    <appender name="fileWarnLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>WARN</level>
        </filter>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH}/asset.warn.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>5</maxHistory>
        </rollingPolicy>
        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印警告日志 -->
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!-- error日志配置-->
    <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH}/asset.error.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>5</maxHistory>
        </rollingPolicy>
        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印错误日志 -->
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!-- 根据设置日志级别,屏蔽相关的日志:以下包及子包的日志会屏蔽掉-->
    <logger name="org.springframework" level="warn"></logger>
    <logger name="com.alibaba.druid" level="warn"></logger>
    <logger name="org.apache" level="warn"></logger>

    <!-- 设置指定类的日志级别-->
    <!--<logger name="com.rec.recserver.controller.UserController" level="info" additivity="false">
        <appender-ref ref="recInfoLog" />
    </logger>-->

    <!-- 项目中info级别的日志会输出到以下日志对象中 -->
    <root level="info">
        <appender-ref ref="consoleLog" />
        <appender-ref ref="fileInfoLog" />
        <appender-ref ref="fileErrorLog" />
        <!-- 这里注释了recInfoLog就只保存你想输出的日志,不会保存项目及其他的相关日志>-->
        <appender-ref ref="fileDebugLog"  />
    </root>
</configuration>

Druid配置

1.pom.xml文件引入依赖。

<!--druid数据库连接池依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.11</version>
        </dependency>

2.yml文件配置

########## 配置数据源 (Druid)##########
spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/asset?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    # 下面为连接池的补充设置,应用到上面所有数据源中
    # 初始化大小,最小,最大
    druid:
      initial-size: 5
      max-active: 20
      min-idle: 5
      time-between-eviction-runs-millis: 60000
      max-wait: 60000
      min-evictable-idle-time-millis: 300000
      validation-query: SELECT 1
      test-while-idle: true
      testOnBorrow: false
      test-on-return: false
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 20
      #配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
      #如果允许时报错 java.lang.ClassNotFoundException:org.apache.log4j.Priority
      #则导入 log4j 依赖即可
      filters: stat,wall,slf4j
      filter:
        stat:
          log-slow-sql: true
          slow-sql-millis: 2000
          db-type: mysql
        slf4j:
          enabled: true
          statement-log-error-enabled: true
          statement-create-after-log-enabled: false
          statement-close-after-log-enabled: false
          result-set-open-after-log-enabled: false
          result-set-close-after-log-enabled: false
      use-global-data-source-stat: true
      connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
      #SQL监控
      stat-view-servlet:
        enabled: true
        login-username: admin
        login-password: 123456
        allow:
        deny:
        reset-enable: false
        url-pattern: /druid/*
      #web拦截
      web-stat-filter:
        url-pattern: /*
        exclusions: "*.js,*.gif,*.jpg,*.css,/druid/*"
        enabled: true
        session-stat-enable: true       # 开启session统计功能
        session-stat-max-count: 1000    # session的最大个数,默认100
      aop-patterns: com.eccom.asset.mapper.* #Spring监控开启
      #time-between-log-stats-millis: 1000 #记录持久化

Logo

华为云1024程序员节送福利,参与活动赢单人4000元礼包,更有热门技术干货免费学习

更多推荐