web-dev-qa-db-fra.com

Configurer la journalisation à l'aide de plusieurs profils

J'essaie de scinder mon logback.xml par des profils sous springboot, voici mon approche:

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>

Et enfin utiliser:

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

Je suis en 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
31
Juan Reina Pascual

Documentation de démarrage Spring recommande d'utiliser logback-spring.xml plutôt que logback.xml et vous pouvez y utiliser la balise Spring Profile:

<configuration>
  <springProfile name="workspace">
    ...
  </springProfile>
  <springProfile name="dev,prd">
    ...
  </springProfile>
</configuration>
114
rhorvath

Si vous souhaitez utiliser différents fichiers de configuration de connexion pour différents profils, vous pouvez le modifier à partir de votre application-*.properties des dossiers.

Par exemple dans votre application-prod.properties tu peux dire:

logging.config=src/main/resources/logback-prod.xml
11
Bahadır Yağan