背景:我们都知道springboot默认日志是打印在控制台中的,不会以文件的形式进行保存。那么日后系统上线肯定是有需要对日志进行定位分析问题的,那么如何实现将控制台输出的日志保存起来?
话不多说,实际效果如下,如果是你预期的那样,咱们接着往下看。
如果最后觉得有所帮助,请不要吝啬你的赞,直接pia的点亮就完事了啦,可好;
如下是集成,按步骤来:
1、在项目的resources目录下创建一个名为logback-spring.xml的日志配置文件,配置文件名最好跟我一样;
2、编写xml文件内容:
code中每句都给出了注释,方便你们理解。
-
<?xml version=
"1.0" encoding=
"utf-8"?>
-
<configuration>
-
<!-- 引入默认得配置文件 -->
-
<include resource=
"org/springframework/boot/logging/logback/defaults.xml"/>
-
-
<!-- 模块名标识日志名称 -->
-
<springProperty scope=
"context" name=
"springAppName" source=
"spring.application.name"/>
-
<!-- info日志单文件大小限制 -->
-
<springProperty scope=
"context" name=
"logback.fileInfoLog.maxFileSize" source=
"logback.fileInfoLog.maxFileSize" defaultValue=
"200MB" />
-
<!-- info日志最大保留时长单位天 -->
-
<springProperty scope=
"context" name=
"logback.fileInfoLog.maxHistory" source=
"logback.fileInfoLog.maxHistory" defaultValue=
"15" />
-
<!-- info日志文件总大小,超过该大小,旧得即将删除 -->
-
<springProperty scope=
"context" name=
"logback.fileInfoLog.totalSizeCap" source=
"logback.fileInfoLog.totalSizeCap" defaultValue=
"100GB" />
-
-
<!-- error日志单文件大小限制 -->
-
<springProperty scope=
"context" name=
"logback.fileErrorLog.maxFileSize" source=
"logback.fileErrorLog.maxFileSize" defaultValue=
"200MB" />
-
<!-- error日志最大保留时长单位天 -->
-
<springProperty scope=
"context" name=
"logback.fileErrorLog.maxHistory" source=
"logback.fileErrorLog.maxHistory" defaultValue=
"15" />
-
<!-- error日志文件总大小,超过该大小,旧得即将删除 -->
-
<springProperty scope=
"context" name=
"logback.fileErrorLog.totalSizeCap" source=
"logback.fileErrorLog.totalSizeCap" defaultValue=
"100GB" />
-
-
<!-- http日志单文件大小限制 -->
-
<springProperty scope=
"context" name=
"logback.fileHttpLog.maxFileSize" source=
"logback.fileHttpLog.maxFileSize" defaultValue=
"200MB" />
-
<!-- http日志最大保留时长单位天 -->
-
<springProperty scope=
"context" name=
"logback.fileHttpLog.maxHistory" source=
"logback.fileHttpLog.maxHistory" defaultValue=
"15" />
-
<!-- http日志文件总大小,超过该大小,旧得即将删除 -->
-
<springProperty scope=
"context" name=
"logback.fileHttpLog.totalSizeCap" source=
"logback.fileHttpLog.totalSizeCap" defaultValue=
"100GB" />
-
<!-- 日志目录 -->
-
<springProperty scope=
"context" name=
"logback.rootDir" source=
"logback.rootDir" defaultValue=
"logs"/>
-
<!-- 控制台输出得日志格式 -->
-
<property name=
"CONSOLE_LOG_PATTERN"
-
value=
"%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
-
<!-- 日志文件输出得日志格式 -->
-
<property name=
"FILE_LOG_PATTERN"
-
value=
"%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %t [%c:%L]-%m%n"/>
-
<!-- 控制台输出 -->
-
<appender name=
"consoleLog"
class=
"ch.qos.logback.core.ConsoleAppender">
-
<layout
class=
"ch.qos.logback.classic.PatternLayout">
-
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
-
</layout>
-
</appender>
-
<!-- info日志得设定 -->
-
<appender name=
"fileInfoLog"
class=
"ch.qos.logback.core.rolling.RollingFileAppender">
-
<filter
class=
"ch.qos.logback.classic.filter.LevelFilter">
-
<level>ERROR</level>
-
<onMatch>DENY</onMatch>
-
<onMismatch>ACCEPT</onMismatch>
-
</filter>
-
<encoder>
-
<pattern>${FILE_LOG_PATTERN}</pattern>
-
</encoder>
-
<file>${logback.rootDir}/${springAppName}.log</file>
-
<!--滚动策略-->
-
<rollingPolicy
class=
"ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy" >
-
<!--路径-->
-
<fileNamePattern>${logback.rootDir}/%d{yyyy-MM,aux}/%d{yyyy-MM-dd,aux}/${springAppName}-%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
-
<maxFileSize>${logback.fileInfoLog.maxFileSize}</maxFileSize>
-
<maxHistory>${logback.fileInfoLog.maxHistory}</maxHistory>
-
<totalSizeCap>${logback.fileInfoLog.totalSizeCap}</totalSizeCap>
-
<cleanHistoryOnStart>
true</cleanHistoryOnStart>
-
</rollingPolicy>
-
</appender>
-
-
<!-- 错误日志 -->
-
<appender name=
"fileErrorLog"
class=
"ch.qos.logback.core.rolling.RollingFileAppender">
-
<filter
class=
"ch.qos.logback.classic.filter.ThresholdFilter">
-
<level>ERROR</level>
-
</filter>
-
<encoder>
-
<pattern>${FILE_LOG_PATTERN}</pattern>
-
</encoder>
-
<file>${logback.rootDir}/${springAppName}-error.log</file>
-
<!--滚动策略-->
-
<rollingPolicy
class=
"ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy" >
-
<!--路径-->
-
<fileNamePattern>${logback.rootDir}/%d{yyyy-MM,aux}/%d{yyyy-MM-dd,aux}/${springAppName}-error-%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
-
<maxFileSize>${logback.fileErrorLog.maxFileSize}</maxFileSize>
-
<maxHistory>${logback.fileErrorLog.maxHistory}</maxHistory>
-
<totalSizeCap>${logback.fileErrorLog.totalSizeCap}</totalSizeCap>
-
<cleanHistoryOnStart>
true</cleanHistoryOnStart>
-
</rollingPolicy>
-
</appender>
-
-
<!-- http日志 -->
-
<appender name=
"httpInfoLog"
class=
"ch.qos.logback.core.rolling.RollingFileAppender">
-
<encoder>
-
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-
5p %m%n</pattern>
-
</encoder>
-
<file>${logback.rootDir}/${springAppName}-http.log</file>
-
<!--滚动策略-->
-
<rollingPolicy
class=
"ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
-
<!--路径-->
-
<fileNamePattern>${logback.rootDir}/%d{yyyy-MM,aux}/%d{yyyy-MM-dd,aux}/${springAppName}-http-%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
-
<maxFileSize>${logback.fileHttpLog.maxFileSize}</maxFileSize>
-
<maxHistory>${logback.fileHttpLog.maxHistory}</maxHistory>
-
<totalSizeCap>${logback.fileHttpLog.totalSizeCap}</totalSizeCap>
-
<cleanHistoryOnStart>
true</cleanHistoryOnStart>
-
</rollingPolicy>
-
</appender>
-
-
<appender name=
"ASYNC_consoleLog"
class=
"ch.qos.logback.classic.AsyncAppender">
-
<appender-ref ref=
"consoleLog"/>
-
</appender>
-
<appender name=
"ASYNC_fileInfoLog"
class=
"ch.qos.logback.classic.AsyncAppender">
-
<appender-ref ref=
"fileInfoLog"/>
-
</appender>
-
<appender name=
"ASYNC_fileErrorLog"
class=
"ch.qos.logback.classic.AsyncAppender">
-
<appender-ref ref=
"fileErrorLog"/>
-
</appender>
-
<appender name=
"ASYNC_httpInfoLog"
class=
"ch.qos.logback.classic.AsyncAppender">
-
<appender-ref ref=
"httpInfoLog"/>
-
</appender>
-
-
<root level=
"info">
-
<appender-ref ref=
"ASYNC_consoleLog" />
-
<appender-ref ref=
"ASYNC_fileInfoLog" />
-
<appender-ref ref=
"ASYNC_fileErrorLog" />
-
</root>
-
<logger name=
"log_http" additivity=
"false" level=
"INFO">
-
<appender-ref ref=
"ASYNC_httpInfoLog"/>
-
</logger>
-
-
</configuration>
3、配置日志配置中所提到的springAppName;
4、使用这个默认不用配置pom依赖,最新的spring-boot-starter-web中已经集成了。如下,重启下项目就能自动生成日志文件了,而且是实时写入的,控制台输出什么,立马就更新进到该xxx.log里头。
记得,配置日志完后,记得刷新下maven或者关闭下项目重新打开,以免误导大家以为日志配置没用。
总结:启动项目,直接将logback-spring.xml文件复制到resources目录下就可以实现日志文件记录。是不是非常非常方便。
另外:
想要在控制台打印sql,如下:
在配置文件中,加上这句就好了;
-
###sql打印
-
logging.level.com.system.xiaoma.mapper=debug
❤如果文章对您有所帮助,就在文章的右上角或者文章的末尾点个赞吧!(づ ̄ 3 ̄)づ
❤如果喜欢大白兔分享的文章,就给大白兔点个关注吧!(๑′ᴗ‵๑)づ╭❤~
❤对文章有任何问题欢迎小伙伴们下方留言或者入群探讨【群号:708072830】
❤鉴于个人经验有限,所有观点及技术研点,如有异议,请直接回复讨论(请勿发表攻击言)
转载:https://blog.csdn.net/weixin_43970743/article/details/115767763