Configure log files ---- [logback-spring.xml]

First, the introduction of related packages

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>1.2.3</version>
</dependency>

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

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.25</version>
</dependency> 

 
#-------------------- 或者以下配置--------------------------#
  <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.22</version>
  </dependency>

  <!-- lombok -->
  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <scope>provided</scope>
  </dependency>

  

Second, the configuration related logpath

       Configure log file is saved in the address file application.properties 

# Log file storage path 
logging.file =. / Logs / myLog.log  

Third, the configuration file logback-spring.xml 

     A 3.1 log configuration 

<? XML Version = "1.0" encoding = "UTF-. 8"?>
<-! logging level from low to high into TRACE <DEBUG <INFO <WARN < ERROR <FATAL, if set to WARN, lower than WARN the information is not output ->
<- Scan:! when this property is set to true, if the configuration file is changed, it will be reloaded, the default value to true ->
<- scanPeriod:! establish monitoring whether configuration files have modified time interval, if not given unit of time, in milliseconds by default.
                 When the scan is true, this property take effect. The default interval is 1 minute. ->
<- Debug:! When this property is set to true, will print logback internal log information, real-time view logback running. The default value is false. ->
<= Configuration Scan "to true" scanPeriod = "10 seconds The">
    <contextName can> the logback </ contextName can>

   
<-! Value is the name of the variable name, the value of the variable value is a value defined. It will be inserted into logger value defined by the context. Once defined,
can with the "$ {}" plus the name of the reference value. Here the root directory logs defined ->
    <Property name = "log.path" value = "F.: / Logs" />
<! - may be the same by the above example defines the name of the log file and defined herein below /web_debug.log <property name = "appName" value = "atguigu-springboot"> </ property> ->


    <! -. 0 log format and color rendering ->
    <! - dependent color rendering log class ->
    <conversionRule conversionWord = "CLR" converterClass = "org.springframework.boot.logging.logback.ColorConverter" / >
    <conversionRule conversionWord = "WEX" converterClass = "org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
    <conversionRule conversionWord = "WEx" converterClass = "org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
    < ! - color log format ->
    <Property name = "CONSOLE_LOG_PATTERN" value = "$ {CONSOLE_LOG_PATTERN: -% CLR (% D {the mM-dd-YYYY HH: mm: ss.SSS})

    {faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint}     

       % clr ([% 15.15t]) {faint}% clr (% - 40.40logger {39}) {cyan}% clr (:) {faint}% m% n $ {LOG_EXCEPTION_CONVERSION_WORD: -% wEx}} "/>
<! -
log output format:
% D represents the date and time,
% represents thread thread name,
% -5level: level 5 show left character width
% logger {50} represents logger name up to 50 characters, or divided for a period . %
MSG: a message log,
% n-newline
->

    <-. 1 represents the console output ch.qos.logback.core.ConsoleAppender ->!.
    <the appender name = "the cONSOLE" class = "ch.qos .logback.core.ConsoleAppender ">
        <-! appender this log is used for the development, only the lowest level configuration, log level console output log information is greater than or equal to this level ->
        <filter class =" CH .qos.logback.classic.filter.ThresholdFilter ">
            <Level> Debug </ Level>
        </ filter>
        <Encoder>
            <Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
            <!-- 设置字符集 -->
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <! - 2 outputs to the document ->
    <! - 2.1 Level to DEBUG log time scrolling output ->
    <appender name = "DEBUG_FILE" class = "ch.qos.logback.core.rolling.RollingFileAppender" >
        <! - the path and file name are recorded in the log file ->
        <File>} $ {log.path /web_debug.log </ File>
        <! - output log file format ->
        <Encoder>
            < pattern>% {D the mM-dd-YYYY HH: mm: ss.SSS} [Thread%]% {50} Logger -5level% -%% n-MSG </ pattern>
            <charset> UTF-. 8 </ charset> < ! - character set ->
        </ Encoder>
        <-! scroll policy logger, by date, by size record ->
        <rollingPolicy class = "ch.qos.logback.core.rolling.TimeBasedRollingPolicy" >
            <! - log archiving ->
            <fileNamePattern>${log.path}/web-debug-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>100MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <!--日志文档保留天数-->
            <maxHistory>15</maxHistory>
        </rollingPolicy>
        <!-- 此日志文档只记录debug级别的 -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>debug</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!-- 2.2 level is INFO log time scrolling output ->
    <appender name = "INFO_FILE" class = "ch.qos.logback.core.rolling.RollingFileAppender">
        <-! path and file name of the log file being recorded ->
        <File>} $ {log.path / web_info.log </ File>
        <- - log file output format!>
        <Encoder>
            <pattern>% {D the mM-dd-YYYY HH: mm: ss.SSS} [Thread%]% {Logger -5level% } 50 -% n-MSG% </ pattern>
            <charset> UTF-. 8 </ charset>
        </ Encoder>
        <-! logger scroll policy, by date, by size recording ->
        <rollingPolicy class = " ch.qos.logback.core.rolling.TimeBasedRollingPolicy ">
            <-! daily logs and archive path format ->
            <fileNamePattern> $ {log.path} / {D Web-info-% the MM-dd-YYYY}. % i.log </ fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>100MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <!--日志文档保留天数-->
            <maxHistory>15</maxHistory>
        </rollingPolicy>
        <!-- 此日志文档只记录info级别的 -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>info</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!-- 2.3 level to WARN log time scrolling output ->
    <appender name = "WARN_FILE" class = "ch.qos.logback.core.rolling.RollingFileAppender">
        <-! path and file name of the log file being recorded ->
        <File>} $ {log.path / web_warn.log </ File>
        <- - log file output format!>
        <Encoder>
            <pattern>% {D the mM-dd-YYYY HH: mm: ss.SSS} [Thread%]% {Logger -5level% } 50 -% n-MSG% </ pattern>
            <charset> UTF-. 8 </ charset> <-! here the character set ->
        </ Encoder>
        <-! logger scroll policy, by date by size recording ->
        <rollingPolicy class = "ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            . <fileNamePattern> $ {log.path} / {D Web-% WARN- the MM-dd-YYYY}% i.log </ fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize> 100MB </ maxFileSize>
            </ timeBasedFileNamingAndTriggeringPolicy>
            <-! the number of days to retain log files ->
            <maxHistory> 15 </ maxHistory>
        </ rollingPolicy>
        <-! This log file records only warn level ->
        <filter class = "ch.qos.logback.classic.filter.LevelFilter">
            <Level> The warn </ Level>
            <onMatch> ACCEPT </ onMatch>
            <onMismatch> DENY </ onMismatch>
        </ filter>
    </ the appender>

    <-! 2.4 level to ERROR log output time of rolling ->
    <the appender name = "ERROR_FILE" class = "ch.qos.logback.core.rolling.RollingFileAppender">
        <! - the path and file name of the log file being recorded ->
        <File> $ {} /web_error.log log.path </ File>
        <! - Output log file format ->
        <Encoder>
            <pattern>% {D the MM-dd-YYYY HH: mm: ss.SSS} [Thread%]% {50} Logger -5level% -%% n-MSG </ pattern>
            <charset> UTF-8 </ charset> <-! character set here ->
        </ Encoder>
        <-! scroll policy logger, by date, by size records ->
        <rollingPolicy class = "ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern> $ {log.path} / {D Web-Error-% the MM-dd-YYYY}. i.log% </ fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class = "ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <the maxFileSize> 100MB </ the maxFileSize>
            </ timeBasedFileNamingAndTriggeringPolicy>
            <-! the number of days to retain log files ->
            <maxHistory> 15 </ maxHistory>
        </ rollingPolicy>
        <-! This log file records only ERROR level ->
        <filter class = "ch.qos.logback.classic.filter.LevelFilter">
            <Level> ERROR < / level>
            <onMatch> ACCEPT </ onMatch>
            <onMismatch> DENY </ onMismatch>
        </ filter>
    </ the appender>

    <-!
        <Logger> to set the logging level to print one specific packet or a certain class of ,
        and designated <appender>. <logger> only a name attribute,
        an optional level addtivity and an optional attribute.
        name: a packet logger used to specify a particular constraint or affected by a certain class.
        level: used to set the printing level, case-insensitive: TRACE, DEBUG, INFO, WARN , ERROR,

              If this property is not set, the logger will inherit the current level superiors.
        addtivity: whether to pass information to their superiors print logger. The default is true.
        <Logger name = "org.springframework.web" Level = "info" />
        <name = Logger "org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor" Level = "the INFO" />
    ->

    <-!
        use of mybatis when, sql statement is under debug will be printed, and here we only configured the info, so if you want to see sql statement, the following two operations:
        first the <root level = "info"> into <root level = "DEBUG"> this will print sql, but this log there will be a lot of other news
        The second is to separate dao directory configuration debug mode, the following code, so the configuration sql statement prints, or other normal level info:
        [logging.level.org.mybatis = debug logging.level.dao = debug]
     ->

    <!

        level: the level used to set the print, case insensitive: TRACE, DEBUG, INFO, WARN , ERROR, ALL and OFF,
        can not be set or synonyms INHERITED NULL. The default is DEBUG
        can contain zero or more elements, identifies the appender will be added to the logger.
    ->

    <! - 4. The final strategy ->
    <- 4.1 development environment:! Print Console ->
    <springProfile name = "dev">
        <Logger name = "com.sdcm.pmp" Level = "Debug" />
    </ springProfile>

    <the root Level = "info">
        <the appender-REF REF = "the CONSOLE" />
        <the appender-REF REF = "the DEBUG_FILE under the" />
        <the appender-REF REF = "INFO_FILE" />
        <REF-REF = the appender "WARN_FILE" />
        <REF-REF = the appender "ERROR_FILE"




        <root level="info">
            <appender-ref ref="CONSOLE" />
            <appender-ref ref="DEBUG_FILE" />
            <appender-ref ref="INFO_FILE" />
            <appender-ref ref="ERROR_FILE" />
            <appender-ref ref="WARN_FILE" />
        </root>
    </springProfile> -->

</configuration>

  3.2 log configuration two

<? xml Version = "1.0" encoding = "UTF-8"?> 
<-! 
Scan: When this property is set to true, if the configuration file is changed, it will be reloaded, the default value is true. 
scanPeriod: Set if there are modified profiles monitoring interval, if the time unit is not given, the default milliseconds when the scan is true, this property effect. The default interval is 1 minute. 
debug: When this property is set to true, will print logback internal log information, real-time view logback running. The default value is false. 
-> 
<= Configuration Scan "to false" scanPeriod = "60 seconds The" Debug = "to false"> 
   <-! Custom log root of -> 
   <Property name = "LOG_HOME" value = "/ App / log" / > 
   <! - definition of log file name -> 
   <Property name = "appName" value = "atguigu-springboot"> </ Property> 
   <! - ch.qos.logback.core.ConsoleAppender console output indicates - > 
   <the appender name = "stdout" class = "CH.
           % thread represents the thread name,
           % -5level: Level 5 show left character width 
           % logger {50} represents logger name up to 50 characters, or divided for periods. 
           % msg: log messages, 
           % n-newline 
       -> 
       <layout class = "ch.qos.logback.classic.PatternLayout"> 
           <pattern>% {D the MM-dd-YYYY HH: mm: ss.SSS} [ Thread%]% {50} Logger -5level% -%% n-MSG </ pattern> 
       </ layout> 
   </ the appender> 

   <-! rolling log file, the log records to the specified file first, when a condition matches , logging to another file ->   
   <appender name = "appLogAppender" class = "ch.qos.logback.core.rolling.RollingFileAppender"> 
       <-! specify the name of the log file -> 
       <file> $ { } LOG_HOME / appName} {$ .log </ File> 
       <! 
       - TimeBasedRollingPolicy: The most commonly used strategy rolling, rolling it to develop strategies based on time, responsible for both the scroll is also responsible for starting the scroll. 
       ->
       <rollingPolicy class = "ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 
           <-! 
           storage location and file name of the file generated when scrolling% d {yyyy-MM-dd }: by day rolling log for 
           % i : when the file size exceeds maxFileSize, i files according rolling 
           -> 
           <fileNamePattern> $ {LOG_HOME} / {$ appName} - {D% the MM-dd-YYYY} - i.log% </ fileNamePattern> 
           <-! - 
           optional node, control the maximum number of reserved archive, delete old files exceeds the number. Scroll assumptions set every day, 
           and maxHistory 365, only keep the last 365 days files, old files before deleting. Note that deleting old files are, 
           those directories created for the archive will be deleted. 
           -> 
           <MaxHistory> 365 </ MaxHistory> 
           <! -
           <timeBasedFileNamingAndTriggeringPolicy class = "ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> 
               <the maxFileSize> 100MB </ the maxFileSize> 
           </ timeBasedFileNamingAndTriggeringPolicy> 
       </ rollingPolicy> 
       <- log output Format:! ->      
       <= class layout " ch.qos.logback.classic.PatternLayout "> 
           <pattern>% {D the mM-dd-YYYY HH: mm: ss.SSS} [% Thread] - [-5level%] [% {50} Logger: Line%] -% MSG% n-</ pattern> 
       </ layout> 
   </ the appender> 

   <-! 
       Logger is mainly used to store log object, you can also define the log type, level
       name: indicates the matching logger type prefix, the first part is packet 
       level: to record the log level, comprising TRACE <DEBUG <INFO <WARN <ERROR 
       the additivity: role is whether children-logger configured appender rootLogger output, 
       to false: this represents only a logger appender-ref, true:
       It represents the current and a logger appender-ref rootLogger the appender-ref valid 
   -> 
   <-! Hibernate logger -> 
   <logger name = "com.atguigu" Level = "Debug" /> 
   <-! Logger the Spring Framework -> 
   <logger name = "org.springframework" Level = "Debug" = the additivity "to false"> </ logger> 



   <-! 
   the root and the parent-child relationship logger is not specifically defined, the default is the root, any class only and a logger will correspond, 
   either logger defined, either root, to determine the key to find the logger, then judge of the logger and appender level. 
   -> 
   <= the root Level "info"> 
       <REF-REF = the appender "stdout" /> 
       <REF-REF = the appender "appLogAppender" /> 
   </ the root> 
</ Configuration>

 

Reference documents: https://www.cnblogs.com/xm-02/p/9109563.html

      https://www.jianshu.com/p/6f093b0b0c8a 

      https://www.cnblogs.com/sky230/p/6420208.html

 

Guess you like

Origin www.cnblogs.com/stt101517/p/11820960.html
Recommended