springboo 添加logback日志

感情迁移 提交于 2019-12-29 00:42:13

springboot默认引入logback相关的jar包

1、在 Application.properties里添加

logging.config=classpath:logback-spring.xml  

注:当文件名为logback-spring.xml时 不需要配置logging.config

 

logback日志基础配置可以参照官方

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />
    <logger name="sample.logback" level="DEBUG" />
    <springProfile name="staging">
        <logger name="sample.logback" level="TRACE" />
    </springProfile>
</configuration>

"sample.logback"是包名

 logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <!-- 文件输出格式 -->
    <property name="PATTERN" value="%-12(%d{yyyy-MM-dd HH:mm:ss.SSS}) |-%-5level [%thread] %c [%L] -| %msg%n"/>
    <!-- test文件路径 -->
    <property name="TEST_FILE_PATH" value="F:/vrv/logs/hrmis"/>
    <!-- prod文件路径 -->
    <property name="PRO_FILE_PATH" value="/vrv/logs/hrmis" />
    <!-- 开发环境 -->
    <springProfile name="dev">
        <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>${PATTERN}</pattern>
            </encoder>
        </appender>
        <logger name="com.example" level="DEBUG"/>
        <root level="info">
            <appender-ref ref="CONSOLE"/>
        </root>
    </springProfile>
    <!-- 测试环境 -->
    <springProfile name="test">
        <!-- 每天产生一个文件 -->
        <appender name="TEST-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <!-- 文件路径 -->
            <file>${TEST_FILE_PATH}</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!-- 文件名称 -->
                <fileNamePattern>${TEST_FILE_PATH}/info.%d{yyyy-MM-dd}.log</fileNamePattern>
                <!-- 文件最大保存历史数量 -->
                <MaxHistory>100</MaxHistory>
            </rollingPolicy>
            <layout class="ch.qos.logback.classic.PatternLayout">
                <pattern>${PATTERN}</pattern>
            </layout>
        </appender>
        <root level="info">
            <appender-ref ref="TEST-FILE"/>
        </root>
    </springProfile>
    <!-- 生产环境 -->
    <springProfile name="prod">
        <appender name="PROD_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${PRO_FILE_PATH}</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>${PRO_FILE_PATH}/warn.%d{yyyy-MM-dd}.log</fileNamePattern>
                <MaxHistory>100</MaxHistory>
            </rollingPolicy>
            <layout class="ch.qos.logback.classic.PatternLayout">
                <pattern>${PATTERN}</pattern>
            </layout>
        </appender>
        <root level="warn">
            <appender-ref ref="PROD_FILE" />
        </root>
    </springProfile>
</configuration>

 

以下是扩展配置,仅作参考:

<?xml version="1.0" encoding="UTF-8" ?>  
<configuration scan="true" scanPeriod="60 seconds" debug="false">  
  
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />  
    <!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径-->  
    <property name="LOG_HOME" value="/var/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}}" />  
    <!-- Console 输出设置 -->  
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">  
        <encoder charset="UTF-8">  
            <!--  显示毫秒数  
                <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>-->  
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>  
        </encoder>  
    </appender>  
    <appender name="FILE"  class="ch.qos.logback.core.rolling.RollingFileAppender">  
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
            <!--日志文件输出的文件名-->  
            <FileNamePattern>${LOG_HOME}/web.%d{yyyy-MM-dd}.log</FileNamePattern>  
            <!--日志文件保留天数-->  
            <MaxHistory>30</MaxHistory>  
        </rollingPolicy>  
        <!--日志文件最大的大小-->  
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">  
            <MaxFileSize>10MB</MaxFileSize>  
        </triggeringPolicy>  
        <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} - %msg%n</pattern>  
        </encoder>  
    </appender>  
    <!-- * 通配符 设置log打印级别 对所有类有效TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF-->  
    <!--将上面的appender添加到root-->  
    <root level="ERROR">  
        <appender-ref ref="STDOUT" />  
        <appender-ref ref="FILE" />  
    </root>  
   <logger name="com.boot" level="INFO"></logger>  
    <!--<!–日志异步到数据库 –>-->  
    <!--<appender name="DB" class="ch.qos.logback.classic.db.DBAppender">-->  
    <!--<!–日志异步到数据库 –>-->  
    <!--<connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">-->  
    <!--<!–连接池 –>-->  
    <!--<dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource">-->  
    <!--<driverClass>com.mysql.jdbc.Driver</driverClass>-->  
    <!--<url>jdbc:mysql://127.0.0.1:3306/databaseName</url>-->  
    <!--<user>root</user>-->  
    <!--<password>root</password>-->  
    <!--</dataSource>-->  
    <!--</connectionSource>-->  
    <!--</appender>-->  
</configuration>  

 关于Spring-Boot给出的配置方式如下:

# LOGGING
logging.config= # Location of the logging configuration file. For instance `classpath:logback.xml` for Logback

logging.exception-conversion-word=%wEx # Conversion word used when logging exceptions.

logging.file= # Log file name. For instance `myapp.log`

logging.level.*= # Log levels severity mapping. For instance `logging.level.org.springframework=DEBUG`

logging.path= # Location of the log file. For instance `/var/log`

logging.pattern.console= # Appender pattern for output to the console. Only supported with the default logback setup.

logging.pattern.file= # Appender pattern for output to the file. Only supported with the default logback setup.

logging.pattern.level= # Appender pattern for log level (default %5p). Only supported with the default logback setup.

logging.register-shutdown-hook=false # Register a shutdown hook for the logging system when it is initialized.

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!