In Spring, how do I configure java.util.Logging so that it can be auto-updated?

In our webapp, we use java.util.Logging (JULI, actually, since we are deploying Tomcat 6). Logging is configured using the logging.properties file in WEB-INF / classes, a la this .

I would like to configure the logger so that it can be automatically installed, for example:

@Autowired
private Logger mylogger;

I searched the Spring, Web, and of course Stack Overflow forums, and I cannot find how to install this. I would appreciate any help with this.

Thank!

+3
source share
3 answers

One way would be to use the Java Config style , so you will have one bean like this:

@Configuration
public class LoggerProvider {
    @Bean
    public Logger logger() {
        return Logger.getLogger("foobar.whatever");
    }
}

.

+3

@Autowired - (a bean), bean spring -. , , .

, " ".

, (, ).

, @Logger, , , :

http://jgeeks.blogspot.com/2008/10/auto-injection-of-logger-into-spring.html

+2

To make Logger injective with @Autowired, you must have a configuration class in which you configured all the Beans that you use with @Autowired. This class will be marked @Configuration. There you should put the @Beanfollowing into the configuration :

@Configuration
public class WebConfiguration {

    @Bean
    @Scope("prototype")
    public Logger produceLogger(InjectionPoint injectionPoint) {
        Class<?> classOnWired = injectionPoint.getMember().getDeclaringClass();
        return LoggerFactory.getLogger(classOnWired);
    }
}
0
source

All Articles