Spring Boot 自定义日志配置

1、Spring Boot默认使用slf4j+logback日志框架。如果要自定义logback日志则需要在项目的资源路径下添加logback.xml文件。logback.xml内容如下:



<configuration scan="false" scanPeriod="60 seconds" debug="false">
    
    <property name="LOG_HOME" value="/logs/log" />
    
    <property name="appName" value="mengxuegu-spring-boot">property>
    
    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] - %-5level - %logger{50} - %msg%npattern>
        layout>
    appender>

      
    <appender name="appLogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <file>${LOG_HOME}/${appName}.logfile>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${LOG_HOME}/${appName}-%d{yyyy-MM-dd}-%i.logfileNamePattern>
            
            <MaxHistory>365MaxHistory>
            
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>100MBmaxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
        rollingPolicy>
             
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [ %thread ] - [ %-5level ] [ %logger{50} : %line ] - %msg%npattern>
        layout>
    appender>

    
    
    <logger name="com.mengxuegu" level="debug" />
    
    <logger name="org.springframework" level="debug" additivity="false">logger>


    
    <root level="info">
        <appender-ref ref="stdout" />
        <appender-ref ref="appLogAppender" />
    root>
configuration> 

2、也可以添加logback-spring.xml文件,logback-spring.xml比logback.xml多了一个可以根据环境来设置日志的标签,如下:

<springProfile name="dev">
	
springProfile>

<springProfile name="dev | test">
	
springProfile>

<springProfile name="!production">
	
springProfile>

3、如果要切换Spring Boot的默认日志框架,首先把它原有的日志框架移除,然后再引入新的框架,比如使用log4j2框架

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
    
    <exclusions>
        <exclusion>
            <artifactId>spring-boot-starter-loggingartifactId>
            <groupId>org.springframework.bootgroupId>
        exclusion>
    exclusions>
dependency>
<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-log4j2artifactId>
dependency>

你可能感兴趣的:(Spring,Boot,日志配置)