美文网首页
Logback日志系统

Logback日志系统

作者: Lv大爷 | 来源:发表于2017-11-29 18:50 被阅读0次

    一、准备工作

    1、准备日志系统所需要的jar包

    (若果是maven来管理包,可参考下面配置pom.xml;不是maven项目可参考下载相应的jar包)
    注意:这些jar包缺一不可!

            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.7.21</version>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-core</artifactId>
                <version>1.2.3</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.2.3</version>
            </dependency>
    
    2、创建配置文件

    在classpath(即src/mian/resources)路径下创建logback.xml配置文件。

    二、编写配置

    在创建的logback.xml文件中,编写配置。

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration scan="true" scanPeriod="60 seconds" debug="false">
        <!-- 定义参数常量 -->
        <!-- TRACE<DEBUG<INFO<WARN<ERROR -->
        <!-- logger.trace("msg") logger.debug... -->
        <property name="log.level" value="debug" />
        <property name="log.maxHistory" value="30" />
        <property name="log.filePath" value="${catalina.base}/logs/webapps" />
        <property name="log.pattern"
            value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n" />
        <!-- 控制台设置 -->
        <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>${log.pattern}</pattern>
            </encoder>
        </appender>
        <!-- DEBUG -->
        <appender name="debugAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <!-- 文件路径 -->
            <file>${log.filePath}/debug.log</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!-- 文件名称 -->
                <fileNamePattern>${log.filePath}/debug/debug.%d{yyyy-MM-dd}.log.gz
                </fileNamePattern>
                <!-- 文件最大保存历史数量 -->
                <maxHistory>${log.maxHistory}</maxHistory>
            </rollingPolicy>
            <encoder>
                <pattern>${log.pattern}</pattern>
            </encoder>
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>DEBUG</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
            </filter>
        </appender>
        <!-- INFO -->
        <appender name="infoAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <!-- 文件路径 -->
            <file>${log.filePath}/info.log</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!-- 文件名称 -->
                <fileNamePattern>${log.filePath}/info/info.%d{yyyy-MM-dd}.log.gz
                </fileNamePattern>
                <!-- 文件最大保存历史数量 -->
                <maxHistory>${log.maxHistory}</maxHistory>
            </rollingPolicy>
            <encoder>
                <pattern>${log.pattern}</pattern>
            </encoder>
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>INFO</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
            </filter>
        </appender>
        <!-- ERROR -->
        <appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <!-- 文件路径 -->
            <file>${log.filePath}/erorr.log</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!-- 文件名称 -->
                <fileNamePattern>${log.filePath}/error/error.%d{yyyy-MM-dd}.log.gz
                </fileNamePattern>
                <!-- 文件最大保存历史数量 -->
                <maxHistory>${log.maxHistory}</maxHistory>
            </rollingPolicy>
            <encoder>
                <pattern>${log.pattern}</pattern>
            </encoder>
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>ERROR</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
            </filter>
        </appender>
        <logger name="com.cn.broncho.o2o" level="${log.level}" additivity="true">
            <appender-ref ref="debugAppender"/>
            <appender-ref ref="infoAppender"/>
            <appender-ref ref="errorAppender"/>
        </logger>
        <root level="info">
            <appender-ref ref="consoleAppender"/>
        </root>
    </configuration>
    

    注:文章若有不足,请大家多多指正,让我们一起愉快的coding!

    相关文章

      网友评论

          本文标题:Logback日志系统

          本文链接:https://www.haomeiwen.com/subject/iyepbxtx.html