Tuesday, 27 August 2019

Spring boot: Configure logging level of the application


You can configure the logging level of spring boot application by adding below property to application.properties file.

logging.level.root={LOGGING_LEVEL}

Eample
logging.level.root=ERROR
logging.level.root=WARN
logging.level.root=INFO
logging.level.root=DEBUG
logging.level.root=TRACE

‘logging.level.root’ property set the root logging level.

Find the below working application.

application.properties
logging.level.root=WARN


HomeController.java
package com.sample.app.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RestController
public class HomeController {
 Logger logger = LoggerFactory.getLogger(HomeController.class);

 @RequestMapping("/")
 public String homePage() {
  logger.trace("A TRACE Message");
  logger.debug("A DEBUG Message");
  logger.info("An INFO Message");
  logger.warn("A WARN Message");
  logger.error("An ERROR Message");
  
  return "Welcome to Spring boot Application Development";
 }

}


App.java
package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
 public static void main(String[] args) {
  SpringApplication.run(App.class, args);

 }

}


pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>springbootMisc</groupId>
 <artifactId>springbootMisc</artifactId>
 <version>1</version>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.6.RELEASE</version>
 </parent>

 <dependencies>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

 </dependencies>
</project>

Run App.java.

Open the url ‘http://localhost:8080/’ in browser, you can see warning and error messages in console.

2019-07-18 22:07:21.230  WARN 40225 --- [nio-8080-exec-1] c.sample.app.controller.HomeController   : A WARN Message
2019-07-18 22:07:21.232 ERROR 40225 --- [nio-8080-exec-1] c.sample.app.controller.HomeController   : An ERROR Message


Total project structure looks like below.

You can download the complete working application from this link.


Previous                                                    Next                                                    Home

No comments:

Post a Comment