飞道的博客

如何实现Springboot项目保存本地系统日志文件【超详细】

368人阅读  评论(0)

背景:我们都知道springboot默认日志是打印在控制台中的,不会以文件的形式进行保存。那么日后系统上线肯定是有需要对日志进行定位分析问题的,那么如何实现将控制台输出的日志保存起来?

话不多说,实际效果如下,如果是你预期的那样,咱们接着往下看。

如果最后觉得有所帮助,请不要吝啬你的赞,直接pia的点亮就完事了啦,可好;

 如下是集成,按步骤来:

1、在项目的resources目录下创建一个名为logback-spring.xml的日志配置文件,配置文件名最好跟我一样;

2、编写xml文件内容:

code中每句都给出了注释,方便你们理解。


  
  1. <?xml version= "1.0" encoding= "utf-8"?>
  2. <configuration>
  3. <!-- 引入默认得配置文件 -->
  4. <include resource= "org/springframework/boot/logging/logback/defaults.xml"/>
  5. <!-- 模块名标识日志名称 -->
  6. <springProperty scope= "context" name= "springAppName" source= "spring.application.name"/>
  7. <!-- info日志单文件大小限制 -->
  8. <springProperty scope= "context" name= "logback.fileInfoLog.maxFileSize" source= "logback.fileInfoLog.maxFileSize" defaultValue= "200MB" />
  9. <!-- info日志最大保留时长单位天 -->
  10. <springProperty scope= "context" name= "logback.fileInfoLog.maxHistory" source= "logback.fileInfoLog.maxHistory" defaultValue= "15" />
  11. <!-- info日志文件总大小,超过该大小,旧得即将删除 -->
  12. <springProperty scope= "context" name= "logback.fileInfoLog.totalSizeCap" source= "logback.fileInfoLog.totalSizeCap" defaultValue= "100GB" />
  13. <!-- error日志单文件大小限制 -->
  14. <springProperty scope= "context" name= "logback.fileErrorLog.maxFileSize" source= "logback.fileErrorLog.maxFileSize" defaultValue= "200MB" />
  15. <!-- error日志最大保留时长单位天 -->
  16. <springProperty scope= "context" name= "logback.fileErrorLog.maxHistory" source= "logback.fileErrorLog.maxHistory" defaultValue= "15" />
  17. <!-- error日志文件总大小,超过该大小,旧得即将删除 -->
  18. <springProperty scope= "context" name= "logback.fileErrorLog.totalSizeCap" source= "logback.fileErrorLog.totalSizeCap" defaultValue= "100GB" />
  19. <!-- http日志单文件大小限制 -->
  20. <springProperty scope= "context" name= "logback.fileHttpLog.maxFileSize" source= "logback.fileHttpLog.maxFileSize" defaultValue= "200MB" />
  21. <!-- http日志最大保留时长单位天 -->
  22. <springProperty scope= "context" name= "logback.fileHttpLog.maxHistory" source= "logback.fileHttpLog.maxHistory" defaultValue= "15" />
  23. <!-- http日志文件总大小,超过该大小,旧得即将删除 -->
  24. <springProperty scope= "context" name= "logback.fileHttpLog.totalSizeCap" source= "logback.fileHttpLog.totalSizeCap" defaultValue= "100GB" />
  25. <!-- 日志目录 -->
  26. <springProperty scope= "context" name= "logback.rootDir" source= "logback.rootDir" defaultValue= "logs"/>
  27. <!-- 控制台输出得日志格式 -->
  28. <property name= "CONSOLE_LOG_PATTERN"
  29. 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}"/>
  30. <!-- 日志文件输出得日志格式 -->
  31. <property name= "FILE_LOG_PATTERN"
  32. value= "%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %t [%c:%L]-%m%n"/>
  33. <!-- 控制台输出 -->
  34. <appender name= "consoleLog" class= "ch.qos.logback.core.ConsoleAppender">
  35. <layout class= "ch.qos.logback.classic.PatternLayout">
  36. <pattern>${CONSOLE_LOG_PATTERN}</pattern>
  37. </layout>
  38. </appender>
  39. <!-- info日志得设定 -->
  40. <appender name= "fileInfoLog" class= "ch.qos.logback.core.rolling.RollingFileAppender">
  41. <filter class= "ch.qos.logback.classic.filter.LevelFilter">
  42. <level>ERROR</level>
  43. <onMatch>DENY</onMatch>
  44. <onMismatch>ACCEPT</onMismatch>
  45. </filter>
  46. <encoder>
  47. <pattern>${FILE_LOG_PATTERN}</pattern>
  48. </encoder>
  49. <file>${logback.rootDir}/${springAppName}.log</file>
  50. <!--滚动策略-->
  51. <rollingPolicy class= "ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy" >
  52. <!--路径-->
  53. <fileNamePattern>${logback.rootDir}/%d{yyyy-MM,aux}/%d{yyyy-MM-dd,aux}/${springAppName}-%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
  54. <maxFileSize>${logback.fileInfoLog.maxFileSize}</maxFileSize>
  55. <maxHistory>${logback.fileInfoLog.maxHistory}</maxHistory>
  56. <totalSizeCap>${logback.fileInfoLog.totalSizeCap}</totalSizeCap>
  57. <cleanHistoryOnStart> true</cleanHistoryOnStart>
  58. </rollingPolicy>
  59. </appender>
  60. <!-- 错误日志 -->
  61. <appender name= "fileErrorLog" class= "ch.qos.logback.core.rolling.RollingFileAppender">
  62. <filter class= "ch.qos.logback.classic.filter.ThresholdFilter">
  63. <level>ERROR</level>
  64. </filter>
  65. <encoder>
  66. <pattern>${FILE_LOG_PATTERN}</pattern>
  67. </encoder>
  68. <file>${logback.rootDir}/${springAppName}-error.log</file>
  69. <!--滚动策略-->
  70. <rollingPolicy class= "ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy" >
  71. <!--路径-->
  72. <fileNamePattern>${logback.rootDir}/%d{yyyy-MM,aux}/%d{yyyy-MM-dd,aux}/${springAppName}-error-%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
  73. <maxFileSize>${logback.fileErrorLog.maxFileSize}</maxFileSize>
  74. <maxHistory>${logback.fileErrorLog.maxHistory}</maxHistory>
  75. <totalSizeCap>${logback.fileErrorLog.totalSizeCap}</totalSizeCap>
  76. <cleanHistoryOnStart> true</cleanHistoryOnStart>
  77. </rollingPolicy>
  78. </appender>
  79. <!-- http日志 -->
  80. <appender name= "httpInfoLog" class= "ch.qos.logback.core.rolling.RollingFileAppender">
  81. <encoder>
  82. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %- 5p %m%n</pattern>
  83. </encoder>
  84. <file>${logback.rootDir}/${springAppName}-http.log</file>
  85. <!--滚动策略-->
  86. <rollingPolicy class= "ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
  87. <!--路径-->
  88. <fileNamePattern>${logback.rootDir}/%d{yyyy-MM,aux}/%d{yyyy-MM-dd,aux}/${springAppName}-http-%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
  89. <maxFileSize>${logback.fileHttpLog.maxFileSize}</maxFileSize>
  90. <maxHistory>${logback.fileHttpLog.maxHistory}</maxHistory>
  91. <totalSizeCap>${logback.fileHttpLog.totalSizeCap}</totalSizeCap>
  92. <cleanHistoryOnStart> true</cleanHistoryOnStart>
  93. </rollingPolicy>
  94. </appender>
  95. <appender name= "ASYNC_consoleLog" class= "ch.qos.logback.classic.AsyncAppender">
  96. <appender-ref ref= "consoleLog"/>
  97. </appender>
  98. <appender name= "ASYNC_fileInfoLog" class= "ch.qos.logback.classic.AsyncAppender">
  99. <appender-ref ref= "fileInfoLog"/>
  100. </appender>
  101. <appender name= "ASYNC_fileErrorLog" class= "ch.qos.logback.classic.AsyncAppender">
  102. <appender-ref ref= "fileErrorLog"/>
  103. </appender>
  104. <appender name= "ASYNC_httpInfoLog" class= "ch.qos.logback.classic.AsyncAppender">
  105. <appender-ref ref= "httpInfoLog"/>
  106. </appender>
  107. <root level= "info">
  108. <appender-ref ref= "ASYNC_consoleLog" />
  109. <appender-ref ref= "ASYNC_fileInfoLog" />
  110. <appender-ref ref= "ASYNC_fileErrorLog" />
  111. </root>
  112. <logger name= "log_http" additivity= "false" level= "INFO">
  113. <appender-ref ref= "ASYNC_httpInfoLog"/>
  114. </logger>
  115. </configuration>

3、配置日志配置中所提到的springAppName;

4、使用这个默认不用配置pom依赖,最新的spring-boot-starter-web中已经集成了。如下,重启下项目就能自动生成日志文件了,而且是实时写入的,控制台输出什么,立马就更新进到该xxx.log里头。

记得,配置日志完后,记得刷新下maven或者关闭下项目重新打开,以免误导大家以为日志配置没用。


总结:启动项目,直接将logback-spring.xml文件复制到resources目录下就可以实现日志文件记录。是不是非常非常方便。


另外:

想要在控制台打印sql,如下:

在配置文件中,加上这句就好了; 


  
  1. ###sql打印
  2. logging.level.com.system.xiaoma.mapper=debug

❤如果文章对您有所帮助,就在文章的右上角或者文章的末尾点个赞吧!(づ ̄ 3 ̄)づ 

❤如果喜欢大白兔分享的文章,就给大白兔点个关注吧!(๑′ᴗ‵๑)づ╭❤~

❤对文章有任何问题欢迎小伙伴们下方留言或者入群探讨【群号:708072830】

❤鉴于个人经验有限,所有观点及技术研点,如有异议,请直接回复讨论(请勿发表攻击言)


转载:https://blog.csdn.net/weixin_43970743/article/details/115767763
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场