Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure logback using several profiles

I´m trying to split my logback.xml by profiles under springboot, this my approach:

logback-prod.xml

<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/defaults.xml" /> <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:- ${java.io.tmpdir:-/tmp}}/}spring.log}"/> <include resource="org/springframework/boot/logging/logback/file- appender.xml" />  <root level="INFO">     <appender-ref ref="CONSOLE" /> </root> </configuration>  

logback-dev.xml

<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/defaults.xml" /> <include resource="org/springframework/boot/logging/logback/console-appender.xml" />  <root level="DEBUG">     <appender-ref ref="CONSOLE" /> </root> 

logback.xml

<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="logback-${spring.profiles.active}.xml"/>  <root level="INFO">     <appender-ref ref="FILE" />     <appender-ref ref="CONSOLE" /> </root> 

And finally use:

-Dspring.profiles.active=dev  or -Dspring.profiles.active=prod 

I got in console:

13:01:44,673 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@2:16 - no  applicable action for [configuration], current ElementPath  is [[configuration][configuration]] 13:01:44,674 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@3:81 - no applicable action for [include], current ElementPath  is [[configuration][configuration][include]] 13:01:44,674 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@4:89 - no applicable action for [include], current ElementPath  is [[configuration][configuration][include]] 13:01:44,674 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@6:25 - no applicable action for [root], current ElementPath  is [[configuration][configuration][root]] 13:01:44,674 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@7:39 - no applicable action for [appender-ref], current ElementPath  is [[configuration][configuration][root][appender-ref]] 13:01:44,675 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO 
like image 438
Juan Reina Pascual Avatar asked Feb 26 '16 12:02

Juan Reina Pascual


2 Answers

Spring boot documentation recommends to use logback-spring.xml rather than logback.xml and in it you can use spring profile tag:

<configuration>   <springProfile name="workspace">     ...   </springProfile>   <springProfile name="dev,prd">     ...   </springProfile> </configuration> 
like image 162
rhorvath Avatar answered Sep 18 '22 15:09

rhorvath


If you want different logback configuration files for different profiles, you can change it from your application-*.properties files.

For example in your application-prod.properties you can say:

logging.config=src/main/resources/logback-prod.xml 
like image 30
Bahadır Yağan Avatar answered Sep 19 '22 15:09

Bahadır Yağan