Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot - number of backup log files restricted to 7

In our spring-boot project we are using slf4j for logging purpose. Below are configuration which we have added in application.properties file

logging.file=/opt/logs/my_log.log
logging.level.org.springframework.web=INFO
logging.level.org.hibernate=INFO
logging.level.nl.yestelecom.boss=DEBUG
logging.level.com.github.isrsal.logging.LoggingFilter=DEBUG

It generates only 7 backup files (my_log.log.1, my_log.log.2 ..., my_log.log.7) with each file of size 10.5MB and after that logging is not happening at all.

Is there any way to change this behavior?

We looked into available properties of spring-boot but, didn't find anything. Any suggestion is appreciated.

like image 847
Ravindra Devadiga Avatar asked Sep 21 '16 07:09

Ravindra Devadiga


People also ask

What is the default logging level in spring boot?

By default, Spring Boot logs only to the console and does not write log files. If you want to write log files in addition to the console output, you need to set a logging. file or logging. path property (for example, in your application.

How do I specify a logging file in spring boot?

To make Spring Boot write its log to disk, set the path and filename. With this configuration, Spring Boot will write to the console and also to a log file called spring. log , at the path you specify.

How do I disable spring boot logging?

In your case you must set logging. level. root on one of level from INFO, WARN, ERROR,FATAL or OFF to turn off all logging.


2 Answers

Spring-Boot only allows limited properties to be configured in its application.properties. See the list here.

The default (out-of-the-box) configuration that Spring-boot uses is defined in base.xml. See base.xml config here which includes this File appender

There are 2 ways to add extra configuration

  1. Add logback-spring.xml

If there is a logback configuration XML with name logback-spring.xml in project's classpath, it is picked up by Spring-Boot on initialization.

  1. Point to config file from application.properties

Within application.properties use following to point to your custom logback XML

logging.config= # Location of the logging configuration file. For instance `classpath:logback.xml` for Logback

Once you add the extra config using any of the above 2 steps, the rollover strategy can be mentioned within that custom XML like this

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <charset>UTF-8</charset>
            <Pattern>%d %-4relative [%thread] %-5level %logger{35} - %msg%n</Pattern>
        </encoder>
    </appender>
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
        <file>${LOG_FILE}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${LOG_FILE}.%i</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>10</maxIndex>
        </rollingPolicy>
        <triggeringPolicy
        class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MB</MaxFileSize>
        </triggeringPolicy>
    </appender>

    <root level="DEBUG">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE"/>
    </root>
</configuration>
like image 83
DeepakV Avatar answered Sep 20 '22 21:09

DeepakV


SFL4J is just wrapper. You need to add extra configuration for logback library:

<configuration>
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>logFile.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- daily rollover -->
      <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>

      <!-- keep 30 days' worth of history capped at 3GB total size -->
      <maxHistory>30</maxHistory>
      <totalSizeCap>3GB</totalSizeCap>

    </rollingPolicy>

    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
  </appender> 

  <root level="DEBUG">
    <appender-ref ref="FILE" />
  </root>
</configuration>

source

In this case we have logs from last 30 days but not bigger than 3GB.

like image 24
Koziołek Avatar answered Sep 23 '22 21:09

Koziołek