I'm using a spring boot project.
Environment:
ch.qos.logback:logback-core:jar:1.1.5
ch.qos.logback:logback-classic:jar:1.1.5
org.springframework.boot:spring-boot-starter-logging:jar:1.3.3.RELEASE
In my project I'm using properties with application.yml (application-dev.yml and application-production.yml)
Since the Logback Spring extension starts before Spring I'm not able to inject the spring.profiles.active into the logback.xml file.
This is a simpler version of my logback.xml file:
<configuration scan="true">
<property name="LOG_PATH" value="/var/log/" />
<property name="APP_NAME" value="xyz" />
<property name="PROFILE" value="-${spring.profiles.active}" />
<property name="CHARSET" value="utf-8" />
<property name="PATTERN" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" />
<appender name="APP-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}${APP_NAME}${PROFILE}.log</file>
<encoder>
<charset>${CHARSET}</charset>
<Pattern>${PATTERN}</Pattern>
</encoder>
</appender>
<logger name="a.b.c" level="INFO">
<appender-ref ref="APP-FILE" />
</logger>
<root level="INFO">
<appender-ref ref="APP-FILE"/>
</root>
The PROFILE I'm looking for is the property spring.profiles.active.
My goal is to have a log file on directory /var/log the files xyz-dev or xyz-production but I getting xyz-spring.profiles.active_IS_UNDEFINED.log instead of course.
Approaches:
1 - Using a component like:
@Component
public class InitializationService implements ApplicationListener<ContextRefreshedEvent> {
// inject spring profile active into logback.xml
}
is not working of course because the logback Spring extension starts before Spring Boot application.
2 - Using a property on the logback.xml like this
<file>${LOG_PATH}${APP_NAME}${PROFILE}.log</file>
The result is xyz-spring.profiles.active_IS_UNDEFINED.log
This is a little late to answer, but I have successfully logged the Spring profile by renaming my "logback.xml" file to "logback-spring.xml", and accessed the profile like this (much) simplified version
<springProperty scope="context" name="ACTIVE_PROFILE" source="spring.profiles.active"/>
<appender name="GRAYLOG" class="com.github.pukkaone.gelf.logback.GelfAppender">
<additionalField>environment=${ACTIVE_PROFILE}</additionalField>
</appender>
<root level="WARN">
<appender-ref ref="GRAYLOG" />
</root>
It seems that "logback-spring.xml" can pick up the profile information.
The specific documentation is here.