Skip to content

Commit

Permalink
improve logger
Browse files Browse the repository at this point in the history
  • Loading branch information
ddecrulle committed Dec 22, 2022
1 parent e2b4ddf commit 921d1f3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>fr.insee.knowledge</groupId>
<artifactId>knowledge</artifactId>
<version>0.4.1</version>
<version>0.4.2</version>
<packaging>jar</packaging>
<name>Knowledge-Back-Office</name>
<description>Back-office services for Knowledge</description>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package fr.insee.knowledge;

import fr.insee.knowledge.configuration.PropertiesLogger;
import fr.insee.knowledge.service.InitializerService;
import org.springdoc.core.SpringDocUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;

import java.io.IOException;


@SpringBootApplication(scanBasePackages = "fr.insee.knowledge", exclude = MongoAutoConfiguration.class)
public class Application {
public class StarterApplication {

static {
SpringDocUtils.getConfig().addHiddenRestControllers(BasicErrorController.class);
Expand All @@ -24,13 +25,18 @@ public class Application {
private InitializerService initializer;

public static void main(String[] args) {
SpringApplication.run(Application.class);
configureApplicationBuilder(new SpringApplicationBuilder()).build().run(args);
}

public static SpringApplicationBuilder configureApplicationBuilder(SpringApplicationBuilder springApplicationBuilder) {
return springApplicationBuilder.sources(StarterApplication.class)
.listeners(new PropertiesLogger());
}


@EventListener(ApplicationReadyEvent.class)
public void doSomethingAfterStartup() throws IOException {
initializer.createCollections();
initializer.importDataFromGithub();

}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package fr.insee.knowledge.configuration;

import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.Environment;
Expand All @@ -13,13 +14,13 @@
import java.util.Arrays;
import java.util.stream.StreamSupport;

@Configuration
public class PropertyLogger {
private static final Logger LOGGER = LoggerFactory.getLogger(PropertyLogger.class);
@Slf4j
public class PropertiesLogger implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
private static final Logger LOGGER = LoggerFactory.getLogger(PropertiesLogger.class);

@EventListener
public void handleContextRefresh(ContextRefreshedEvent event) {
final Environment env = event.getApplicationContext().getEnvironment();
@Override
public void onApplicationEvent(@NonNull ApplicationEnvironmentPreparedEvent event) {
final Environment env = event.getEnvironment();
LOGGER.info("====== Environment and configuration ======");
LOGGER.info("Active profiles: {}", Arrays.toString(env.getActiveProfiles()));
final MutablePropertySources sources = ((AbstractEnvironment) env).getPropertySources();
Expand All @@ -33,4 +34,5 @@ public void handleContextRefresh(ContextRefreshedEvent event) {
.forEach(prop -> LOGGER.info("{}: {}", prop, env.getProperty(prop)));
LOGGER.info("===========================================");
}

}

0 comments on commit 921d1f3

Please sign in to comment.