self4j日誌工具類 : LogUtil

import java.text.MessageFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 
 * 日誌工具類
 */
public class LogUtil {

    /**
     * logger
     */
    protected Logger log = null;

    private LogUtil(String logName) {
        log = LoggerFactory.getLogger(logName);
    }

    private LogUtil(Class<?> clazz) {
        log = LoggerFactory.getLogger(clazz);
    }
    
    private LogUtil(Logger log) {
        this.log=log;
    }
    
    /**
     * 
     * 
     * @param logName
     * @return
     */
    public static LogUtil getLogUtil(String logName) {
        return new LogUtil( logName); 
    }
    
    /**
     * 
     * 
     * @param clazz
     * @return
     */
    public static LogUtil getLogUtil(Class<?> clazz) {
        return new LogUtil(clazz);
    }
    
    public static LogUtil getLogUtil(Logger log) {
        return new LogUtil(log);
    }

    /**
     * debug日誌
     * 
     * @param message
     * @param args
     */
    public void debug(String message, Object... args) {

        if (log.isDebugEnabled()) {
            log.debug(MessageFormat.format(message, args));
        }
    }

    /**
     * debug日誌
     * 
     * @param message
     * @param args
     */
    public void debug(Throwable e, String message, Object... args) {

        if (log.isDebugEnabled()) {
            log.debug(MessageFormat.format(message, args), e);
        }
    }

    /**
     * info級別日誌
     * 
     * @param message
     * @param args
     */
    public void info(String message, Object... args) {

        if (log.isInfoEnabled()) {
            log.info(MessageFormat.format(message, args));
        }
    }

    /**
     * info級別日誌
     * 
     * @param message
     * @param args
     */
    public void info(Throwable e, String message, Object... args) {

        if (log.isInfoEnabled()) {
            log.info(MessageFormat.format(message, args), e);
        }
    }

    /**
     * warn級別日誌
     * 
     * @param message
     * @param args
     */
    public void warn(String message, Object... args) {

        if (log.isWarnEnabled()) {
            log.warn(MessageFormat.format(message, args));
        }
    }

    /**
     * warn級別日誌
     * 
     * @param message
     * @param args
     */
    public void warn(Throwable e, String message, Object... args) {

        if (log.isWarnEnabled()) {
            log.warn(MessageFormat.format(message, args), e);
        }
    }

    /**
     * error級別日誌
     * 
     * @param message
     * @param args
     */
    public void error(Throwable e, String message, Object... args) {

        if (log.isErrorEnabled()) {
            log.error(MessageFormat.format(message, args), e);
        }
    }

    /**
     * error級別日誌
     * 
     * @param message
     * @param args
     */
    public void error(String message, Object... args) {

        if (log.isErrorEnabled()) {
            log.error(MessageFormat.format(message, args));
        }
    }

}





使用:

在對應的類中增加常量

private static final LogUtil logger = LogUtil.getLogUtil(Test.class);

logger.info("日誌信息:{0}",msg);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章