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

📄 log.java

📁 urlrewritefilter-2.6-src.zip
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        if (isUsingCommonsLogging()) {
            commonsLog.warn(o, throwable);
            return;
        }
        write("WARN", o, throwable);
    }

    public void warn(Throwable throwable) {
        if (!isWarnEnabled()) {
            return;
        }
        if (isUsingLog4j()) {
            log4jLogger.warn(throwable);
            return;
        }
        if (isUsingCommonsLogging()) {
            commonsLog.warn(throwable);
            return;
        }
        write("WARN", throwable, throwable);
    }

    public void error(Object o) {
        if (!isErrorEnabled()) {
            return;
        }
        if (isUsingLog4j()) {
            log4jLogger.error(o);
            return;
        }
        if (isUsingCommonsLogging()) {
            commonsLog.error(o);
            return;
        }
        write("ERROR", o);
    }

    public void error(Object o, Throwable throwable) {
        if (!isErrorEnabled()) {
            return;
        }
        if (isUsingLog4j()) {
            log4jLogger.error(o, throwable);
            return;
        }
        if (isUsingCommonsLogging()) {
            commonsLog.error(o, throwable);
            return;
        }
        write("ERROR", o, throwable);
    }

    public void error(Throwable throwable) {
        if (!isErrorEnabled()) {
            return;
        }
        if (isUsingLog4j()) {
            log4jLogger.error(throwable);
            return;
        }
        if (isUsingCommonsLogging()) {
            commonsLog.error(throwable);
            return;
        }
        write("ERROR", throwable, throwable);
    }

    public void fatal(Object o) {
        if (!isFatalEnabled()) {
            return;
        }
        if (isUsingLog4j()) {
            log4jLogger.fatal(o);
            return;
        }
        if (isUsingCommonsLogging()) {
            commonsLog.fatal(o);
            return;
        }
        write("FATAL", o);
    }

    public void fatal(Object o, Throwable throwable) {
        if (!isFatalEnabled()) {
            return;
        }
        if (isUsingLog4j()) {
            log4jLogger.fatal(o, throwable);
            return;
        }
        if (isUsingCommonsLogging()) {
            commonsLog.fatal(o, throwable);
            return;
        }
        write("FATAL", o, throwable);
    }

    public void fatal(Throwable throwable) {
        if (!isFatalEnabled()) {
            return;
        }
        if (isUsingLog4j()) {
            log4jLogger.fatal(throwable);
            return;
        }
        if (isUsingCommonsLogging()) {
            commonsLog.fatal(throwable);
            return;
        }
        write("FATAL", throwable, throwable);
    }

    /**
     * Will get an instance of log for a given class.
     *
     * @param aClass to log for
     * @return the log instance
     */
    public static Log getLog(Class aClass) {
        return new Log(aClass);
    }

    /**
     * Set the logging level (options are TRACE, DEBUG, INFO, WARN, ERROR, FATAL).
     *
     * @param level the level to log with
     */
    public static void setLevel(String level) {

        usingSystemOut = false;
        usingSystemErr = false;
        if (level != null) {
            if (level.startsWith("SYSOUT:")) {
                usingSystemOut = true;
                level = level.substring("SYSOUT:".length());
            }
            if (level.startsWith("STDOUT:")) {
                usingSystemOut = true;
                level = level.substring("STDOUT:".length());
            }
            if (level.startsWith("STDERR:")) {
                usingSystemErr = true;
                level = level.substring("STDERR:".length());
            }
        }

        // reset all level info
        traceLevelEnabled = false;
        debugLevelEnabled = false;
        infoLevelEnabled = false;
        warnLevelEnabled = false;
        errorLevelEnabled = false;
        fatalLevelEnabled = false;

        // set correct level
        boolean levelSelected = false;
        if ("TRACE".equals(level)) {
            traceLevelEnabled = true;
            levelSelected = true;
        }
        if ("DEBUG".equals(level)) {
            debugLevelEnabled = true;
            levelSelected = true;
        }
        if ("INFO".equals(level)) {
            infoLevelEnabled = true;
            levelSelected = true;
        }
        if ("WARN".equals(level)) {
            warnLevelEnabled = true;
            levelSelected = true;
        }
        if ("ERROR".equals(level)) {
            errorLevelEnabled = true;
            levelSelected = true;
        }
        if ("FATAL".equals(level)) {
            fatalLevelEnabled = true;
            levelSelected = true;
        }
        if (!levelSelected) {
            infoLevelEnabled = true;
        }
    }

    /**
     * Handles writing for throwable.
     *
     * @param level     log level to log for
     * @param throwable to log
     */
    private void write(String level, Object o, Throwable throwable) {
        String msg = getMsg(level, o).toString();
        if (usingSystemOut || context == null) {
            System.out.println(msg);
            throwable.printStackTrace(System.out);
        } else if (usingSystemErr) {
            System.err.println(msg);
            throwable.printStackTrace(System.err);
        } else {
            context.log(msg, throwable);
        }
    }

    /**
     * Handles writing of log lines.
     *
     * @param level log level to log for
     * @param o     object to log (runs toString)
     */
    private void write(String level, Object o) {
        String msg = getMsg(level, o).toString();
        if (usingSystemOut || context == null) {
            System.out.println(msg);
        } else if (usingSystemErr) {
            System.err.println(msg);
        } else {
            context.log(msg);
        }
    }

    private StringBuffer getMsg(String level, Object o) {
        StringBuffer msg = new StringBuffer();
        msg.append(extraInfo());
        if (clazz == null) {
            msg.append("null");
        } else {
            msg.append(clazz.getName());
        }
        msg.append(" ");
        msg.append(level);
        msg.append(": ");
        msg.append(o.toString());
        return msg;
    }

    private String extraInfo() {
        return "";
        /*
        String logLineStr = logCounter++ + "";
        while ( logLineStr.length() < 6 ) logLineStr = "0" + logLineStr;
        return Thread.currentThread().getName() + " " + logLineStr + " ";
        */
    }

    /**
     * Resets log to default state.
     */
    public static void resetAll() {
        Log.context = null;
        setLevel(DEFAULT_LOG_LEVEL);
        Log.usingSystemOut = false;
        Log.usingSystemErr = false;
        Log.usingLog4j = false;
        Log.usingCommonsLogging = false;
    }

    /**
     * Will setup Log based on the filter config.  Uses init paramater "logLevel" to get the log level.
     * Defaults to "INFO".
     *
     * @param filterConfig the filter config to use
     */
    public static void setConfiguration(final FilterConfig filterConfig) {
        resetAll();

        if (filterConfig == null) {
            localLog.error("no filter config passed");
            return;
        }
        Log.context = filterConfig.getServletContext();

        String logLevelConf = filterConfig.getInitParameter("logLevel");
        localLog.error("logLevelConf: " + logLevelConf);

        if (logLevelConf != null) {
            logLevelConf = StringUtils.trim(logLevelConf.toUpperCase());
        }

        if ("LOG4J".equals(logLevelConf)) {
            usingLog4j = true;
        } else if ("COMMONS".equals(logLevelConf)) {
            usingCommonsLogging = true;
        } else {
            setLevel(logLevelConf);
        }

        localLog.debug("logLevel set to " + logLevelConf);
    }

}

⌨️ 快捷键说明

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