0
votes

I'm using Spring, log4j and slf4j. While trying to log some info from a @PostConstruct -annotated method log4j complains about not been yet initialized. When I later on log from this class log4j works OK.

private final static Logger log = LoggerFactory.getLogger(MyClass.class);

@PostConstruct
public void init() {
    log.info("initializing service");
}

Output:
log4j:WARN No appenders could be found for logger (mypackage.MyClass)

Log4j configuration is a a file-based one:

log4j.rootLogger=INFO, console   
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %-5p %c %x - %m%n
1

1 Answers

1
votes

the PostConstruct methods are called by Spring container right after dependency injection is done, but before init-methods. So in post-construct class instance isn't fully initialised. I would suggest using InitializingBean or init-method for that purpose.