Chat logging configuration project

I believe we all know, as a developer, in addition to write bug, should also be able to address the bug by logging. In this way, the line out of the question, some people come to us to solve, someone needs us, we can keep their jobs ...... visible, to know how to detect and resolve to engage in their own bug out how important it is.

Hey, joke, introduced the topic. I believe many people have used in the project log management tools: log4j, logback and so on. But I do not know how many people are adding to the project so that logs configuration: Project to use log4j, good, import the appropriate package maven, Internet to find the configuration files and watch it more reliable, though he 7 March 21, try to put items inside the log files normal output, ok, close the configuration file, continue to write bug to go, but for the configuration file which has little knowledge of the configuration, anyway, can be used on the line.

In fact, this is a very good habit, after all, only CV programmer is no future career prospects. Strong brother today based on actual case from his past before the project simple and we talk about in the on-line project, some of the problems of logging configuration, and approach.

First of all, to project lines on the beginning of the project, the configuration of log4j2.xml log file contents:


<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN" monitorInterval="30">
    <Properties>
        <Property name="fileName">./logs/xxx</Property>
    </Properties>
    <!--先定义所有的appender-->
    <appenders>
        <!--这个输出控制台的配置-->
        <console name="Console" target="SYSTEM_OUT">
            <!--输出日志的格式-->
            <PatternLayout pattern="[xxx] [%d{yyyy-MM-dd HH:mm:ss,SSS}] [%t] [%p] %c{1}:%L - %m%n"/>
        </console>
        <!-- 这个会打印出所有的info及以下级别的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档-->
        <RollingFile name="RollingFileDebug" fileName="${fileName}/debug.log"
                     filePattern="${fileName}/$${date:yyyy-MM}/debug-%d{yyyy-MM-dd}-%i.log">
            <!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
            <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="[xxx] [%d{yyyy-MM-dd HH:mm:ss,SSS}] [%t] [%p] %c{1}:%L - %m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="10 MB"/>
            </Policies>
            <DefaultRolloverStrategy/>
        </RollingFile>
    <RollingFile name="RollingFileInfo" fileName="${fileName}/info.log"
                     filePattern="${fileName}/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log">
            ……//内容类似上面的RollingFileDebug
        </RollingFile>
    <RollingFile ……  </RollingFile>
    </appenders>
    <!--然后定义logger,只有定义了logger并引入的appender,appender才会生效-->
    <loggers>
        <!--过滤掉spring和mybatis的一些无用的DEBUG信息-->
        <logger name="org.springframework" level="INFO"></logger>
        <logger name="org.mybatis" level="DEBUG"></logger>
        <root level="debug">
            <appender-ref ref="Console"/>
            <appender-ref ref="RollingFileDebug"/>
      ……
        </root>
    </loggers>
</configuration>

The project has a timer function is through a large number of fish per minute third-party content interface, query data, and then process the data storage.

Log configuration:

<loggers>
  <!--过滤掉spring和mybatis的一些无用的DEBUG信息-->
  <logger name="org.springframework" level="INFO"></logger>
  <logger name="org.mybatis" level="DEBUG"></logger>
  <root level="debug">
    <appender-ref ref="Console"/>
    <appender-ref ref="RollingFileDebug"/>
        ……
  </root>
</loggers>

As can be seen, the log console output, but also the different levels of logging output and baling. The final out of the log file directory like this:

Combined with the characteristics of the project, I do not know where we have not felt the need for improvement?

In just a period of time the project on-line, and there is nothing wrong with it found that the normal output log files, online direct knock out of the question command: tail -f console.log you can see real-time log output, find the problem. But also automatic daily log file archiving, retention, log history, it seems everything is so reasonable and comfortable.

However, after a period of operation the project, highlighting the problem gradually. One day, strong brother found in other projects to deploy the same machine when the disk is not enough? ? ? Odd strange, this machine did not put several projects, how there is no space for it, hit command to see which files are taking up large disk space:

find . -type f -size +800M  -print0 | xargs -0 du -h | sort -nr

Found on the line item after a period of time, console.log log file actually reached more than 30 G, which is too much. Unconsciously, the console output log of its own expansion project to the point that simply lawlessness. In addition to obtaining a third party look at the contents of the data output interface outside the main log output is printed sql statement. So that is a very simple approach, since the project has stabilized on the line, the output of sql statement Kan Bukan in fact have little effect. So, first log output level changed to INFO level, reducing print sql statement:

<loggers>
  <!--过滤掉spring和mybatis的一些无用的DEBUG信息-->
  <logger name="org.springframework" level="INFO"></logger>
  <logger name="org.mybatis" level="INFO"></logger>
  <root level="INFO">
    <appender-ref ref="Console"/>
    <appender-ref ref="RollingFileDebug"/>
    <appender-ref ref="RollingFileInfo"/>
    <appender-ref ref="RollingFileWarn"/>
    <appender-ref ref="RollingFileError"/>
  </root>
</loggers>

But not long before found that, although the change log output level, but because of third-party projects timers call log output interface is still very large, and console.log file does not automatically archived, the file will remain at breakneck a daily rate of expansion, will run out of disk.

There is no way greedy things at once, needless to say, today you want to kill this snake:

<loggers>
  <!--过滤掉spring和mybatis的一些无用的DEBUG信息-->
  <logger name="org.springframework" level="INFO"></logger>
  <logger name="org.mybatis" level="INFO"></logger>
  <root level="INFO">
    <appender-ref ref="RollingFileDebug"/>
    <appender-ref ref="RollingFileInfo"/>
    <appender-ref ref="RollingFileWarn"/>
    <appender-ref ref="RollingFileError"/>
  </root>
</loggers>

Directly to the console log deleted. Some people may ask: do not read the log output? A problem how to do? In fact, the log can still visible. We see that already have debug.log, info.log, error.log and other configuration files, and these files based on different output levels are also updated in real time. Then the console log in this case in fact completely unnecessary, and this stuff is more suitable for development for use.

After solving this problem, we continue to look at the configuration archive logs, do not know if you know the following configuration, how logs are archived. Various levels of logging after reaching what will archive a file? Up one level archive archive one day a few files? The number of files is full, the output log back how to do?


<RollingFile name="RollingFileDebug" fileName="${fileName}/debug.log"
filePattern="${fileName}/$${date:yyyy-MM}/debug-%d{yyyy-MM-dd}-%i.log">
<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="[xxx] [%d{yyyy-MM-dd HH:mm:ss,SSS}] [%t] [%p] %c{1}:%L - %m%n"/>
<Policies>
  <TimeBasedTriggeringPolicy/>
  <SizeBasedTriggeringPolicy size="10 MB"/>
</Policies>
<DefaultRolloverStrategy />
</RollingFile>

In fact, very simple, the content of the above configuration, note these three lines:

<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="10 MB"/>
<DefaultRolloverStrategy/>

The first line means: by time-triggered archiving, ie to a certain time, regardless of the current master file (such as: debug.log) How big are its archive (such as output: debug-2019-10-01- 1.log);

The second row means that: when the master file reaches 10M archive files.

The third line means: scroll according to a stored algorithm, the number of archives stored on the same day the level of primary log files. Used above is the default algorithm, the default archive file up to the main seven every day.

By the above configuration, the list of log files in the directory ./2019-10/ follows:

Then, one day a level default document is saved seven enough? Like the article mentioned above, a large amount of log output, leaving only seven still not enough? Right, in fact, we can set the configuration of the third row of the above, add the parameter min, max number of attributes such as file archiving limit set max = 20 even more will be able to save more copies log file.

Of course, the more log files, the more disk space occupied, but also set up 20 if not enough of it? The remaining new log is not to lose?

In fact, the default DefaultRolloverStrategy archiving strategy is: will delete the oldest log that day, then the front end of the log file numbers by -1 rename, and finally write a new log file with the highest number of the newly generated. In other words, throw away the old log, save the newer logs, yes, naked male slag characteristics -> grass is always greener. But this situation is also more in line with our view the log: general online after experiencing a problem, we all like to see if there is an error log of recent information, find the error and then solve the problem.

Of course, if our project log is more important, did not lose, such as payment-related systems. So, the above approach is not very suitable for the log file must always be preserved, can be considered for treatment by building a log on the line ELK log management systems. However, because this is not the focus of this article, not to mention the future have the opportunity to talk.

In the future we hope to use a something, can ponder see more of, a configuration is suitable for different environments, at least know what to do, so knock yourself out of the code will be more confident. Well, today to this, the next issue really want to start design patterns, ha ha!

No. get more public attention, there are problems in public may ask questions Oh No:

Strong brother hundred thousand forced hundred

Hundred thousand forced hundred programming, Internet and nothing new insights

Published 54 original articles · won praise 69 · Views 250,000 +

Guess you like

Origin blog.csdn.net/seanxwq/article/details/102790487