no

Hands on Coding: Spring Logging for Beginners

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:

@Component
@Slf4j
public class DebugLogger implements LoggerComponent {

  @Override
  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.

@SpringBootApplication
@RequiredArgsConstructor
public class Application {

  private final List<LoggerComponent> loggers;

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

  @EventListener
  public void onStartup(ContextRefreshedEvent event) {

    loggers.stream().forEach(LoggerComponent::logLevels);
  }
}

2.4 Spring Configuration File

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

logging:
  level:
    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. When to Use Each Logger

There is no standard on how and when to use the different logging levels. In my own experience, this is how I use them. Coupled with proper package structure, so I can control the logging level of a particular process.

Info
- Informational messages without state such as component startup and configuration settings.

Warn
- Informational state that can be ignored by the system. 
- Example: An unknown status of an entity.

Debug
- Informational events with state that are useful for developers. I use this in public methods.
- Example: Controller endpoints and service methods.

Trace
- Informational events with state that are useful for developers. I use this in private methods.
- Example: Private methods in a service.

Error
- Event that causes the application to get into an error state. Recoverable.
- Example: Unable to communicate to an external service. Having a mechanism to retry sending the message.

Fatal
- Event that causes the application to get into an error state. Unrecoverable.
- Example: Database instance going down.

4. Code Repository

Related

spring 2277630675979384350

Post a Comment Default Comments

item