Automatically create new directory in date format after each log generation with Spring Boot logback

Tram Ho

In this article, I will guide you to use logback in spring boot and create a directory containing log files in the format “yyyy-MM-dd” and “yyyy-MM-dd hhmmss”:

Tools and libraries used in the article:

  • Spring tool suite 4
  • Spring boot 2.7.3
  • Maven 3
  • Java 8
  • Logback 1.2.11

1. Directory structure:

image.png

2. Contents of pom file:

By default, in spring boot when you add “spring-boot-starter-web” -> logback will be included: image.png

3. Configure logback:

Spring boot will automatically read the logback config according to the names below:

  • logback-spring.xml
  • logback.xml

In this article I use “logback-spring.xml”:

I will analyze a few variables in the log file config:

  • Variable value “byDate” -> create a folder containing log files with format “yyyy-MM-dd”
  • Variable value “bySecond” -> create folder containing log file with format “yyyy-MM-dd’T’HHmmss”
  • Variable value “${LOG_PATH}” -> default value will be taken from application.yml or application.properties:

image.png

Next I will start the service:

  • Service started success:

image.png

  • Check the directory containing the log file:

image.png

image.png

=> The directory containing the log and the log file was created successfully.

Next I will create a controller and declare a mapping to check if after performing an action (accessing the path declared in the controller) -> the folder containing the log will automatically generate a new folder or not?

I create a class like below:

image.png

I access the path just created in the controller and the result:

image.png

Check the directory containing the log:

image.png

Check log file contents:

image.png

You see there is a problem here, the directory containing the log is not automatically created after performing an action, the system will automatically update the log file content to the directory containing the previously created log. during the first service start. In order for the system to create a new log file directory, the service needs to be restarted. The reason is that the first time the service starts, the system remembers the path containing the log file and the next time the log is generated, the system does not automatically update in real time.

Here I will solve the problem by using SiftingAppender in the logback:

In the <discriminator> tag there are 2 tags:

  • The <key> tag contains the variable name, here I leave the variable name as “folderDate”
  • The <defaultValue> tag contains the value of the directory name that contains the log. we need to update this value in real time every time 1 action is performed

To update the <defaultValue> tag value, I will create a class that implements Filter, the purpose is to capture actions.

image.png

In the filter class I use the “doFilter()” function to catch the actions. In the function “doFilter()” I will take the real-time value and use “MDC” to update the value of the directory name containing the log with the value variable name “${folderDate}”

Declare this filter class in the “LogBackdemoApplication” startup class:

image.png

I will restart the service and check again:

  • For the first time starting the service:

image.png

image.png

  • For the second time trying to access the path declared in the controller:

image.png

image.png

image.png

Thus, the problem has been resolved. In this way, it is possible to create a directory containing logs in real time. In fact, it is possible to apply this method so that every day can create a directory containing logs in real time, if you want to create a directory containing logs in the format “yyyy-MM-dd” just use the value variable ”

b y EASY a t e t H a y v {byDate}” instead of “ {bySecond}”.

 

Share the news now

Source : Viblo