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 badges2 Answers
Reset to default 0The 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
版权声明:本文标题:java - Logback creates new file log for every @Scheduled call - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744012402a2575805.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论