Springboot新建日志文件配置方法

  • Post author:
  • Post category:其他


Springboot日志可以是txt文件,也可添加html格式文件

  • 首先在 resources目录下创建 logback.xml 文件,springboot 默认导入依赖 不需要手动导入
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
    <!--定义日志文件的存储地址 -->
    <property name="LOG_HOME" value="../logs" />
 
    <property name="COLOR_PATTERN" value="%black(%contextName-) %red(%d{yyyy-MM-dd HH:mm:ss}) %green([%thread]) %highlight(%-5level) %boldMagenta( %replace(%caller{1}){'\t|Caller.{1}0|\r\n', ''})- %gray(%msg%xEx%n)" />
    <!-- 控制台输出 -->
    <appender name="STDOUT"
              class="ch.qos.logback.core.ConsoleAppender">
        <encoder
                class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}:%L - %msg%n</pattern>
        </encoder>
    </appender>
 
    <!-- 按照每天生成日志文件 -->
    <appender name="FILE"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy
                class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日志文件输出的文件名 -->
            <FileNamePattern>${LOG_HOME}/water.%d{yyyy-MM-dd}.log
            </FileNamePattern>
            <!--日志文件保留天数 -->
            <MaxHistory>7</MaxHistory>
        </rollingPolicy>
        <encoder
                class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}:%L - %msg%n</pattern>
        </encoder>
        <!--日志文件最大的大小 -->
        <triggeringPolicy
                class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>60MB</MaxFileSize>
        </triggeringPolicy>
    </appender>
 
    <!-- 生成html格式日志开始 -->
    <appender name="HTML" class="ch.qos.logback.core.FileAppender">
        <!-- 过滤器,只记录WARN级别的日志 -->
        <!-- <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>info</level>
            <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> -->
 
        <encoder
                class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <layout class="ch.qos.logback.classic.html.HTMLLayout">
                <pattern>%p%d%msg%M%F{32}%L</pattern>
            </layout>
        </encoder>
        <file>${LOG_HOME}/error-log.html</file>
    </appender>
    <!-- 生成html格式日志结束 -->
 
    <!-- 每天生成一个html格式的日志开始 -->
    <appender name="FILE_HTML"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy
                class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日志文件输出的文件名 -->
            <FileNamePattern>${LOG_HOME}/water.%d{yyyy-MM-dd}.html
            </FileNamePattern>
            <!--日志文件保留天数 -->
            <MaxHistory>7</MaxHistory>
        </rollingPolicy>
        <encoder
                class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <layout class="ch.qos.logback.classic.html.HTMLLayout">
                <pattern>%p%d%msg%M%F{32}%L</pattern>
            </layout>
        </encoder>
        <!--日志文件最大的大小 -->
        <triggeringPolicy
                class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>30MB</MaxFileSize>
        </triggeringPolicy>
    </appender>
    <!-- 每天生成一个html格式的日志结束 -->
 
    <!--myibatis log configure -->
    <logger name="com.apache.ibatis" level="TRACE" />
    <logger name="java.sql.Connection" level="DEBUG" />
    <logger name="java.sql.Statement" level="DEBUG" />
    <logger name="java.sql.PreparedStatement" level="DEBUG" />
 
    <!-- 日志输出级别 -->
    <root level="INFO">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
        <appender-ref ref="HTML" />
        <appender-ref ref="FILE_HTML" />
    </root>
 
</configuration>
  • 添加完成后文件夹生成日志文件如下:

  • 如果不需要 html文件 只需要 txt文件 的话 可以将文件内容改成如下:

     	<?xml version="1.0" encoding="UTF-8"?>  
     	<configuration scan="true" scanPeriod="60 seconds" debug="false">  
     	    <contextName>logback</contextName>  
     	    <property name="log.path" value="app_log/log"/>  
     	  
     	    <!--输出到控制台-->  
     	    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">  
     	        <!--此日志appender是为开发使用,只配置最底级别,控制台输出的日志级别是大于或等于此级别的日志信息-->  
     	        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">  
     	            <level>debug</level>  
     	        </filter>  
     	        <encoder>  
     	            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>  
     	            <charset>utf-8</charset>  
     	        </encoder>  
     	    </appender>  
     	  
     	    <!--输出到debug-->  
     	    <appender name="debug" class="ch.qos.logback.core.rolling.RollingFileAppender">  
     	        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
     	            <fileNamePattern>${log.path}/logback-debug-%d{yyyy-MM-dd}.log</fileNamePattern>  
     	        </rollingPolicy>  
     	        <append>true</append>  
     	        <encoder>  
     	            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>  
     	            <charset>utf-8</charset>  
     	        </encoder>  
     	        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印DEBUG日志 -->  
     	            <level>DEBUG</level>  
     	            <onMatch>ACCEPT</onMatch>  
     	            <onMismatch>DENY</onMismatch>  
     	        </filter>  
     	    </appender>  
     	  
     	    <!--输出到info-->  
     	    <appender name="info" class="ch.qos.logback.core.rolling.RollingFileAppender">  
     	        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
     	            <fileNamePattern>${log.path}/logback-info-%d{yyyy-MM-dd}.log</fileNamePattern>  
     	        </rollingPolicy>  
     	        <append>true</append>  
     	        <encoder>  
     	            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>  
     	            <charset>utf-8</charset>  
     	        </encoder>  
     	        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印INFO日志 -->  
     	            <level>INFO</level>  
     	            <onMatch>ACCEPT</onMatch>  
     	            <onMismatch>DENY</onMismatch>  
     	        </filter>  
     	    </appender>  
     	  
     	    <!--输出到error-->  
     	    <appender name="error" class="ch.qos.logback.core.rolling.RollingFileAppender">  
     	        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
     	            <fileNamePattern>${log.path}/logback-error-%d{yyyy-MM-dd}.log</fileNamePattern>  
     	        </rollingPolicy>  
     	        <append>true</append>  
     	        <encoder>  
     	            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>  
     	            <charset>utf-8</charset>  
     	        </encoder>  
     	        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印ERROR日志 -->  
     	            <level>ERROR</level>  
     	            <onMatch>ACCEPT</onMatch>  
     	            <onMismatch>DENY</onMismatch>  
     	        </filter>  
     	    </appender>  
     	  
     	    <!--输出到warn-->  
     	    <appender name="warn" class="ch.qos.logback.core.rolling.RollingFileAppender">  
     	        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
     	            <fileNamePattern>${log.path}/logback-warn-%d{yyyy-MM-dd}.log</fileNamePattern>  
     	        </rollingPolicy>  
     	        <append>true</append>  
     	        <encoder>  
     	            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>  
     	            <charset>utf-8</charset>  
     	        </encoder>  
     	        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印WARN日志 -->  
     	            <level>WARN</level>  
     	            <onMatch>ACCEPT</onMatch>  
     	            <onMismatch>DENY</onMismatch>  
     	        </filter>  
     	    </appender>  
     	  
     	    <!--用户统计-->  
     	    <appender name="dataApp" class="ch.qos.logback.core.rolling.RollingFileAppender">  
     	        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
     	            <fileNamePattern>${log.path}/logback-data-%d{yyyy-MM-dd}.log</fileNamePattern>  
     	        </rollingPolicy>  
     	        <append>true</append>  
     	        <encoder>  
     	            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>  
     	            <charset>utf-8</charset>  
     	        </encoder>  
     	        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印INFO日志 -->  
     	            <level>INFO</level>  
     	            <onMatch>ACCEPT</onMatch>  
     	            <onMismatch>DENY</onMismatch>  
     	        </filter>  
     	    </appender>  
     	  
     	    <!--用于统计数据 比如某个方法执行了多少次-->  
     	    <logger name="dataLogger">  
     	        <level value="INFO"/>  
     	        <appender-ref ref="dataApp"/>  
     	    </logger>  
     	  
     	    <!--分别设置对应的日志输出节点 -->  
     	    <root level="info">  
     	        <appender-ref ref="console"/>  
     	        <appender-ref ref="debug"/>  
     	        <appender-ref ref="info"/>  
     	        <appender-ref ref="error"/>  
     	        <appender-ref ref="warn"/>  
     	    </root>  
     	</configuration> 



版权声明:本文为sakuraous原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。