Spring Boot Log with Logback

May 25, 2020 | No comments



Spring Boot Logging with Logback

In this article we will cover some important concepts about logging with Spring Boot and Logback. Spring Boot uses Commons Logging for all internal logging but leaves the underlying log implementation open. Spring Boot supports logging using Java Util Logging, Log4J2, and Logback. By default, if we use the “Starters”, Logback is used for logging. In this article we will work with Logback for logging with a Spring Boot Application. Logback Loggers may be assigned levels. The set of possible levels are (TRACE, DEBUG, INFO, WARN and ERROR) We will go through the details of turning on Logging at different levels also different levels under different environments (different Profiles)

Purpose

In this article we will walk through a simple Spring Boot application configured with logback for logging. In this application, we will enable different logging under different profiles though logback_profile.xml file.
Application Details
ProfileController controller contains log messages for each log level (TRACE, DEBUG, INFO, WARN, and ERROR). This controller can be accessed at localhost:8080/profiles. The controller will return the name of the active profile to the browser. This controller will write log messages to the console with different available levels as per the active profile. We configure the different log level for different profiles using logback-profile.xml

Project Structure


Technology used
Spring Boot 2.2.6.RELEASE Logback 1.2.3 Maven 3 Java 11

Code reference

applications.properties
# if no active profile, default is 'default'
# spring.profiles.active=dev
mail.smtp.host=localhost.mail.com
mail.smtp.port=0000  
mail.smtp.user=rootadmin
appserver.version=1

# logging level
logging.level.org.springframework=ERROR
#logging.level.com.bootng=DEBUG
# root level
#logging.level.=INFO
# output to a file
logging.file=springboot-profile-app.log
logging.pattern.file=%d %p %c{1.} [%t] %m%n
logging.pattern.console=%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 <springProfile name="default">
  <appender name="stdout"
   class="ch.qos.logback.core.ConsoleAppender">
   <encoder>
    <pattern>%d{yy-MMMM-dd HH:mm:ss:SSS} %5p %t %c{2}:%L - %m%n
    </pattern>
   </encoder>
  </appender>
  <root level="DEBUG">
   <appender-ref ref="stdout" />
  </root>
  <logger name="com.bootng" level="DEBUG" />
 </springProfile>

 <springProfile name="dev">
  <logger name="com.bootng" level="DEBUG" />
 </springProfile>

 <springProfile name="prod">
  <logger name="com.bootng" level="ERROR" />
 </springProfile>
</configuration>
ProfileController
@Controller
public class ProfileController {
 private static final Logger logger = LoggerFactory.getLogger(ProfileController.class);
 @Autowired
  private Environment environment;
  @RequestMapping(value = {"/profile"}, method = RequestMethod.GET,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public @ResponseBody ResponseEntity <String> paymentInfo() {
    logger.trace("Inside GET profile");
    logger.debug("Inside GET profile");
    logger.info("Inside GET profile");
    logger.warn("Inside GET profile");
    logger.error("Inside GET profile");
    
    String result = "Currently active profile - ";
    for (String profileName : environment.getActiveProfiles()) {
       result+=" " + profileName;
    }  
    return new ResponseEntity<String>(result, HttpStatus.OK);
  }
 }

Run Application

With dev profile
Run the application in with active profile=dev and go to localhost:8080/profile. You can see the following logs.
mvn spring-boot:run -Dspring-boot.run.profiles=dev
Console
01:39:43.304 [http-nio-8080-exec-1] TRACE com.bootng.ProfileController - Inside GET profile
01:39:43.305 [http-nio-8080-exec-1] DEBUG com.bootng.ProfileController - Inside GET profile
01:39:43.305 [http-nio-8080-exec-1] INFO com.bootng.ProfileController - Inside GET profile
01:39:43.305 [http-nio-8080-exec-1] WARN com.bootng.ProfileController - Inside GET profile
01:39:43.305 [http-nio-8080-exec-1] ERROR com.bootng.ProfileController - Inside GET profile
With prod profile
Run the application in with active profile=prod and go to localhost:8080/profile. You can see the following logs.
mvn spring-boot:run -Dspring-boot.run.profiles=prod
Console
1:40:27.179 [http-nio-8080-exec-1] ERROR com.bootng.ProfileController - Inside GET profile
You can see the difference in the logs from the same controller under different profiles.

Summary and Git Project

Download Source Code
git clone git@github.com:bootng/spring-boot-references.git cd spring-boot-profiles #Build the project mnv install # Run the application with dev set as the active profile mvn spring-boot:run -Dspring-boot.run.profiles=dev # Run the application with prod set as the active profile mvn spring-boot:run -Dspring-boot.run.profiles=prod
Summary
We can configure log levels differently for different packages. We can configure a log for different profiles using the springProfile tag. Logback recommends using logback-spring.xml for configuring logs. logback-spring.xml is optional and used to control the logs though different appenders or levels. we can also turn on logs for different packages or root using application.properties

References

No comments :

Post a Comment

Please leave your message queries or suggetions.