⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 log.java

📁 UrlRewriteFilter 是一个不错的URL转换工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * Copyright (c) 2005-2007, Paul Tuckey
 * All rights reserved.
 * ====================================================================
 * Licensed under the BSD License. Text as follows.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   - Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   - Neither the name tuckey.org nor the names of its contributors
 *     may be used to endorse or promote products derived from this
 *     software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 */
package org.tuckey.web.filters.urlrewrite.utils;

import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;


/**
 * Log copies the style of commons logging.  The only reason this exists is that many problems
 * were had with Log4j and commons-logging interfering with peoples installed configuration.
 * It is very easy to change this to log4j, slf4j or commons-logging by settting log level to "log4j", "slf4j" or "commons"
 * Note, this will fall back to system.log if not initialised with a context.
 *
 * @author Paul Tuckey
 * @version $Revision: 42 $ $Date: 2006-10-29 09:43:56 +1300 (Sun, 29 Oct 2006) $
 */
public class Log {

    private static Log localLog = Log.getLog(Log.class);

    // static vars
    private static ServletContext context = null;
    private static final String DEFAULT_LOG_LEVEL = "INFO";
    private static boolean usingSystemOut = false;
    private static boolean usingSystemErr = false;
    private static boolean usingLog4j = false;
    private static boolean usingSlf4j = false;
    private static boolean usingCommonsLogging = false;

    private static boolean traceLevelEnabled = false;
    private static boolean debugLevelEnabled = false;
    private static boolean infoLevelEnabled = false;
    private static boolean warnLevelEnabled = false;
    private static boolean errorLevelEnabled = false;
    private static boolean fatalLevelEnabled = false;

    private Class clazz = null;

    private org.apache.log4j.Logger log4jLogger = null;
    private org.slf4j.Logger slf4jLogger = null;
    private org.apache.commons.logging.Log commonsLog = null;

    private Log(Class clazz) {
        this.clazz = clazz;
        // check for log4j or commons
        isUsingLog4j();
        isUsingSlf4j();
        isUsingCommonsLogging();
    }

    private boolean isUsingLog4j() {
        if (usingLog4j && log4jLogger == null) {
            this.log4jLogger = org.apache.log4j.Logger.getLogger(clazz);
        }
        return usingLog4j;
    }

    private boolean isUsingSlf4j() {
        if (usingSlf4j && slf4jLogger == null) {
            this.slf4jLogger = org.slf4j.LoggerFactory.getLogger(clazz);
        }
        return usingSlf4j;
    }

    public boolean isUsingCommonsLogging() {
        if (usingCommonsLogging && commonsLog == null) {
            this.commonsLog = org.apache.commons.logging.LogFactory.getLog(clazz);
        }
        return usingCommonsLogging;
    }

    public boolean isTraceEnabled() {
        if (isUsingLog4j()) return log4jLogger.isEnabledFor(org.apache.log4j.Priority.DEBUG);
        if (isUsingSlf4j()) return slf4jLogger.isTraceEnabled();
        if (isUsingCommonsLogging()) return commonsLog.isTraceEnabled();
        return traceLevelEnabled;
    }

    public boolean isDebugEnabled() {
        if (isUsingLog4j()) return log4jLogger.isEnabledFor(org.apache.log4j.Priority.DEBUG);
        if (isUsingSlf4j()) return slf4jLogger.isDebugEnabled();
        if (isUsingCommonsLogging()) return commonsLog.isDebugEnabled();
        return traceLevelEnabled || debugLevelEnabled;
    }

    public boolean isInfoEnabled() {
        if (isUsingLog4j()) return log4jLogger.isEnabledFor(org.apache.log4j.Priority.INFO);
        if (isUsingSlf4j()) return slf4jLogger.isInfoEnabled();
        if (isUsingCommonsLogging()) return commonsLog.isInfoEnabled();
        return traceLevelEnabled || debugLevelEnabled || infoLevelEnabled;
    }

    public boolean isWarnEnabled() {
        if (isUsingLog4j()) return log4jLogger.isEnabledFor(org.apache.log4j.Priority.WARN);
        if (isUsingSlf4j()) return slf4jLogger.isWarnEnabled();
        if (isUsingCommonsLogging()) return commonsLog.isWarnEnabled();
        return traceLevelEnabled || debugLevelEnabled || infoLevelEnabled || warnLevelEnabled;
    }

    public boolean isErrorEnabled() {
        if (isUsingLog4j()) return log4jLogger.isEnabledFor(org.apache.log4j.Priority.ERROR);
        if (isUsingSlf4j()) return slf4jLogger.isErrorEnabled();
        if (isUsingCommonsLogging()) return commonsLog.isErrorEnabled();
        return traceLevelEnabled || debugLevelEnabled || infoLevelEnabled || warnLevelEnabled || errorLevelEnabled;
    }

    public boolean isFatalEnabled() {
        if (isUsingLog4j()) return log4jLogger.isEnabledFor(org.apache.log4j.Priority.FATAL);
        if (isUsingSlf4j()) return slf4jLogger.isErrorEnabled(); // note: SLF4J has no fatal level
        if (isUsingCommonsLogging()) return commonsLog.isFatalEnabled();
        return traceLevelEnabled || debugLevelEnabled || infoLevelEnabled || warnLevelEnabled || errorLevelEnabled || fatalLevelEnabled;
    }


    public void trace(Object o) {
        if (!isTraceEnabled()) {
            return;
        }
        if (isUsingLog4j()) {
            log4jLogger.debug(o);
            return;
        }
        if (isUsingSlf4j()) {
            slf4jLogger.trace(String.valueOf(o));
            return;
        }
        if (isUsingCommonsLogging()) {
            commonsLog.trace(o);
            return;
        }
        write("TRACE", o);
    }

    public void trace(Object o, Throwable throwable) {
        if (!isTraceEnabled()) {
            return;
        }
        if (isUsingLog4j()) {
            log4jLogger.debug(o, throwable);
            return;
        }
        if (isUsingSlf4j()) {
            slf4jLogger.trace(String.valueOf(o), throwable);
            return;
        }
        if (isUsingCommonsLogging()) {
            commonsLog.trace(o, throwable);
            return;
        }
        write("TRACE", o, throwable);
    }

    public void trace(Throwable throwable) {
        if (!isTraceEnabled()) {
            return;
        }
        if (isUsingLog4j()) {
            log4jLogger.debug(throwable);
            return;
        }
        if (isUsingSlf4j()) {
            slf4jLogger.trace("", throwable);
            return;
        }
        if (isUsingCommonsLogging()) {
            commonsLog.trace(throwable);
            return;
        }
        write("TRACE", throwable, throwable);
    }

    public void debug(Object o) {
        if (!isDebugEnabled()) {
            return;
        }
        if (isUsingLog4j()) {
            log4jLogger.debug(o);
            return;
        }
        if (isUsingSlf4j()) {
            slf4jLogger.debug(String.valueOf(o));
            return;
        }
        if (isUsingCommonsLogging()) {
            commonsLog.debug(o);
            return;
        }
        write("DEBUG", o);
    }

    public void debug(Object o, Throwable throwable) {
        if (!isDebugEnabled()) {
            return;
        }
        if (isUsingLog4j()) {
            log4jLogger.debug(o, throwable);
            return;
        }
        if (isUsingSlf4j()) {
            slf4jLogger.debug(String.valueOf(o), throwable);
            return;
        }
        if (isUsingCommonsLogging()) {
            commonsLog.debug(o, throwable);
            return;
        }
        write("DEBUG", o, throwable);
    }

    public void debug(Throwable throwable) {
        if (!isDebugEnabled()) {
            return;
        }
        if (isUsingLog4j()) {
            log4jLogger.debug(throwable);
            return;
        }
        if (isUsingSlf4j()) {
            slf4jLogger.debug("", throwable);
            return;
        }
        if (isUsingCommonsLogging()) {
            commonsLog.debug(throwable);
            return;
        }
        write("DEBUG", throwable, throwable);
    }

    public void info(Object o) {
        if (!isInfoEnabled()) {
            return;
        }
        if (isUsingLog4j()) {
            log4jLogger.info(o);
            return;
        }
        if (isUsingSlf4j()) {
            slf4jLogger.info(String.valueOf(o));
            return;
        }
        if (isUsingCommonsLogging()) {
            commonsLog.info(o);
            return;
        }
        write("INFO", o);
    }

    public void info(Object o, Throwable throwable) {
        if (!isInfoEnabled()) {
            return;
        }
        if (isUsingLog4j()) {
            log4jLogger.info(o, throwable);
            return;
        }
        if (isUsingSlf4j()) {
            slf4jLogger.info(String.valueOf(o), throwable);
            return;
        }
        if (isUsingCommonsLogging()) {
            commonsLog.info(o, throwable);
            return;
        }
        write("INFO", o, throwable);
    }

    public void info(Throwable throwable) {
        if (!isInfoEnabled()) {
            return;
        }
        if (isUsingLog4j()) {
            log4jLogger.info(throwable);
            return;
        }
        if (isUsingSlf4j()) {
            slf4jLogger.info("", throwable);
            return;
        }
        if (isUsingCommonsLogging()) {
            commonsLog.info(throwable);
            return;
        }
        write("INFO", throwable, throwable);
    }

    public void warn(Object o) {
        if (!isWarnEnabled()) {
            return;
        }
        if (isUsingLog4j()) {
            log4jLogger.warn(o);
            return;
        }
        if (isUsingSlf4j()) {
            slf4jLogger.warn(String.valueOf(o));
            return;
        }
        if (isUsingCommonsLogging()) {
            commonsLog.warn(o);
            return;
        }
        write("WARN", o);
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -