0% found this document useful (0 votes)
10 views3 pages

Spting Profiles

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Spting Profiles

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

How to Use Profiles in Spring Boot

What Are Profiles?


Every enterprise application has many environments, like:

Dev | Test | Stage | Prod | UAT / Pre-Prod

How Do we Maintain Profiles?


We make properties files for each environment and set the profile in the
application accordingly, so it will pick the respective properties file.
Spring Boot — by default — provides just one property file
( application.properties). So, how will we segregate the properties based on the
environment

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.

Then, we need to create three application.properties:

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?

We will use the application.properties to use the key below:


spring.profiles.active=dev
From here, Spring Boot will know which profile to pick. Let's run the application
now!

With the profile in DEV mode, and it should pick H2 DB.

Logging in Spring Boot


------------------------

Let’s first create a Spring Boot module.


Now let’s create our only class file, LoggingController:

@RestController
public class LoggingController {

Logger logger = LoggerFactory.getLogger(LoggingController.class);

@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");

return "Howdy! Check out the Logs to see the output...";


}
}
http://localhost:8080/.
In the case of logging, the only mandatory dependency is Apache Commons Logging.
We need to import it only when using Spring 4.x (Spring Boot 1.x) since it’s
provided by Spring Framework’s spring-jcl module in Spring 5 (Spring Boot 2.x).
We shouldn’t worry about importing spring-jcl at all if we’re using a Spring Boot
Starter (which we almost always are). That’s because every starter, like our
spring-boot-starter-web, depends on spring-boot-starter-logging, which already
pulls in spring-jcl for us.
When using starters, Logback is used for logging by default.

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:

java -jar target/spring-boot-logging-0.0.1-SNAPSHOT.jar --trace


Spring Boot also gives us access to a more fine-grained log level setting via
environment variables. There are several ways we can accomplish this.

First, we can set our logging level within our VM Options:

-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

If we want to change the verbosity permanently, we can do so in the


application.properties file as described here:

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:

<logger name="org.springframework" level="INFO" />


<logger name="com.baeldung" level="INFO" />
for more details https://www.baeldung.com/spring-boot-logging

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.

You might also like