springboot2日志的使用

2018-08-24  本文已影响0人  哈利路亚911

springboot2使用的日志框架是slf4j+logback

pom依赖追加

<!--logging -->

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>log4j-over-slf4j</artifactId>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
        </dependency>
        <!-- end of logging -->

logback.xml 信息

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

    <property name="log.default_pattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS}|%-5level|%X{traceId}|%logger{1}|%msg%n"/>
    <property name="log.default_max_history" value="14"/>
    <property name="log.default_charset" value="UTF-8"/>
    <property name="log.home" value="logs"/>

    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${log.default_pattern}</pattern>
            <charset>${log.default_charset}</charset>
        </encoder>
    </appender>


    <appender name="info" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.home}/info.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${log.home}/archives/info.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>${log.default_max_history}</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${log.default_pattern}</pattern>
            <charset>${log.default_charset}</charset>
        </encoder>
    </appender>

    <appender name="error" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>WARN</level>
        </filter>
        <file>${log.home}/error.log</file>
        <rollingPolicy  class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${log.home}/archives/error.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>${log.default_max_history}</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${log.default_pattern}</pattern>
            <charset>${log.default_charset}</charset>
        </encoder>
    </appender>

    <!-- 数据库日志 -->
    <appender name="db_info"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.home}/db_info.log</file>
        <rollingPolicy
                class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${log.home}/archives/db_info.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>${log.default_max_history}</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${log.default_pattern}</pattern>
            <charset>${log.default_charset}</charset>
        </encoder>
    </appender>

    <!-- SQL相关日志输出-->
    <logger name="com.hizhu.openapi.dao" level="debug" additivity="false">
        <appender-ref ref="db_info"/>
    </logger>


    <root level="INFO">
        <appender-ref ref="info"/>
        <appender-ref ref="error"/>
    </root>


</configuration>

%d:表示日期时间
%thread:表示线程名
%-5level:级别从左到右显示5个字符宽度
%logger{50}:表示logger名字最长50个字符,否则按照句点分割
%msg:日志信息
%n:是换行符

上一篇下一篇

猜你喜欢

热点阅读