001/** 002 * Copyright 2010-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.common.util.log.log4j.spring; 017 018import org.apache.log4j.ConsoleAppender; 019import org.apache.log4j.PatternLayout; 020import org.kuali.common.util.log.log4j.DefaultLog4JService; 021import org.kuali.common.util.log.log4j.Log4JPatternConstants; 022import org.kuali.common.util.log.log4j.Log4JService; 023import org.kuali.common.util.log.log4j.ParamFactory; 024import org.kuali.common.util.log.log4j.model.Appender; 025import org.kuali.common.util.log.log4j.model.AppenderRef; 026import org.kuali.common.util.log.log4j.model.Layout; 027import org.kuali.common.util.log.log4j.model.Level; 028import org.kuali.common.util.log.log4j.model.Log4JConfiguration; 029import org.kuali.common.util.log.log4j.model.Logger; 030import org.kuali.common.util.log.log4j.model.Param; 031import org.kuali.common.util.log.log4j.model.Threshold; 032import org.kuali.common.util.xml.service.XmlService; 033import org.kuali.common.util.xml.spring.Log4JXmlServiceConfig; 034import org.springframework.beans.factory.annotation.Autowired; 035import org.springframework.context.annotation.Bean; 036import org.springframework.context.annotation.Configuration; 037import org.springframework.context.annotation.Import; 038 039@Configuration 040@Import({ Log4JXmlServiceConfig.class }) 041public class Log4JConfig { 042 043 protected static final String SPRING = "org.springframework"; 044 protected static final String JAXB = "javax.xml.bind"; 045 protected static final String STDOUT = "stdout"; 046 047 @Autowired 048 XmlService service; 049 050 @Bean 051 public Log4JService log4jService() { 052 return new DefaultLog4JService(service); 053 } 054 055 @Bean 056 public Log4JConfiguration log4JContextDefault() { 057 return getLog4JContext(Log4JPatternConstants.DEFAULT, Threshold.INFO); 058 } 059 060 @Bean 061 public Log4JConfiguration log4JContextJAXB() { 062 return getLog4JContext(Log4JPatternConstants.DEBUG, Threshold.INFO); 063 } 064 065 @Bean 066 public Log4JConfiguration log4JContextTest() { 067 return getLog4JContext(Log4JPatternConstants.DEBUG, Threshold.INFO); 068 } 069 070 @Bean 071 public Log4JConfiguration log4JContextDebug() { 072 return getLog4JContext(Log4JPatternConstants.DEBUG, Threshold.DEBUG); 073 } 074 075 @Bean 076 public Log4JConfiguration log4JContextMaven() { 077 Logger spring = new Logger(SPRING, new Level(Threshold.WARN)); 078 return getLog4JContext(Log4JPatternConstants.MAVEN, Threshold.INFO, spring); 079 } 080 081 protected Log4JConfiguration getLog4JContext(String pattern, Threshold threshold) { 082 return getLog4JContext(pattern, threshold, null); 083 } 084 085 protected Log4JConfiguration getLog4JContext(String pattern, Threshold threshold, Logger logger) { 086 Param param = ParamFactory.getPatternParam(pattern); 087 Layout layout = new Layout(PatternLayout.class, param); 088 Appender console = new Appender(STDOUT, ConsoleAppender.class, layout); 089 AppenderRef ref = new AppenderRef(console.getName()); 090 Logger root = Logger.getRootLogger(ref, new Level(threshold)); 091 if (logger == null) { 092 return new Log4JConfiguration.Builder(root).appender(console).reset(true).build(); 093 } else { 094 return new Log4JConfiguration.Builder(root).appender(console).logger(logger).reset(true).build(); 095 } 096 } 097}