How to insert an active spring profile in a log

I am using 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 use properties with application.yml (application-dev.yml and application-production.yml)

Since the Logback spring extension starts before spring, I cannot insert the spring.profiles.active file 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="abc" 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 spring.profiles.active property.

My goal is to have a log file in the / var / log directory of the xyz-dev or xyz-production files, but I get xyz- spring.profiles.active_IS_UNDEFINED.log .

Approaches:

1 - Use of a component such as:

 @Component public class InitializationService implements ApplicationListener<ContextRefreshedEvent> { // inject spring profile active into logback.xml } 

doesn't work, of course, because the logback spring extension starts before the spring boot application.

2 - Using a property on logback.xml like this

 <file>${LOG_PATH}${APP_NAME}${PROFILE}.log</file> 

The result is xyz- spring.profiles.active_IS_UNDEFINED.log

+6
source share
3 answers

It's a bit late to answer, but I successfully registered the Spring profile by renaming my logback.xml file to logback- spring.xml and gaining access to a 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 get profile information.

Specific documentation here .

+8
source

Here is what I did for my project. Inside logback.xml

 <include resource="org/springframework/boot/logging/logback/defaults.xml"/> <property resource="application.properties"/> 

Then you can use the properties defined in the application.properties file. ${MY-PROPERTY} In my application.properties application, I have a property for the log file name.

This does not work with the application.yml => YAML properties file, because the yaml file yaml interpreted after the log is initialized.

+3
source
  • Since you are using Spring Boot, you can define logging.file=blah.log . See white papers .
  • Or you can use the vanilla log and pass the system variable: -DLOG_FILE=blah.log .

Why you should not use profiles:

What you are trying to do is not what Spring profiles were created for. They are needed to enable / disable beans to enable / disable behavior at startup. Profiles are not recommended in practice in general, since what you test and what works in production is different (although sometimes there is no other choice). See the Cautions section of the original announcement .

Another problem with using profiles is that multiple profiles can be activated at the same time. Profiles were created to turn on / off small unrelated parts. For instance. spring.profiles.active=cache-off,perf-monitoring-on or something like that.

With all that said, you can still define a profile and set the logging.file=prod.log in that profile. This is still very bad, not how to use profiles, but better.

+2
source

All Articles