0
0
Spring Bootframework~5 mins

Package-level log configuration in Spring Boot

Choose your learning style9 modes available
Introduction

Package-level log configuration helps you control how much detail your app shows in logs for specific parts. It keeps logs clear and useful.

You want to see detailed logs only for a specific package while keeping others quiet.
You need to debug a problem in one part of your app without too much noise from other parts.
You want to reduce log size by limiting logs from less important packages.
You want different log levels for different packages in your Spring Boot app.
Syntax
Spring Boot
logging.level.<package-name>=<log-level>

Replace <package-name> with the Java package you want to configure, like com.example.service.

Replace <log-level> with levels like TRACE, DEBUG, INFO, WARN, ERROR.

Examples
Shows detailed debug logs only for the com.example.service package.
Spring Boot
logging.level.com.example.service=DEBUG
Sets info level logs for Spring Web package, so you see normal info messages but not debug details.
Spring Boot
logging.level.org.springframework.web=INFO
Sets the default log level to WARN for all packages unless overridden.
Spring Boot
logging.level.root=WARN
Sample Program

This example sets DEBUG logs for the controller package and ERROR logs for the repository package. When the controller method runs, debug and info logs appear. When the repository method runs, only error logs appear.

Spring Boot
application.properties
logging.level.com.example.controller=DEBUG
logging.level.com.example.repository=ERROR

// Sample Spring Boot Controller class
package com.example.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    private static final Logger logger = LoggerFactory.getLogger(HelloController.class);

    @GetMapping("/hello")
    public String sayHello() {
        logger.debug("Debug: Entering sayHello method");
        logger.info("Info: Returning greeting message");
        return "Hello, World!";
    }
}

// Sample Spring Boot Repository class
package com.example.repository;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

@Repository
public class DataRepository {
    private static final Logger logger = LoggerFactory.getLogger(DataRepository.class);

    public void fetchData() {
        logger.error("Error: Data fetch failed");
        logger.info("Info: This info will not show because level is ERROR");
    }
}
OutputSuccess
Important Notes

Package names are case-sensitive and must match your Java package structure exactly.

Use root to set a default log level for all packages.

Changing log levels at runtime may require restarting your Spring Boot app.

Summary

Package-level log configuration controls log detail per Java package.

Set it in application.properties using logging.level.<package>=<level>.

This helps focus on important logs and reduce noise.