Tentativo di inserire proprietà definite nello application.properties/application.yml
nello script logback.groovy
in Spring Boot progetto.Come ottenere le proprietà dell'ambiente da application.properties in logback.groovy nel progetto Spring Boot?
Non riesco a inserire Environment
o ApplicationContext
in script groovy.
Esistono soluzioni alternative?
Sono non alla ricerca di soluzioni come System.getProperty('spring.profiles.active')
src/main/risorse/logback.groovy
import org.springframework.core.env.Environment
@Inject private Environment env; //this is not working. how to get env here?
println "spring.profiles.active : ${env.getProperty('spring.profiles.active')}"
appender("STDOUT", ConsoleAppender) {
encoder(PatternLayoutEncoder) {
pattern = "%green(%d{HH:mm:ss.SSS}) [%thread] %highlight(%-5level) %cyan(%logger{36}) - %msg%n"
}
}
if(System.getProperty("spring.profiles.active")?.equalsIgnoreCase("prod")) {
root INFO, ["STDOUT", "FILE"]
} else {
root INFO, ["STDOUT"]
}
src/main/risorse/application.yml
---
spring:
profiles:
active: development
solo una parola di avvertimento: 'spring.profile * s * .active' - più di uno è permesso (e separato da una virgola). potresti voler usare un'espressione regolare o almeno un controllo "contiene". – cfrick
Buon punto. Mi assicurerò di gestire questo caso quando riesco a iniettare correttamente Environment in logback.groovy. – xmlking
@xmlking hai mai risolto questo? Avere lo stesso problema ora. – code