深入讲解Logback

什么是Logback

ogback是由log4j创始人设计的又一个开源日志组件。是log4j的一个 改良版本。此外完整实现SLF4J API使你可以很方便地更换成其它日志系统如log4j或JDK14 Logging。logback-access访问模块与Servlet容器集成提供通过Http来访问日志的功能。

在springboot中如何使用logback

这部分网上已经被人写烂了,我就不写了,随便丢一个教程吧
https://blog.csdn.net/white_ice/article/details/85065219

给出一个项目中的案例

logback-spring.xml


<configuration scan="true" scanPeriod="60 seconds" debug="false">
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>

    <property name="LOG_FILE_NAME_PATTERN" value="/usr/local/hppalogs/web/hppa-web.%d{yyyy-MM-dd}.%i.log"/>
    
    <property name="CONSOLE_LOG_PATTERN"
              value="%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%c){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
    <property name="FILE_LOG_PATTERN"
              value="%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %c : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>

    
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <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_FILE_NAME_PATTERN}fileNamePattern>
            
            <maxHistory>366maxHistory>
            
            <totalSizeCap>2GBtotalSizeCap>

            
            <timeBasedFileNamingAndTriggeringPolicy
                    class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MBmaxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>

        rollingPolicy>
        <encoder>
            <pattern>${FILE_LOG_PATTERN}pattern>
        encoder>
    appender>

    <root level="DEBUG">
        <appender-ref ref="console"/>
        <appender-ref ref="file"/>
    root>
configuration>

如何设置不同的包下面打印不同级别的日志?

<logger name="com.baomidou.mybatisplus" level="DEBUG" additivity="false">
    <appender-ref ref="STDOUT"/>
    <appender-ref ref="FILE"/>
logger>

如何设置打印到不同的配置文件里面去?

Logger log = LoggerFactory.getLogger("log1");
Logger requestLog = LoggerFactory.getLogger("log2");
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_HOME}/dbw-hppa-server.logfile>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${LOG_HOME}/dbw-hppa-server.%d{yyyy-MM-dd}.log.zipfileNamePattern>
    rollingPolicy>
    <append>trueappend>
    <encoder>
        <pattern>[%thread]%d{HH:mm:ss.SSS} %-5level %logger{100}[%line] - %msg %mdc %npattern>
        <charset>UTF-8charset>
    encoder>
appender>

<appender name="requestLogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_HOME}/dbw-hppa-server-request.logfile>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${LOG_HOME}/dbw-hppa-server-request.%d{yyyy-MM-dd}.log.zipfileNamePattern>
    rollingPolicy>
    <append>trueappend>
    <encoder>
        <pattern>[%thread]%d{HH:mm:ss.SSS} %-5level %logger{100}[%line] - %msg %mdc %npattern>
        <charset>UTF-8charset>
    encoder>
appender>

<logger name="log1" level="DEBUG" additivity="false">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="requestLogAppender"/>
logger>
<logger name="log2" level="ERROR" additivity="false">
    <appender-ref ref="STDOUT"/>
    <appender-ref ref="FILE"/>
logger>

你可能感兴趣的:(工具使用)