Skip to main content

Logging

Overview​

In this lab, you will:

  • Configure different logging levels for different profiles
  • Create a custom log message to show the current profile

You will use project from the previous lab springboot-rest-demo.

Create Logging Levels for Dev Profile​

To create a set of logging levels for the dev profile:

  • Create a file called application-dev.properties in src/main/resources
  • Add the following:
Click here to view the example solution
# application-dev.properties
logging.level.root=DEBUG
logging.level.org.springframework.web=INFO

This will show detailed debug messages when running the dev profile. Spring web logs will be at INFO level.

Create Logging Levels for Test Profile​

To create a set of logging levels for the test profile:

  • Create a file called application-test.properties in src/main/resources
  • Add the following:
Click here to view the example solution
# application-test.properties
logging.level.root=INFO
logging.level.org.springframework.web=ERROR

# H2 in-memory database setup for tests
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.jpa.defer-datasource-initialization=true

Create Logging Levels for Prod Profile​

To create a set of logging levels for a prod profile:

  • Create a file called application-prod.properties in src/main/resources
  • Add the following:
Click here to view the example solution
# application-prod.properties
logging.level.root=WARN
logging.level.org.springframework.web=ERROR

Logging Level Explanations​

Dev profile (application-dev.properties):​

  • logging.level.root=DEBUG — You will see detailed debug logs for all packages.
  • logging.level.org.springframework.web=INFO — Spring web logs will be at INFO level.

Test profile (application-test.properties):​

  • logging.level.root=INFO — Only info, warning, and error logs will be shown for all packages.
  • logging.level.org.springframework.web=ERROR — Only errors from Spring web will be shown.

Prod profile (application-prod.properties):​

  • logging.level.root=WARN — Only warnings and errors will be shown for all packages.
  • logging.level.org.springframework.web=ERROR — Only errors from Spring web will be shown.

Understanding Logging Levels​

Logging levels in order of verbosity (most to least):

  1. TRACE - Very detailed information, typically only of interest when diagnosing problems
  2. DEBUG - Fine-grained informational events that are most useful to debug an application
  3. INFO - Informational messages that highlight the progress of the application
  4. WARN - Potentially harmful situations or unexpected events
  5. ERROR - Error events but the application can continue running
  6. FATAL - Very severe error events that might cause the application to terminate

How it works:

  • When you set a logging level, you see that level and all levels above it
  • For example, if you set INFO, you'll see INFO, WARN, ERROR, and FATAL messages
  • If you set WARN, you'll only see WARN, ERROR, and FATAL messages

Testing Different Profiles​

You can test the different logging configurations by running your application with different profiles:

Using Maven:

mvn spring-boot:run -Dspring-boot.run.profiles=dev
mvn spring-boot:run -Dspring-boot.run.profiles=test
mvn spring-boot:run -Dspring-boot.run.profiles=prod

Using Gradle:

./gradlew bootRun --args='--spring.profiles.active=dev'
./gradlew bootRun --args='--spring.profiles.active=test'
./gradlew bootRun --args='--spring.profiles.active=prod'

Using application.properties:

spring.profiles.active=dev

Running the Application with Different Profiles​

Using Maven​

Click here to view the Maven commands
# Run with dev profile
mvn spring-boot:run -Dspring-boot.run.profiles=dev

# Run with test profile
mvn spring-boot:run -Dspring-boot.run.profiles=test

# Run with prod profile
mvn spring-boot:run -Dspring-boot.run.profiles=prod

Using Gradle​

Click here to view the Gradle commands
# Run with dev profile
./gradlew bootRun --args='--spring.profiles.active=dev'

# Run with test profile
./gradlew bootRun --args='--spring.profiles.active=test'

# Run with prod profile
./gradlew bootRun --args='--spring.profiles.active=prod'

Alternative: Set in application.properties​

Click here to view the properties configuration
# Add this line to your main application.properties file
spring.profiles.active=dev

Note: When you switch between profiles, observe how the amount and verbosity of log output changes according to the logging levels configured for each profile.

Summary​

When you run your application with different profiles, the amount and detail of log output will change according to the logging level set for that profile. This helps you see more information during development and less (only important issues) in production.

Benefits of profile-specific logging:

  • Development: Detailed debugging information to help troubleshoot issues
  • Testing: Moderate logging to verify functionality without overwhelming detail
  • Production: Minimal logging focused on warnings and errors to reduce noise and improve performance