欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring boot 日志配置详解

程序员文章站 2024-03-04 11:13:23
最近在学习spring boot框架的路上,今日看了一下spring boot日志配置,顺便留个笔记记录一下。 1.新建logback.xml文件   内容...

最近在学习spring boot框架的路上,今日看了一下spring boot日志配置,顺便留个笔记记录一下。

1.新建logback.xml文件

spring boot 日志配置详解 

内容如下:

<!-- logback configuration. see http://logback.qos.ch/manual/index.html -->
<configuration scan="true" scanperiod="10 seconds">
 <include resource="org/springframework/boot/logging/logback/base.xml" />

 <appender name="info_file" class="ch.qos.logback.core.rolling.rollingfileappender">
  <file>${log_path}/info.log</file>
  <rollingpolicy class="ch.qos.logback.core.rolling.timebasedrollingpolicy">
   <filenamepattern>${log_path}/info-%d{yyyymmdd}.log.%i</filenamepattern>
   <timebasedfilenamingandtriggeringpolicy class="ch.qos.logback.core.rolling.sizeandtimebasedfnatp">
    <maxfilesize>500mb</maxfilesize>
   </timebasedfilenamingandtriggeringpolicy>
   <maxhistory>2</maxhistory>
  </rollingpolicy>
  <layout class="ch.qos.logback.classic.patternlayout">
   <pattern>%d{yyyy-mm-dd hh:mm:ss.sss} [%thread] %-5level %logger{36} -%msg%n
   </pattern>
  </layout>
 </appender>

 <appender name="error_file" class="ch.qos.logback.core.rolling.rollingfileappender">
  <filter class="ch.qos.logback.classic.filter.thresholdfilter">
   <level>error</level>
  </filter>
  <file>${log_path}/error.log</file>
  <rollingpolicy class="ch.qos.logback.core.rolling.timebasedrollingpolicy">
   <filenamepattern>${log_path}/error-%d{yyyymmdd}.log.%i
   </filenamepattern>
   <timebasedfilenamingandtriggeringpolicy class="ch.qos.logback.core.rolling.sizeandtimebasedfnatp">
    <maxfilesize>500mb</maxfilesize>
   </timebasedfilenamingandtriggeringpolicy>
   <maxhistory>2</maxhistory>
  </rollingpolicy>
  <layout class="ch.qos.logback.classic.patternlayout">
   <pattern>%d{yyyy-mm-dd hh:mm:ss.sss} [%thread] %-5level %logger{36} -%msg%n

   </pattern>
  </layout>
 </appender>
 
 <!-- hibernate日志输入 -->
 <!-- <logger name="org.hibernate.type.descriptor.sql.basicbinder"
  level="trace" />
 <logger name="org.hibernate.type.descriptor.sql.basicextractor"
  level="trace" />
 <logger name="org.hibernate.sql" level="info" />
 <logger name="org.hibernate.engine.queryparameters" level="info" />
 <logger name="org.hibernate.engine.query.hqlqueryplan" level="info" /> -->

 <root level="info">
  <appender-ref ref="info_file" />
  <appender-ref ref="error_file" />
 </root>
 
</configuration>

2.然后在application.properties文件中假如如下代码:

#log
logging.config=classpath:logback.xml
logging.path=/workspace/rock/jpa-test/log

3.这时就可以使用log对象了

private final logger log = logger.getlogger(this.getclass());

log.info("你好");

注:由于我的项目在d盘,所以第二步设置的文件路径为:d:/workspace/rock/jpa-test/log

运行程序后,进入路径会有三个文件

spring boot 日志配置详解

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。