View Javadoc

1   package org.kuali.common.util.log.log4j.spring;
2   
3   import org.apache.log4j.ConsoleAppender;
4   import org.apache.log4j.PatternLayout;
5   import org.kuali.common.util.log.log4j.DefaultLog4JService;
6   import org.kuali.common.util.log.log4j.Log4JPatternConstants;
7   import org.kuali.common.util.log.log4j.Log4JService;
8   import org.kuali.common.util.log.log4j.ParamFactory;
9   import org.kuali.common.util.log.log4j.model.Appender;
10  import org.kuali.common.util.log.log4j.model.AppenderRef;
11  import org.kuali.common.util.log.log4j.model.Layout;
12  import org.kuali.common.util.log.log4j.model.Level;
13  import org.kuali.common.util.log.log4j.model.Log4JConfiguration;
14  import org.kuali.common.util.log.log4j.model.Logger;
15  import org.kuali.common.util.log.log4j.model.Param;
16  import org.kuali.common.util.log.log4j.model.Threshold;
17  import org.kuali.common.util.xml.service.XmlService;
18  import org.kuali.common.util.xml.spring.Log4JXmlServiceConfig;
19  import org.springframework.beans.factory.annotation.Autowired;
20  import org.springframework.context.annotation.Bean;
21  import org.springframework.context.annotation.Configuration;
22  import org.springframework.context.annotation.Import;
23  
24  @Configuration
25  @Import({ Log4JXmlServiceConfig.class })
26  public class Log4JConfig {
27  
28  	protected static final String SPRING = "org.springframework";
29  	protected static final String JAXB = "javax.xml.bind";
30  	protected static final String STDOUT = "stdout";
31  
32  	@Autowired
33  	XmlService service;
34  
35  	@Bean
36  	public Log4JService log4jService() {
37  		return new DefaultLog4JService(service);
38  	}
39  
40  	@Bean
41  	public Log4JConfiguration log4JContextDefault() {
42  		return getLog4JContext(Log4JPatternConstants.DEFAULT, Threshold.INFO);
43  	}
44  
45  	@Bean
46  	public Log4JConfiguration log4JContextJAXB() {
47  		return getLog4JContext(Log4JPatternConstants.DEBUG, Threshold.INFO);
48  	}
49  
50  	@Bean
51  	public Log4JConfiguration log4JContextTest() {
52  		return getLog4JContext(Log4JPatternConstants.DEBUG, Threshold.INFO);
53  	}
54  
55  	@Bean
56  	public Log4JConfiguration log4JContextDebug() {
57  		return getLog4JContext(Log4JPatternConstants.DEBUG, Threshold.DEBUG);
58  	}
59  
60  	@Bean
61  	public Log4JConfiguration log4JContextMaven() {
62  		Logger spring = new Logger(SPRING, new Level(Threshold.WARN));
63  		return getLog4JContext(Log4JPatternConstants.MAVEN, Threshold.INFO, spring);
64  	}
65  
66  	protected Log4JConfiguration getLog4JContext(String pattern, Threshold threshold) {
67  		return getLog4JContext(pattern, threshold, null);
68  	}
69  
70  	protected Log4JConfiguration getLog4JContext(String pattern, Threshold threshold, Logger logger) {
71  		Param param = ParamFactory.getPatternParam(pattern);
72  		Layout layout = new Layout(PatternLayout.class, param);
73  		Appender console = new Appender(STDOUT, ConsoleAppender.class, layout);
74  		AppenderRef ref = new AppenderRef(console.getName());
75  		Logger root = Logger.getRootLogger(ref, new Level(threshold));
76  		if (logger == null) {
77  			return new Log4JConfiguration.Builder(root).appender(console).reset(true).build();
78  		} else {
79  			return new Log4JConfiguration.Builder(root).appender(console).logger(logger).reset(true).build();
80  		}
81  	}
82  }