Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I inject a logger into a field in the sample spring boot application?

Tags:

What I want is to make spring autowire a logger. So, in other words, I want to have this working:

import javax.servlet.http.HttpServletResponse;  import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;  @Controller public class MainController {      @Autowired     private Logger logger;          @RequestMapping("/")     public String enterSite(HttpServletResponse response) {         logger.info("site entered");         return "welcome";     } } 

Right now it throws an exception at startup: "No qualifying bean of type [org.slf4j.Logger] found for dependency...".

My pom.xml dependencies:

<parent>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-parent</artifactId>         <version>1.2.0.M1</version>         <relativePath /> <!-- lookup parent from repository -->     </parent>      <dependencies>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter</artifactId>         </dependency>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-data-jpa</artifactId>         </dependency>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-web</artifactId>         </dependency>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-data-rest</artifactId>         </dependency>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-actuator</artifactId>         </dependency>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-test</artifactId>             <scope>test</scope>         </dependency>         <dependency>             <groupId>org.apache.tomcat.embed</groupId>             <artifactId>tomcat-embed-jasper</artifactId>             <scope>provided</scope>         </dependency>         <dependency>             <groupId>javax.servlet</groupId>             <artifactId>jstl</artifactId>         </dependency>          <dependency>             <groupId>postgresql</groupId>             <artifactId>postgresql</artifactId>             <version>9.1-901.jdbc4</version>         </dependency>         <!-- <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId>              </dependency> -->     </dependencies> 

I read this: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-logging

It says if you use one of the starter poms (I do) Logback is used - but for internal logging. Can it be autowired in my classes?

like image 582
vic Avatar asked Oct 22 '14 21:10

vic


People also ask

How do I add loggers to Spring boot?

React + Spring Boot Microservices and SpringSpring Boot uses Apache Commons logging for all internal logging. Spring Boot's default configurations provides a support for the use of Java Util Logging, Log4j2, and Logback. Using these, we can configure the console logging as well as file logging.


2 Answers

If the objective here is code reduction then try Project Lombok. You then don't even need to declare the logger - just add an annotation and use log instead of logger

So your above code will now look like this:

import javax.servlet.http.HttpServletResponse;  import org.slf4j.Logger; // import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;  import lombok.extern.slf4j.Slf4j;  @Slf4j @Controller public class MainController {      @RequestMapping("/")     public String enterSite(HttpServletResponse response) {         log.info("site entered");         return "welcome";     } } 
like image 186
Simon Jenkins Avatar answered Oct 05 '22 01:10

Simon Jenkins


Although it is not the usual way you can add a logger bean directly in your context reproducing the classic bind:

private final Logger logger = LoggerFactory.getLogger(MainController.class); 

simply inserting in the spring context:

<bean id="logger" scope="prototype" class="org.slf4j.LoggerFactory" factory-method="getLogger">     <constructor-arg name="name" value="youLoggerName" /> </bean> 

then you can simply inject your logger:

@Autowired private Logger logger; 
like image 21
fl4l Avatar answered Oct 05 '22 02:10

fl4l