Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

java - How to use different fileName patterns based on the lifecycle environment

How to use different versions of logback.xml based on the active spring boot profile

src/main/resources/logback.xml
src/main/resources/logback-qa.xml
src/main/resources/logback-staging.xml
src/main/resources/logback-production.xml

My intention is to change the fileNamePattern alone based on the environment as well as the file path, I would prefer not to created additional files, if its easier to achieve it with a single file

<fileNamePattern>logs/app.%d{yyyy-MM-dd}.log</fileNamePattern>
<fileNamePattern>logs/app-qa.%d{yyyy-MM-dd}.log</fileNamePattern>
<fileNamePattern>logs/app-staging.%d{yyyy-MM-dd}.log</fileNamePattern>
<fileNamePattern>/logs/app-production.%d{yyyy-MM-dd}.log</fileNamePattern>
question from:https://stackoverflow.com/questions/65641002/how-to-use-different-filename-patterns-based-on-the-lifecycle-environment

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

logback-spring.xml supports springProfile tag and Spring Boot recommends to use logback-spring.xml instead of logback.xml. You can use springProfile tag in logback-spring.xml as follows:

<springProfile name="staging">
    <!-- configuration to be enabled when the "staging" profile is active -->
</springProfile>

<springProfile name="dev | staging">
    <!-- configuration to be enabled when the "dev" or "staging" profiles are active -->
</springProfile>

<springProfile name="!production">
    <!-- configuration to be enabled when the "production" profile is not active -->
</springProfile>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...