目录
- 基本概念
- idea控制台sql打印(myBATisplus)
- application.yml配置日志文件
- logback-spring.XML 详细配置
- 验证
基本概念
日志级别从低到高分为:
TRACE < DEBUG < INFO < WARN < ERROR < FATAL
如果设置为INFO,则低于INFO级别的信息都不会输出
默认情况下,Spring Boot项目就会用Logback来记录日志,并用INFO级别输出到控制台,因为spring-boot-starter 中包含了 spring-boot-starter-logging,该依赖内容就是 Spring Boot 默认的日志框架 logback
话不多说,下面直接贴配置
idea控制台sql打印(mybatisplus)
在日常开发中,我们需要在开发工具idea控制台打印sql,用mybatisplus框架的同学在yml文件加以下配置就可以实现打印sql
mybatis-plus: mapper-locations: classpath:/mapper/**/*.xml # 实体扫描,多个package用逗号或者分号分隔 typeAliasesPackage: com.daztk.mes.*.* global-config: #数据库相关配置 db-config: #主键类型 AUTO:"数据库ID自增", INPUT:"用户输入ID", ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID"; id-type: ID_WORKER banner: false #原生配置 configuration: map-underscore-to-camel-case: true cache-enabled: false call-setters-on-nulls: true jdbc-type-for-null: 'null'
application.yml配置日志文件
logging: config: classpath:logback-spring.xml
logback-spring.xml 详细配置
<?xml version="1.0" encoding="UTF-8"?> <configuration debug="false" scan="true" scanPeriod="10 seconds"> <!-- 日志级别从低到高分为TRACE < DEBUG < INFO < WARN < ERROR < FATAL,如果设置为INFO,则低于INFO级别的编程客栈信息都不会输出 --> <!-- scan:默认值为true,当此属性设置为true时,配置文件如果发生改变,将会被重新加载 --> <!-- scanPeriod:设置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒。当scan为true时,此属性生效。默认的时间间隔为1分钟。 --> <!-- debug:默认值为false。当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。 --> <contextName>logback</contextName> <!--设置属性 --> <property name="APPLICATION_NAME" value="mes"/> <property name="path" value="/logs/mes" /> <property name="maxHistory" value="30" /> <property name="maxFileSize" value="100MB" /> <!-- 彩色日志 --> <!-- 彩色日志依赖的渲染类 --> <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" /> <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" /> <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedwhitespaceThrowableProxyConverter" /> <!-- 彩色日志格式 --> <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39} - [%3line]){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/> <!--输出到控制台--> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>debug</level> </filter> <encoder> <Pattern>${CONSOLE_LOG_PATTERN}</Pattern> <charset>UTF-8</charset> </encoder> </appender> <!-- debug日志输出文件 --> <appender name="debug_file" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${path}/debug.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 每天对日志归档 --> <fileNamePattern>${path}/%d{yyyyMMdd}/debug-%d{yyyyMMdd}-%i.log</fileNamePattern> <maxHistory>${maxHistory}</maxHistory> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>${maxFileSize}</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger [%file : %line] - %msg%n</pattern> <charset>UTF-8</charset> </encoder> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>DEBUG</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <!-- info日志输出文件 --> <appender name="info_file" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${path}/info.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 每天对日志归档 --> <fileNamePattern>${path}/%d{yyyyMMdd}/info-%d{yyyyMMdd}-%i.log</fileNamePattern> <maxHistory>${maxHistory}</maxHistory> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>${maxFileSize}</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger [%file : %line] - %msg%n</pattern> <charset>UTF-8</charset> </encoder> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <!-- warn日志输出文件 --> <appender name="warn_file" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${path}/warn.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 每天对日志归档 --> <fileNamePattern>${path}/%d{yyyyMMdd}/warn-%d{yyyyMMdd}-%i.log</fileNamePattern> <maxHistory>${maxHistory}</maxHistory> <timeBasedFileNa编程客栈mingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>${maxFileSize}</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <gUeXbJetqNpattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger [%file : %line] - %msg%n</pattern> <charset>UTF-8</charset> </encoder> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>WARN</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <!-- error日志输出文件 --> <appender name="error_file" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${path}/error.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 每天对日志归档 --> <fileNamePattern>${path}/%d{yyyyMMdd}/error-%d{yyyyMMdd}-%i.log</fileNamePattern> <maxHistory>${maxHistory}</maxHistory> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>${maxFileSize}</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger [%file : %line] - %msg%n</pattern> <charset>UTF-8</charset> </encoder>编程客栈; <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>ACCEPT</onMatch> python <onMismatch>DENY</onMismatch> </filter> </appender> <!--全量日志--> <appender name="detail_file" class="ch.qos.logback.core.rolling.RollingFileAppender" additivity="false"> <File>${path}/${APPLICATION_NAME}.log</File> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger [%file : %line] - %msg%n</pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${path}/%d{yyyyMMdd}/${APPLICATION_NAME}-%d{yyyyMMdd}.log</fileNamePattern> </rollingPolicy> </appender> <!--项目源码包以debug级别输出,便于开发调试--> <logger name="com.mes" level="debug" /> <!--第三方包以info级别输出,减少日志量 --> <logger name="org.springframework" level="info" /> <!--开发环境:打印控制台--> <springProfile name="dev"> <logger name="com.daztk.mes.module" level="debug"/> </springProfile> <springProfile name="!dev"> <logger name="com.daztk.mes.module" level="debug"/> </springProfile> <root> <level value="info"/> <appender-ref ref="console"/> <appender-ref ref="debug_file"/> <appender-ref ref="info_file"/> <appender-ref ref="warn_file"/> <appender-ref ref="error_file"/> <appender-ref ref="detail_file"/> </root> </configuration>
验证
启动自己的项目,访问后台接口,然后会看到你的日志文件夹下有以下文件
在需要打印日志的Java文件中加入代码
修改系统日期,比如我是从2024年5月30日修改成31日,再访问接口,会自动归档5月30号的日志文件
里面日志如下
到此这篇关于springboot项目配置logback-spring.xml实现按日期归档日志的方法的文章就介绍到这了,更多相关springboot 按日期归档日志内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
精彩评论