Spting Profiles
Spting Profiles
The solution would be to create more property files and add the "profile" name as
the suffix and configure Spring Boot to pick the appropriate properties based on
the profile.
application-dev.properties
application-test.properties
application-prod.properties
, the application.properties will remain as a master properties file, but if we
override any key in the profile-specific file, the latter will gain precedence.
I will now define DB configuration properties for in respective properties file
and add code in DBConfiguration.class to pick the appropriate settings.
application.properties
spring.application.name
app.message=This is the ${spring.application.name}
application_dev.properties
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;
spring.datasource.username=
spring.datasource.password=
We have used the @Profile("Dev") to let the system know that this is the BEAN
that should be picked up when we set the application profile to DEV. The other two
beans will not be created at all
One last setting is how to let the system know that this is DEV, TEST, or PROD.
But, how do we do this?
@RestController
public class LoggingController {
@RequestMapping("/")
public String index() {
logger.trace("A TRACE Message");
logger.debug("A DEBUG Message");
logger.info("An INFO Message");
logger.warn("A WARN Message");
logger.error("An ERROR Message");
As we can see, the default logging level of the Logger is preset to INFO, meaning
that TRACE and DEBUG messages are not visible.
In order to activate them without changing the configuration, we can pass the –
debug or –trace arguments on the command line:
-Dlogging.level.org.springframework=TRACE
-Dlogging.level.com.baeldung=TRACE
Alternatively, if we’re using Maven, we can define our log settings via the command
line:
mvn spring-boot:run
-Dspring-boot.run.arguments=--logging.level.org.springframework=TRACE,--
logging.level.com.baeldung=TRACE
logging.level.root=WARN
logging.level.com.baeldung=TRACE
Finally, we can change the logging level permanently by using our logging framework
configuration file.
We mentioned that Spring Boot Starter uses Logback by default. Let’s see how to
define a fragment of a Logback configuration file in which we set the level for two
separate packages:
Changing the Logging Level at the Runtime for a Spring Boot Application
-----------------------------------------------------------------------
https://www.baeldung.com/spring-boot-changing-log-level-at-
runtime#:~:text=2.-,Spring%20Boot%20Actuator,as%20part%20of%20the%20path.