admin管理员组

文章数量:1356220

I am trying to create a logging file for my Spring scheduler app, but logback keeps creating a new file every time the scheduler starts. I want a single file from the time when the scheduler starts to when it stops.

Scheduler.java

@Scheduled(fixedRate = 5000)
public void schedule() {
    log.info("Starting log...");

    this.callSomething()
}

application.yml

logging:
  file:
    name: "application"
    path: "logs/"
  logback:
    rollingpolicy:
      max-history: 90
      file-name-pattern: logs/${logging.file.name}.%d{yyyy-MM-dd_HH-mm-ss}.%i.log
      max-file-size: 10MB
  level:
    root: info

The results are:

application.2025-03-29_17-50-02.0.log
application.2025-03-29_18-07-58.0.log
application.2025-03-29_18-08-03.0.log
application.2025-03-29_18-08-08.0.log
application.log

Besides the pattern-named logs, the "application.log" also gets created as the application shuts down.

How can I have the logs written in a single file?

I am trying to create a logging file for my Spring scheduler app, but logback keeps creating a new file every time the scheduler starts. I want a single file from the time when the scheduler starts to when it stops.

Scheduler.java

@Scheduled(fixedRate = 5000)
public void schedule() {
    log.info("Starting log...");

    this.callSomething()
}

application.yml

logging:
  file:
    name: "application"
    path: "logs/"
  logback:
    rollingpolicy:
      max-history: 90
      file-name-pattern: logs/${logging.file.name}.%d{yyyy-MM-dd_HH-mm-ss}.%i.log
      max-file-size: 10MB
  level:
    root: info

The results are:

application.2025-03-29_17-50-02.0.log
application.2025-03-29_18-07-58.0.log
application.2025-03-29_18-08-03.0.log
application.2025-03-29_18-08-08.0.log
application.log

Besides the pattern-named logs, the "application.log" also gets created as the application shuts down.

How can I have the logs written in a single file?

Share Improve this question edited Mar 30 at 11:09 Mark Rotteveel 110k229 gold badges156 silver badges223 bronze badges asked Mar 29 at 16:18 fireflyfirefly 3223 silver badges11 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

The behaviour you face is due to two misconfigurations:

  • Your rolling policy includes a timestamp down to seconds.

  • You don't use append: true, so a new file is created every time the application restarts.

This should work better:

file-name-pattern: logs/${logging.file.name}.%d{yyyy-MM-dd}.%i.log

By default the application.log and application-xxxx.log will created because that is specified in the application.properties due to logging.file.name and logging.logback.filenamepattern,when you start the scheduler capture the currentdatetime ,pass the currentdate time as a parameter to create a file and forwardingly write the logging statements there,before the scheduler stops capture the currentdatetime again and rename that file

本文标签: javaLogback creates new file log for every Scheduled callStack Overflow