Logback Framework--the potential problems caused by log segmentation

Source: Internet
Author: User
  • Source:
    1. The Logback-test.xml file is as follows and there are 2 parameters that we need to focus on:
      1. filenamepattern: The section of the log file name changes here is month and day, plus 1 files to split the self-increment variable, warning, month and day of the value depends on the system time, the self-increment variable depends on the Logback framework running memory variables.
      2. MaxFileSize: Here the log file segmentation condition for the log file size reaches 1M.
        <?XML version= "1.0" encoding= "UTF-8"?><Configuration>    <Appendername= "Testlog"class= "Ch.qos.logback.core.rolling.RollingFileAppender">        <Rollingpolicyclass= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy">            <!--The part of our log file name change is the date of the month, plus 1 additional split-increment variables -            <Filenamepattern>Test-log.%d{yyyy-mm-dd-hh}.%i.log</Filenamepattern>            <!--The number of saved history files is generated for each log file, and the log file is stored for a period of 7 days. -            <maxhistory>168</maxhistory>            <Timebasedfilenamingandtriggeringpolicyclass= "Ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">                <MaxFileSize>1MB</MaxFileSize><!--log file segmentation condition for log file size up to 1M -            </Timebasedfilenamingandtriggeringpolicy>        </Rollingpolicy>        <Encoder>            <!--pattern node, which sets the input format for the log -            <pattern>%d{hh:mm:sss}%p [%thread] (%file:%line\)-%m%n</pattern>            <!--encoding of the recording log -            <CharSet>UTF-8</CharSet>        </Encoder>    </Appender>    <Root Level= "Debug">                <Appender-refref= "Testlog" />    </Root></Configuration>
        View Code

    2. Output log source code is as follows, it should be noted that:
      1. We use while loop output log, more than normal log output intensity is much higher;
      2. Our log content is "Hello logback, line" +i.
         PackageDemo.logback;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory; Public classLogfilecut { Public Static voidMain (string[] args) {Logger Logger= Loggerfactory.getlogger ("Demo.logback.LogFileCut"); inti = 0;  while(I < 100000) {Logger.debug ("Hello Logback, line ++++++" +i); I++; }  }}
        View Code

  • According to the Source:
    1. The first step, we now output 100,000 logs to the log file, each file size is 1MB,
      As you can see, the actual file size is indeterminate 1142kb,1152kb, which is greater than 1MB, and the last log file is less than 1MB because it has not been filled. This is the first question that we want to figure out, why is the log file size actually larger than the maximum value we set ?
    2. The second step, because we are using the main method to output the log, we run again is equivalent to the server restarts again, this time we will change the output of the log, "Hello Logback, Line ++++++" +i. <br/Running Results
      1. First look at the number of files, the same number of cycles, the number of new log files is 6, the front 0~6 files is the first step to run, the following 7~12 is generated this time.
      2. Compare the first 7 files in the two graphs the size of the 0~6,6 file is very easy to understand, but 0~5 how all 6 files have been added , is this the second question we are going to answer?
  • Logback Log Segmentation Problem Analysis:
    • The first question: Why is the actual size of the log split greater than the set upper limit value?
      • evidence: such as.
      • cause Analysis:
        • When a log file is split, there are two actions:
          1. Compare the size of the current log file with the set value, determine whether to split ;
          2. The
          3. continues to write logs to the current log file as long as the tessellation has not been completed.
            When you are still comparing, I have output hundreds of meters ... Yes....
        • When the log file size has been determined to reach a predetermined value of the moment, the log file has not been split, and the log file is still written to the log. Therefore, the actual size of the log file is usually greater than the size of the set value. How much
        • is exceeded: The size of the log file actually exceeds the predetermined value, which basically depends on the speed at which log records are written relative to the log file creation after the point in time zeropoint that the log file size reaches the critical value.
        • in general, the more frequently the logs are output in code, the more critical values are exceeded (our loop output log here is very strong).
        • treacherous: JIT in trouble? Repeating the individual steps above, observing the log split file size, we are not difficult to find, the log file list at the beginning of a few files are always smaller than the following log files. In other words, the log output speed of the runtime has changed, and I suspect that it is most likely because the while loop was detected as a hotspot code by the JIT compiler, so it was recompiled to make the log output faster, resulting in larger log files later.
    • The second question: Why is the log file that should be locked after "reboot" be entered again?
      • Evidence: Check the end of 0~6, we can all find "Hello Logback, Line ++++++" +i, this record exists. That is, before the "restart", supposedly "full" to reach the upper value of the log file, after the "restart", the log has been written again.
      • Cause Analysis:
        • Journal name: Test-log.2018-08-14-13.0.log. File name accurate to the hour, we "restart" before and after, are on the same day 13 points!
          1. The change in the log name depends on the time, as well as the split sequence number, which is determined by the operating system and divided by the Logback framework. The variable value corresponding to the split ordinal is not persisted! Once restarted, it can only start again, so in the same time period (here is the same day of the same one hours) in a reboot, will not create a new log file, but in the original log to append records;
          2. The first question has already been said, the day so the log file at the time of comparison (not yet compared to completion), the log output is still in progress, so the logfile will become larger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.