001package org.kuali.common.util.log.log4j.spring; 002 003import org.apache.log4j.ConsoleAppender; 004import org.apache.log4j.PatternLayout; 005import org.kuali.common.util.log.log4j.DefaultLog4JService; 006import org.kuali.common.util.log.log4j.Log4JPatternConstants; 007import org.kuali.common.util.log.log4j.Log4JService; 008import org.kuali.common.util.log.log4j.ParamFactory; 009import org.kuali.common.util.log.log4j.model.Appender; 010import org.kuali.common.util.log.log4j.model.AppenderRef; 011import org.kuali.common.util.log.log4j.model.Layout; 012import org.kuali.common.util.log.log4j.model.Level; 013import org.kuali.common.util.log.log4j.model.Log4JConfiguration; 014import org.kuali.common.util.log.log4j.model.Logger; 015import org.kuali.common.util.log.log4j.model.Param; 016import org.kuali.common.util.log.log4j.model.Threshold; 017import org.kuali.common.util.xml.service.XmlService; 018import org.kuali.common.util.xml.spring.Log4JXmlServiceConfig; 019import org.springframework.beans.factory.annotation.Autowired; 020import org.springframework.context.annotation.Bean; 021import org.springframework.context.annotation.Configuration; 022import org.springframework.context.annotation.Import; 023 024@Configuration 025@Import({ Log4JXmlServiceConfig.class }) 026public class Log4JConfig { 027 028 protected static final String SPRING = "org.springframework"; 029 protected static final String JAXB = "javax.xml.bind"; 030 protected static final String STDOUT = "stdout"; 031 032 @Autowired 033 XmlService service; 034 035 @Bean 036 public Log4JService log4jService() { 037 return new DefaultLog4JService(service); 038 } 039 040 @Bean 041 public Log4JConfiguration log4JContextDefault() { 042 return getLog4JContext(Log4JPatternConstants.DEFAULT, Threshold.INFO); 043 } 044 045 @Bean 046 public Log4JConfiguration log4JContextJAXB() { 047 return getLog4JContext(Log4JPatternConstants.DEBUG, Threshold.INFO); 048 } 049 050 @Bean 051 public Log4JConfiguration log4JContextTest() { 052 return getLog4JContext(Log4JPatternConstants.DEBUG, Threshold.INFO); 053 } 054 055 @Bean 056 public Log4JConfiguration log4JContextDebug() { 057 return getLog4JContext(Log4JPatternConstants.DEBUG, Threshold.DEBUG); 058 } 059 060 @Bean 061 public Log4JConfiguration log4JContextMaven() { 062 Logger spring = new Logger(SPRING, new Level(Threshold.WARN)); 063 return getLog4JContext(Log4JPatternConstants.MAVEN, Threshold.INFO, spring); 064 } 065 066 protected Log4JConfiguration getLog4JContext(String pattern, Threshold threshold) { 067 return getLog4JContext(pattern, threshold, null); 068 } 069 070 protected Log4JConfiguration getLog4JContext(String pattern, Threshold threshold, Logger logger) { 071 Param param = ParamFactory.getPatternParam(pattern); 072 Layout layout = new Layout(PatternLayout.class, param); 073 Appender console = new Appender(STDOUT, ConsoleAppender.class, layout); 074 AppenderRef ref = new AppenderRef(console.getName()); 075 Logger root = Logger.getRootLogger(ref, new Level(threshold)); 076 if (logger == null) { 077 return new Log4JConfiguration.Builder(root).appender(console).reset(true).build(); 078 } else { 079 return new Log4JConfiguration.Builder(root).appender(console).logger(logger).reset(true).build(); 080 } 081 } 082}