Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SpringBoot系列之—日志打印的问题(经验总结) #42

Open
johnnian opened this issue Nov 18, 2017 · 0 comments

Comments

1 participant
@johnnian
Copy link
Owner

commented Nov 18, 2017

问题复盘

在公司的一个项目中,有几个子系统用SpringBoot运行,期间出现了一个问题:

由于我们的服务器用的是自建的MongoDB副本集,而MongoDB是出了名的 吃内存,由于系统内存被耗尽,导致一些基础设施的无法正常运行。

然后我们的几个子系统就狂打错误日志,就直接吧磁盘给占满了。真是无语了~

3640765df92c24b526a47e5aec9c4ba8

解决方法

目前SpringBoot的日志,使用Logback,原先的配置如下:

 <appender name="ROLLING_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">  
    	<level>INFO</level>  
    </filter>
    <file>/Users/Johnnian/info.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- daily rollover -->
      <fileNamePattern>/Users/Johnnian/info.%d{yyyy-MM-dd}.log</fileNamePattern>
      <maxHistory>7</maxHistory>
    </rollingPolicy>
    <encoder>
      <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%5p] [%t] [%c:%L] - %m%n</pattern>
    </encoder>
  </appender>

现在的处理:

  1. 日志设定当天最大存储大小;
  2. 设置所有日志总大小

Logback的rollingPolicy,除了ch.qos.logback.core.rolling.TimeBasedRollingPolicy 之外,还可以配置成ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy(大小限制+时间滚动处理)

新的配置如下:

 <appender name="ROLLING_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">  
    	<level>ERROR</level>  
    </filter>
    <file>/Users/Johnnian/info.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
      <!-- daily rollover -->
      <fileNamePattern>/Users/Johnnian/info.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
      <maxHistory>7</maxHistory>
      <maxFileSize>100MB</maxFileSize>
      <totalSizeCap>1GB</totalSizeCap>
    </rollingPolicy>
    <encoder>
      <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%5p] [%t] [%c:%L] - %m%n</pattern>
    </encoder>
  </appender>

注意:

fileNamePattern 字段配置: info.%d{yyyy-MM-dd}.%i.log, 需要添加 %i,否则启动报错。

参考链接

@johnnian johnnian added this to 框架 in Java Web Nov 18, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.