001/**
002 * Copyright 2005-2016 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.rice.core.util;
017
018import org.apache.log4j.Logger;
019import static org.apache.log4j.Level.WARN;
020import static org.apache.log4j.Level.ERROR;
021import static org.apache.log4j.Level.FATAL;
022
023
024/**
025 * Class with static methods wrapping {@link Logger} methods. Automatically sets up logger for you. It's called the <code>BufferedLogger</code> because
026 * it handles everything in a {@link StringBuilder} using {@link StringBuilder#append(CharSequence)} method<br/>
027 * <br/>
028 *  
029 * To use these just do
030 * <code>
031 * import BufferedLogger.*
032 * </code>
033 * 
034 * @see org.apache.log4j.Logger
035 */
036public class BufferedLogger {
037    
038    /**
039     * Applies a pattern with parameters to create a {@link String} used as a logging message
040     *  
041     * 
042     * @param pattern to format against
043     * @param objs an array of objects used as parameters to the <code>pattern</code>
044     * @return Logging Message
045     */
046    private static final CharSequence getMessage(Object ... objs) {
047        StringBuilder retval = new StringBuilder();
048        
049        for (Object obj : objs) {
050            retval.append(obj);
051        }
052        
053        return retval;
054    }
055    
056    /**
057     * Uses {@link StackTraceElement[]} from {@link Throwable} to determine the calling class. Then, the {@link Logger} is retrieved for it by
058     * convention
059     * 
060     * 
061     * @return Logger for the calling class
062     */
063    private static final Logger getLogger() {
064        try {
065            return Logger.getLogger(Class.forName(new Throwable().getStackTrace()[2].getClassName()));
066        }
067        catch (Exception e) {
068            // This will never happen unless Java is broken
069            return Logger.getLogger(BufferedLogger.class);
070        }
071    }
072
073    /**
074     * Uses {@link StackTraceElement[]} from {@link Throwable} to determine the calling class. Then, the {@link Logger} is retrieved for it by
075     * convention. Just like {@link #getLogger()} except this is intended to be called directly from classes.
076     * 
077     * 
078     * @return Logger for the calling class
079     */
080    public static final Logger logger() {
081        try {
082            return Logger.getLogger(Class.forName(new Throwable().getStackTrace()[1].getClassName()));
083        }
084        catch (Exception e) {
085            // This will never happen unless Java is broken
086            return Logger.getLogger(BufferedLogger.class);
087        }
088    }
089
090    /**
091     * Wraps {@link Logger#trace(String)}
092     * 
093     * @param pattern to format against
094     * @param objs an array of objects used as parameters to the <code>pattern</code>
095     */
096    public static final void trace(Object ... objs) {
097        Logger log = getLogger();
098        if (log.isTraceEnabled()) {
099            log.trace(getMessage(objs));
100        }
101    }
102
103    /**
104     * Wraps {@link Logger#debug(String)}
105     * 
106     * @param pattern to format against
107     * @param objs an array of objects used as parameters to the <code>pattern</code>
108     */
109    public static final void debug(Object ... objs) {
110        Logger log = getLogger();
111        if (log.isDebugEnabled()) {
112            log.debug(getMessage(objs));
113        }
114    }
115
116    /**
117     * Wraps {@link Logger#info(String)}
118     * 
119     * @param pattern to format against
120     * @param objs an array of objects used as parameters to the <code>pattern</code>
121     */
122    public static final void info(Object ... objs) {
123        Logger log = getLogger();
124        if (log.isInfoEnabled()) {
125            log.info(getMessage(objs));
126        }
127    }
128
129    /**
130     * Wraps {@link Logger#warn(String)}
131     * 
132     * @param pattern to format against
133     * @param objs an array of objects used as parameters to the <code>pattern</code>
134     */
135    public static final void warn(Object ... objs) {
136        Logger log = getLogger();
137        if (log.isEnabledFor(WARN)) {
138            log.warn(getMessage(objs));
139        }
140    }
141
142    /**
143     * Wraps {@link Logger#error(String)}
144     * 
145     * @param pattern to format against
146     * @param objs an array of objects used as parameters to the <code>pattern</code>
147     */
148    public static final void error(Object ... objs) {
149        Logger log = getLogger();
150        if (log.isEnabledFor(ERROR)) {
151            getLogger().error(getMessage(objs));
152        }
153    }
154    
155    /**
156     * Wraps {@link Logger#fatal(String)}
157     * 
158     * @param pattern to format against
159     * @param objs an array of objects used as parameters to the <code>pattern</code>
160     */
161    public static final void fatal(Object ... objs) {
162        Logger log = getLogger();
163        if (log.isEnabledFor(FATAL)) {
164            log.fatal(getMessage(objs));
165        }
166    }
167}