java - Log4j not initialized when logger is called from a @PostConstruct -annotated method -
i'm using spring, log4j , slf4j. while trying log info @postconstruct -annotated method log4j complains not been yet initialized. when later on log 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 found logger (mypackage.myclass)
log4j configuration 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
the postconstruct
methods called spring container right after dependency injection done, before init-methods. in post-construct class instance isn't initialised. suggest using initializingbean
or init-method
purpose.