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