Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Logging Levels for nested classes in Spring Boot Properties

I am attempting to set logging levels separately for 2 nested class loggers, an example of this being loggers that are named:

com.package.ParentClass$LoggerOne and com.package.ParentClass$LoggerTwo. Adding loggers in the logback.xml file for those exact loggers works fine, but it doesn't seem to have any effect when trying to set the loggers for spring boot's logging.level properties group.

A class with these loggers would look something like this:

class ParentClass {

    private static Logger logger = LoggerFactory.getLogger(ParentClass.class);

    // code that uses ParentClass logger

    class LoggerOne {

        private static Logger logger = LoggerFactory.getLogger(LoggerOne.class);

        // code that uses LoggerOne logger


    }

    class LoggerTwo {

        private static Logger logger = LoggerFactory.getLogger(LoggerTwo.class);

        // code that uses LoggerTwo logger

    }

}
like image 463
Bwvolleyball Avatar asked Aug 31 '26 06:08

Bwvolleyball


2 Answers

The solution, as found on this GitHub issues comment, is to surround the logger class / sub-class in [].

Example:

logging:
  level:
    com.package.[ParentClass$LoggerOne]: INFO
    com.package.[ParentClass$LoggerTwo]: ERROR
    com.package.ParentClass: DEBUG

This allows fine-grained control of logging levels for the parent class and any nested class contained within the parent.

like image 83
Bwvolleyball Avatar answered Sep 02 '26 20:09

Bwvolleyball


I tried to use brackets, and it works for me:

logging.level.com.package[ParentClass$LoggerOne]=INFO
logging.level.com.package[ParentClass$LoggerTwo]=DEBUG
like image 45
leafriend Avatar answered Sep 02 '26 22:09

leafriend



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!