Recent Tutorials and Articles
    Customizing Log Levels in Spring Boot Application
    Published on: 2018-09-08 08:09:35
    Posted By: Amit Kumar

    This article describes how to customize log levels using management endpoint and properties file in Spring Boot applications.

    Abstract


    Spring Boot has been getting quite popular for Microservices development for good reasons. One of the reasons is to provide ability to manipulate basic logging configurations without need of a dedicated logging configuration file.

    Please note that configurations demonstrated in this tutorial will only work with default Logback setup that comes with Spring Boot.

     

    Pre-requisites


    If you are new to actuator endpoints of Spring Boot, it is recommended to go through Mapping Spring Boot Endpoints to Custom Path.

     

    Changing Log Levels using Properties file


    You can modify the log level of ROOT by adding following property to application.properties file of your Spring Boot project - 

    # log_level can be one of OFF, TRACE, DEBUG, INFO, WARN and ERROR
    logging.level.root=<log_level>

     

    To change a package's log level, add a property in your application.properties on following lines -

    # log_level can be one of OFF, TRACE, DEBUG, INFO, WARN and ERROR
    logging.level.<package>=<log_level>

    For instance, to change, logging level of package com.aksain and its subpackages to WARN, following property can be added to application.properties -

    logging.level.com.aksain=WARN

     

    Changing Log Levels using Management Endpoint


    While it is super easy to change basic logging levels from application.properties, it still needs a restart to reflect the changes. However, it is sometimes desirable to change the log levels to debug a problem without restarting a service as after restarting, problem go away temporarity many a times.

    Spring Boot has introduced loggers endpoint starting Spring Boot 1.5.x.

    You can get current status of log levels of all package using following endpoint -

    curl http://<service-hostname>:<service-portno>/actuator/loggers

    In order to get current status of log level of a package, you can use following endpoint -

    curl http://<service-hostname>:<service-portno>/actuator/loggers/<root or packagename>

     

    To update log level, you can make a POST request to loggers endpoint -

    # log_level can be one of OFF, TRACE, DEBUG, INFO, WARN and ERROR
    curl -H "Content-Type: application/json" -X POST http://<service-hostname>:<service-portno>/actuator/loggers/<package_name> -d '{"configuredLevel":"<log_level>"}'

     

    For instance, to update log level of package com.aksain to WARN, we can make following request -

    curl -H "Content-Type: application/json" -X POST http://<service-hostname>:<service-portno>/actuator/loggers/com.aksain -d '{"configuredLevel":"WARN"}'

    Thank you for reading through the tutorial. In case of any feedback/questions/concerns, you can communicate same to us through your comments and we shall get back to you as soon as possible.

    Posted By: Amit Kumar
    Published on: 2018-09-08 08:09:35

    Comment Form is loading comments...