logback使用

1、需要引入maven配置

ch.qos.logback
logback-classic
1.0.0
这个会依赖slf4j-api和logback-core。

ch.qos.logback
logback-access
1.1.5

所有的jar包
logback-access-1.0.0.jar
logback-classic-1.0.0.jar
logback-core-1.0.0.jar
slf4j-api-1.6.0.jar

2、LoggerFactory.getLogger()方法可以传递两种参数。自己起的名字和类.class。主要在打印的日志里有所区分

3、logback有默认的配置,会读取项目里的logback.xml和logback-test.xml配置文件

4、配置文件里可以设置到类级别的打印日志

5、appender里加上filter可以使文件里只打印改类型的日志

6、通过定义包来打印框架的日志

7、xml配置里如果使用变量来定义参数,那么就得 ,

8、打印info级别日志时如果getLogger()参数是自定义的string的话,可能会引起无法输出日志的问题

配置文件


<configuration>

    
    <property name="LOG_PATH" value="../logs" />

    
    <appender name="errorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <File>${LOG_PATH}/all_error.logFile>
        
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERRORlevel>
            
            <onMatch>ACCEPTonMatch>
            
            <onMismatch>DENYonMismatch>
        filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            
            <FileNamePattern>${LOG_PATH}/all_error.%i.logFileNamePattern>
            <MinIndex>1MinIndex>
            <MaxIndex>5MaxIndex>
        rollingPolicy>
        
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MBMaxFileSize>
        triggeringPolicy>
        
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{yyyy-MM-dd hh:mm:ss.SSS}:[%thread] %-5level %logger{36} : %msg %nPattern>
        layout>
    appender>

    
    <appender name="commonLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <File>${LOG_PATH}/common.logFile>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            
            <FileNamePattern>${LOG_PATH}/common.%i.logFileNamePattern>
            <MinIndex>1MinIndex>
            <MaxIndex>5MaxIndex>
        rollingPolicy>
        
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MBMaxFileSize>
        triggeringPolicy>
        
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{yyyy-MM-dd hh:mm:ss.SSS}:[%thread] %-5level %logger{36} : %msg %nPattern>
        layout>
    appender>

    
    <appender name="frontLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <File>${LOG_PATH}/front.logFile>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            
            <FileNamePattern>${LOG_PATH}/front.%i.logFileNamePattern>
            <MinIndex>1MinIndex>
            <MaxIndex>10MaxIndex>
        rollingPolicy>
        
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>100MBMaxFileSize>
        triggeringPolicy>
        
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{yyyy-MM-dd hh:mm:ss.SSS}:[%thread] %-5level %logger{36} : %msg %nPattern>
        layout>
    appender>

    
    <appender name="jobLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <File>${LOG_PATH}/job.logFile>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            
            <FileNamePattern>${LOG_PATH}/job.%i.logFileNamePattern>
            <MinIndex>1MinIndex>
            <MaxIndex>10MaxIndex>
        rollingPolicy>
        
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>100MBMaxFileSize>
        triggeringPolicy>
        
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{yyyy-MM-dd hh:mm:ss.SSS}:[%thread] %-5level %logger{36} : %msg %nPattern>
        layout>
    appender>

    
    <appender name="remoteLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <File>${LOG_PATH}/remote.logFile>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            
            <FileNamePattern>${LOG_PATH}/remote.%i.logFileNamePattern>
            <MinIndex>1MinIndex>
            <MaxIndex>10MaxIndex>
        rollingPolicy>
        
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>100MBMaxFileSize>
        triggeringPolicy>
        
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{yyyy-MM-dd hh:mm:ss.SSS}:[%thread] %-5level %logger{36} : %msg %nPattern>
        layout>
    appender>

    
    <appender name="serviceLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <File>${LOG_PATH}/service.logFile>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            
            <FileNamePattern>${LOG_PATH}/service.%i.logFileNamePattern>
            <MinIndex>1MinIndex>
            <MaxIndex>10MaxIndex>
        rollingPolicy>
        
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>100MBMaxFileSize>
        triggeringPolicy>
        
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{yyyy-MM-dd hh:mm:ss.SSS}:[%thread] %-5level %logger{36} : %msg %nPattern>
        layout>
    appender>

    
    <appender name="payLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <File>${LOG_PATH}/pay.logFile>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            
            <FileNamePattern>${LOG_PATH}/pay.%i.logFileNamePattern>
            <MinIndex>1MinIndex>
            <MaxIndex>10MaxIndex>
        rollingPolicy>
        
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>100MBMaxFileSize>
        triggeringPolicy>
        
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{yyyy-MM-dd hh:mm:ss.SSS}:[%thread] %-5level %logger{36} : %msg %nPattern>
        layout>
    appender>

    <logger name="com.galaxy.fym.common" level="ERROR">
        <appender-ref ref="commonLog"/>
    logger>
    <logger name="com.galaxy.fym.front" level="ERROR">
        <appender-ref ref="frontLog"/>
    logger>
    <logger name="com.galaxy.fym.job" level="INFO">
        <appender-ref ref="jobLog"/>
    logger>
    <logger name="com.galaxy.fym.remote" level="INFO">
        <appender-ref ref="remoteLog"/>
    logger>
    <logger name="com.galaxy.fym.service" level="INFO">
        <appender-ref ref="serviceLog"/>
    logger>
    <logger name="com.galaxy.fym.pay.Pay" level="INFO">
        <appender-ref ref="payLog"/>
    logger>

    
    

    
    <root>
        <appender-ref ref="errorLog"/>
    root>
configuration>

这个是项目里实际用到的配置,踩了很多坑总结出来的


<configuration>

    
    <property name="LOG_LEVEL" value="ERROR" />

    
    <property name="LOG_PATH" value="../logs" />

    <jmxConfigurator />

    
    <appender name="errorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <File>${LOG_PATH}/error.logFile>
        
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>${LOG_LEVEL}level>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            
            <FileNamePattern>${LOG_PATH}/error.%i.logFileNamePattern>
            <MinIndex>1MinIndex>
            <MaxIndex>5MaxIndex>
        rollingPolicy>
        
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MBMaxFileSize>
        triggeringPolicy>
        
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{yyyy-MM-dd hh:mm:ss.SSS}:[%thread] %-5level %logger{36} : %msg %nPattern>
        layout>
    appender>

    
    <appender name="infoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <File>${LOG_PATH}/info.logFile>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFOlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            
            <FileNamePattern>${LOG_PATH}/info.%i.logFileNamePattern>
            <MinIndex>1MinIndex>
            <MaxIndex>5MaxIndex>
        rollingPolicy>
        
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MBMaxFileSize>
        triggeringPolicy>
        
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{yyyy-MM-dd hh:mm:ss.SSS}:[%thread] %-5level %logger{36} : %msg %nPattern>
        layout>
    appender>

    
    <appender name="stdoutAppender" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%npattern>
        layout>
    appender>

    
    <logger name="com.galaxy.wz" level="INFO">
        <appender-ref ref="stdoutAppender" />
        <appender-ref ref="infoLog" />
    logger>

    
    <root level="${LOG_LEVEL}">
        <appender-ref ref="errorLog"/>
    root>
configuration>

你可能感兴趣的:(Java)