首页 > 代码库 > logback日志

logback日志

一、什么是日志框架?

是一套能够实现日志输出的工具包

能够描述系统运行状态的所有时间都可以算作日志

用户下线,接口超时,数据崩溃

 

二、日志框架的能力

1、定制输出目标(文件,回滚策略,数据库,网络的第三方)

2、定制输出格式

3、携带的上下文信息(时间戳,类路径,调用线程,堆栈)

4、运行时选择性输出

5、灵活的配置

6、优异的性能

 

三、常见的日志框架

1jdk 自带的 JUL

2apache 自带的 JCL

3Log4j

4Log4j2

5Logback

6SLF4j

7jboss-logging

 

日志门面:JCLSLF4jjboss-logging(不是服务于大众)

日志实现:Log4jLog4j2apache生产,太先进了,复杂性上过渡设计)、LogbackJUL(实现简陋)

 

四、如何使用

private final Logger logger = LoggerFactory.getLogger(LoggerTest.class);

logger.debug(“debug...”);

 

 技术分享

 

@Slf4j

Lombok  

log.info(“name: “+ name +”, password:” + password);

log.info(“name: {}, password:{}”, name, password);  

 

1Application.yml配置

Logging:

Pattern:

Console: “%d - %msg%n”

File: /var/log/tomcat/a.log

Level:

Com.imooc.LoggerTest: debug

 

2、Logback-spring.xml配置

 

<?xml version="1.0" encoding="UTF-8" ?>

<configuration>

    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">

        <layout class="ch.qos.logback.classic.PatternLayout">

            <pattern>

                %d - %msg%n

            </pattern>

        </layout>

    </appender>

 

    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">

        <filter class="ch.qos.logback.classic.filter.LevelFilter">

            <level>ERROR</level>

            <onMatch>DENY</onMatch>

            <onMismatch>ACCEPT</onMismatch>

        </filter>

        <encoder>

            <pattern>

                %msg%n

            </pattern>

        </encoder>

        <!--滚动策略-->

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

            <!--路径-->

            <fileNamePattern>/var/log/tomcat/sell/info.%d.log</fileNamePattern>

        </rollingPolicy>

    </appender>

 

 

    <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">

        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">

            <level>ERROR</level>

        </filter>

        <encoder>

            <pattern>

                %msg%n

            </pattern>

        </encoder>

        <!--滚动策略-->

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

            <!--路径-->

            <fileNamePattern>/var/log/tomcat/sell/error.%d.log</fileNamePattern>

        </rollingPolicy>

    </appender>

 

    <root level="info">

        <appender-ref ref="consoleLog" />

        <appender-ref ref="fileInfoLog" />

        <appender-ref ref="fileErrorLog" />

    </root>

</configuration>

 

 

 

 

logback日志