jp.techie.achicoco.framework.util.LogUtil.java Source code

Java tutorial

Introduction

Here is the source code for jp.techie.achicoco.framework.util.LogUtil.java

Source

/*
 * Copyright 2012 bose999.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package jp.techie.achicoco.framework.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 * 
 * @author bose999
 *
 */
public class LogUtil {

    /**
     * 
     */
    private Log log = null;

    /**
     * 
     */
    private Class<?> clz;

    /**
     * 
     * 
     * @param clz Class
     */
    public LogUtil(Class<?> clz) {
        this.clz = clz;
        log = LogFactory.getLog(clz);
    }

    /**
     * FATAL
     * 
     * @return boolean
     */
    public boolean isFatalEnabled() {
        return log.isFatalEnabled();
    }

    /**
     * ERROR
     * 
     * @return boolean
     */
    public boolean isErrorEnabled() {
        return log.isErrorEnabled();
    }

    /**
     * WARN
     * 
     * @return boolean
     */
    public boolean isWarnEnabled() {
        return log.isWarnEnabled();
    }

    /**
     * INFO
     * 
     * @return boolean
     */
    public boolean isInfoEnabled() {
        return log.isInfoEnabled();
    }

    /**
     * DEBUG
     * 
     * @return boolean
     */
    public boolean isDebugEnabled() {
        return log.isDebugEnabled();
    }

    /**
     * TRACE
     * 
     * @return boolean
     */
    public boolean isTraceEnabled() {
        return log.isTraceEnabled();
    }

    /**
     * FATAL
     */
    public void fatal(String message, Exception e) {
        if (log.isFatalEnabled()) {
            log.fatal(makeLogMessage(message), e);
        }
    }

    /**
     * FATAL
     */
    public void fatal(String message) {
        if (log.isFatalEnabled()) {
            log.fatal(makeLogMessage(message));
        }
    }

    /**
      * ERROR
      */
    public void error(String message) {
        if (log.isErrorEnabled()) {
            log.error(makeLogMessage(message));
        }
    }

    /**
      * WARN
      */
    public void warn(String message) {
        if (log.isWarnEnabled()) {
            log.warn(makeLogMessage(message));
        }
    }

    /**
      * INFO
      */
    public void info(String message) {
        if (log.isInfoEnabled()) {
            log.info(makeLogMessage(message));
        }
    }

    /**
      * DEBUG
      */
    public void debug(String message) {
        if (log.isDebugEnabled()) {
            log.debug(makeLogMessage(message));
        }
    }

    /**
      * TRACE
      */
    public void trace(String message) {
        if (log.isTraceEnabled()) {
            log.trace(makeLogMessage(message));
        }
    }

    /**
     * ???
     * 
     * @param message ??
     * @return ?
     */
    protected String makeLogMessage(String message) {
        int lineNumber = getLineNumber(clz.getName());
        StringBuffer logMessageStringBuffer = new StringBuffer("Line:");
        logMessageStringBuffer.append(lineNumber);
        logMessageStringBuffer.append(" ");
        logMessageStringBuffer.append(message);
        return logMessageStringBuffer.toString();
    }

    /**
     * ??
     * 
     * @param className ??
     * @return 
     */
    protected static int getLineNumber(String className) {
        try {
            throw new Exception();
        } catch (Exception e) {
            StackTraceElement[] stackTraceElements = e.getStackTrace();
            for (int i = 0; i < stackTraceElements.length; i++) {
                StackTraceElement element = stackTraceElements[i];
                if (element != null && className.equals(element.getClassName())) {
                    return element.getLineNumber();
                }
            }
        }
        return 0;
    }
}