spring boot日志配置文件(彩色日志)logback-spring.xml

文章目录

    • 前言
    • ✨彩色日志
    • ✨输出到控制台
    • ✨输出到文件
      • 1.输出所有日志
      • 2.输出错误日志
    • ✨配置不同环境下的日志级别
    • ✨完整的logback-spring.xml文件配置
    • ✨文件下载

前言

spring boot项目记录日志、每日日志归档、彩色日志都会在logback-spring.xml文件中进行配置。给大家分享完整的日志配置信息,以及配置的各种含义。

✨彩色日志



<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(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}" />

实现结果
spring boot日志配置文件(彩色日志)logback-spring.xml_第1张图片

✨输出到控制台


<appender name="CONSOLE"
          class="ch.qos.logback.core.ConsoleAppender">
    
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>infolevel>
    filter>
    <encoder>
        <Pattern>${CONSOLE_LOG_PATTERN}Pattern>
        
        <charset>UTF-8charset>
    encoder>
appender>

✨输出到文件

每日日志文件及日志归档。

1.输出所有日志


<appender name="ALL_FILE"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    
    <file>${log.path}/log_all.logfile>
    
    <encoder>
        <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -
            %msg%npattern>
        <charset>UTF-8charset> 
    encoder>
    
    <rollingPolicy
        class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        
        <fileNamePattern>${log.path}/debug/log-all-%d{yyyy-MM-dd}.%i.log
        fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy
            class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>100MBmaxFileSize>
        timeBasedFileNamingAndTriggeringPolicy>
        
        <maxHistory>15maxHistory>
    rollingPolicy>
    
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
        <level>debuglevel>
    filter>
appender>

注:本示例所有日志文件记录为debug级别的,可以不设置,记录所有级别日志

2.输出错误日志


<appender name="ERROR_FILE"
          class="ch.qos.logback.core.rolling.RollingFileAppender">
    
    <file>${log.path}/log_error.logfile>
    
    <encoder>
        <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -
            %msg%npattern>
        <charset>UTF-8charset> 
    encoder>
    
    <rollingPolicy
        class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${log.path}/error/log-error-%d{yyyy-MM-dd}.%i.log
        fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy
            class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>100MBmaxFileSize>
        timeBasedFileNamingAndTriggeringPolicy>
        
        <maxHistory>15maxHistory>
    rollingPolicy>
    
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
        <level>ERRORlevel>
        <onMatch>ACCEPTonMatch>
        <onMismatch>DENYonMismatch>
    filter>
appender>

结果:
spring boot日志配置文件(彩色日志)logback-spring.xml_第2张图片

✨配置不同环境下的日志级别

开发环境下设置最低级别为debug水平:


<springProfile name="dev">
    <root level="debug">
        <appender-ref ref="CONSOLE"/>
    root>
springProfile>

✨完整的logback-spring.xml文件配置


<configuration scan="true" scanPeriod="10 seconds">
	<contextName>logbackcontextName>
	<property name="log.path" value="log" />

	
	
	<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(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}" />


	
	<appender name="CONSOLE"
		class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<Pattern>${CONSOLE_LOG_PATTERN}Pattern>
			
			<charset>UTF-8charset>
		encoder>
	appender>

	

	
	<appender name="ALL_FILE"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${log.path}/log_all.logfile>
		<encoder>
			<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -
				%msg%npattern>
			<charset>UTF-8charset> 
		encoder>
		<rollingPolicy
			class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${log.path}/debug/log-all-%d{yyyy-MM-dd}.%i.log
			fileNamePattern>
			<timeBasedFileNamingAndTriggeringPolicy
				class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>100MBmaxFileSize>
			timeBasedFileNamingAndTriggeringPolicy>
			
			<maxHistory>15maxHistory>
		rollingPolicy>
	appender>

	
	<appender name="ERROR_FILE"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${log.path}/log_error.logfile>
		<encoder>
			<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -
				%msg%npattern>
			<charset>UTF-8charset>
		encoder>
		<rollingPolicy
			class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${log.path}/error/log-error-%d{yyyy-MM-dd}.%i.log
			fileNamePattern>
			<timeBasedFileNamingAndTriggeringPolicy
				class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>100MBmaxFileSize>
			timeBasedFileNamingAndTriggeringPolicy>
			<maxHistory>15maxHistory>
		rollingPolicy>
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>ERRORlevel>
			<onMatch>ACCEPTonMatch>
			<onMismatch>DENYonMismatch>
		filter>
	appender>

	
	<springProfile name="dev">
		<logger name="com.xxx.xxx" level="debug" />
	springProfile>

	<root level="info">
		<appender-ref ref="CONSOLE" />
		<appender-ref ref="ALL_FILE" />
		<appender-ref ref="ERROR_FILE" />
	root>
	
	
	<logger name="com.alibaba.nacos.client.naming" level="WARN"/>

configuration>

✨文件下载

若想查看完整中文注释,了解配置功能,可下载文件
spring boot日志配置文件(彩色日志)logback-spring.xml_第3张图片
spring boot日志配置文件(彩色日志)logback-spring.xml_第4张图片spring boot日志配置文件(彩色日志)logback-spring.xml_第5张图片

下载后直接覆盖原文件

下载链接:logback-spring.xml

	
	<springProfile name="dev">
	
		<logger name="com.xxx.xxx" level="debug" />
	springProfile>

你可能感兴趣的:(Spring,Boot,spring,boot,spring,idea,java)