slf4j+logback&logback.xml

Add maven dependence

<dependencies>
<!--https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</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>
    <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.2.3 </version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-access -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-access</artifactId>
        <version>1.2.3</version>
    </dependency>
</dependencies>

Configuration logback.xml under the resources directory

<? XML Version = "1.0" encoding = "UTF-. 8" ?> 
< Configuration Debug = "to true" > 

    <-! Set the log context name followed output format may be defined by the print log context name contextName can% - > 
    < contextName can > SK-log </ contextName can > 
    <-! used to set the relevant variables, arranged by the key-value method, and then accessed by $ {key} in the latter configuration file -> 
    < Property name = "sk.name" value = "Test" > </ Property > 

    <!--======================================ConsoleAppender===================================================================-->
    <!--Log output component output is mainly responsible for the log and the log format. stdout: Standard Output -> 
    < the appender name = "stdout" class = "ch.qos.logback.core.ConsoleAppender" > 
        <-! defines a filter under the log output will not be printed LEVEL the logger in the level and at the level of joint decision (filter non-essential) stdout output of the 
          threshold filter, filter out logs below a specified threshold. When the log level is equal to or higher than the critical value, the filter returns NEUTRAL; When the threshold level is lower than the log, the log will be rejected. -> 
        < filter class = "ch.qos.logback.classic.filter.ThresholdFilter" > 
            < Level > Debug </ Level > 
        </ filter > 
        <! - level filters, filter based on log level. If the level is equal to the log level configuration, will accept or reject filter logs according onMath and onMismatch 
        <Level>: Set filtering level
        <onMatch>: for configuration matching the filter 
        <onMismatch>: configure the operation does not match the filter -> 
        <-!         <filter class = "ch.qos.logback.classic.filter.LevelFilter"> -> 
        <-!             <Level> Debug </ Level> -> 
        <-!             <onMatch> ACCEPT </ onMatch> -> 
        <-!             <onMismatch> DENY </ onMismatch> -> 
        <! -         </ filter> -> 
        ! <- Encoder default configuration PatternLayoutEncoder -> 
        <-! define console output format -> 
        <-! 
            % D {the mM-dd-YYYY HH: mm:ss} time format is hh HH is made 24h 12h Ltd. 
            [% thread] thread 
            % -5level level
            % logger {36} log packet 36 packet length 
            [% file:% line] and the row number of the target class 
            % msg log information 
            % n linefeed 
            (Encoder required) -> 
        < Encoder > 
            < pattern > % {YYYY-MM-D HH dd: mm: SS} [Thread%]% {36} Logger -5level% [% File: Line%] - n-% MSG% </ pattern > 
        </ Encoder > 
    </ the appender > 

    <-! === ======================= RollingFileAppender ========================== ================================================== === -> 

    <! -<the appender name = "File" class = "ch.qos.logback.core.rolling.RollingFileAppender"> -> 

        <-! & lt;! & ndash; if it is true, is appended to the end of the log file, if it is false, empty the existing file, the default is to true -> 
        <-! warning Append mode is mandatory for RollingFileAppender but after I added defaulting to append = true I still do not add up ndash &; & gt;.. -> 
        <-! & lt !; & ndash; <the append> to false </ the append> & ndash; & gt; -> 

        <-! <filter class = "ch.qos.logback.classic.filter.ThresholdFilter"> -> 
            ! <- <Level > the trace </ Level> -> 
        <-! </ filter> -> 
        <-! & lt; & ndash;!rollingPolicy did not write, then there will be a warning No TriggeringPolicy was set for the RollingFileAppender named file does not export local files & ndash; & gt;-->
        <!--<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">-->
            <!--<fileNamePattern>E:/logDemo1/${sk.name}.log.%i.%d{yyyy-MM-dd}</fileNamePattern>-->
            <!--&lt;!&ndash;30d周期&ndash;&gt;-->
            <!--<maxHistory>30</maxHistory>-->
            <!--&lt;!&ndash;每个文件最大10M&ndash;&gt;-->
            <!--<maxFileSize>10MB</maxFileSize>-->
        <!--</rollingPolicy>-->

        <!--<encoder>-->
            <!--<pattern>%d{yyyy-MM-dd  HH:mm:ss} [%thread] %-5level %logger{36} [%file : %line] - %msg%n</pattern>-->
        <!--</ Encoder> -> 
    <-! </ the appender> -> 

    <-! ============================ ================================================== =========================== -> 

    <-! root is the default logger did not set the output level here -> 
    < root > 
        <! - defines appender, log appender by which to write -> 
        < appender-ref ref = "stdout" /> 
        < appender-ref ref = "File" /> 
    </ root > 

    <! - = log ================================= specified package ============= ========================================= ->
    <! -The logger is not specified appender, it will inherit those appender root node defined      -> 
    < logger name = "edu.cn" Level = "Debug" > 
        <-! <Appender-ref ref = "stdout" /> - -> 
    </ Logger > 

    <-! through LoggerFactory.getLogger ( "Test3") can obtain the Logger 
        the additivity = "to false" no double its own output and inheritance appender root of -> 
    < Logger name = " test3 " Level =" the trace " the additivity =" to false " > 
        < the appender REF- REF =" stdout " />
        <appender-ref ref="file"/>
    </ Logger > 

    <-! Provision of the additivity = "false", so it will not use rootLogger when output appender -> 
    <-! But the logger itself is not configured appender, so use this logger output log, then you not output anywhere -> 
    <-!     <Logger name = "the Test3" Level = "the trace" the additivity = "to false"> </ Logger> -> 

</ Configuration >

 

Guess you like

Origin www.cnblogs.com/yanguobin/p/11519396.html