【java-日志组件】slf4j+logback配置及详解

文本主要介绍一下slf4j+logback在java工程中的配置,面向的读者是已经对slf4j+logback有一定了解的同学,不了解的同学,请看文章末尾的相关链接。以后会写一篇介绍slf4j框架的文章,其中会说明logback与之的关系。下面咱们进入正题。


一、基于maven的slf4j+logback pom.xml配置

<dependency>
    <groupId>org.slf4jgroupId>
    <artifactId>slf4j-apiartifactId>
    <version>1.7.10version>
dependency>
<dependency>
    <groupId>ch.qos.logbackgroupId>
    <artifactId>logback-classicartifactId>
    <version>1.1.2version>
dependency>
<dependency>
    <groupId>ch.qos.logbackgroupId>
    <artifactId>logback-coreartifactId>
    <version>1.1.2version>
dependency>

二、在classpath目录下新建logback.xml配置文件



<configuration scan="false" scanPeriod="60 seconds" debug="false">
    
    <property name="LOG_HOME" value="/app/log" />
    
    <property name="appName" value="netty">property>
    
    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        <Encoding>UTF-8Encoding>
        
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
        layout>
    appender>

      
    <appender name="appLogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <Encoding>UTF-8Encoding>
          
        <file>${LOG_HOME}/${appName}.logfile>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${LOG_HOME}/${appName}-%d{yyyy-MM-dd}-%i.logfileNamePattern>
            
            <MaxHistory>365MaxHistory>
            
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>100MBmaxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
        rollingPolicy>
             
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [ %thread ] - [ %-5level ] [ %logger{50} : %line ] - %msg%npattern>
        layout>
    appender>

    
    
    <logger name="org.hibernate" level="error" />
    
    <logger name="org.springframework" level="error" additivity="false">logger>

    <logger name="com.creditease" level="info" additivity="true">
        <appender-ref ref="appLogAppender" />
    logger>

    
    <root level="info">
        <appender-ref ref="stdout" />
        <appender-ref ref="appLogAppender" />
    root>
configuration> 

参考资料
log4j+logback+slf4j+commons-logging的关系与调试
日志组件logback的介绍及配置使用方法
使用logback
logback的SizeBasedTriggeringPolicy和TimeBasedRollingPolicy联合使用问题

你可能感兴趣的:(Java编程)