Hands on Coding: Spring Logging for Beginners

2 min readApr 6, 2024

1. Overview

Logging is a vital aspect of programming for both beginners and experts, often underestimated but crucial for understanding application behavior. Strategic placement of logging statements aids in debugging and comprehending program execution, especially in production environments.

This tutorial is hands-on so I’ll just paste the reference for you: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#features.logging

2. Hands-On Coding

Logging is available out-of-the-box from Spring, but we can customize its level, format, etc as defined in the document above.

I created a Spring Boot project to capture the behavior of the logging levels that you can use as a reference as you code.

2.1 Common Logging Interface

Let’s introduce an interface that we will implement with different logging-level classes. The method logLevels will print all the logs applicable to a particular package.

package com.czetsuyatech.logger;

import org.slf4j.Logger;

public interface LoggerComponent {

default void logLevels() {
System.out.println(getClass().getPackageName() + ": " + "-".repeat(50));

getLogger().info("Hello World");
getLogger().debug("Hello World");
getLogger().warn("Hello World");
getLogger().trace("Hello World");
getLogger().error("Hello World");

Logger getLogger();

2.2 Configure Package Names

We will define the following packages.

  • debug
  • error
  • info
  • trace
  • warn

For each package, we need to define a concrete class of interface LoggerComponent

For example, in the debug we will have something like:

public class DebugLogger implements LoggerComponent {

public Logger getLogger() {
return log;

2.3 Spring Boot Application Class

We will inject the instances of LoggerComponent in a list so that we can iterate through each component. We will call the logLevels method to print the log.

public class Application {

private final List<LoggerComponent> loggers;

public static void main(String[] args) {
SpringApplication.run(Application.class, args);

public void onStartup(ContextRefreshedEvent event) {


2.4 Spring Configuration File

And finally, we will configure the log level per package.

root: info
com.czetsuyatech.logger.info: info
com.czetsuyatech.logger.debug: debug
com.czetsuyatech.logger.warn: warn
com.czetsuyatech.logger.trace: trace
com.czetsuyatech.logger.error: error

We should have the following logs in our console which should pretty much explain the logging behavior. For example, error level only prints errors, while debug prints info, warn, and error as well.

3. Code Repository.

The code is available at https://github.com/czetsuya/lab-spring-logging.




Open for Collaboration | Senior Java Backend Developer