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

📄 log.java

📁 通用添加日志源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        return targets;
    }

    /**
     * Replaces all log targets by the given target.
     *
     * @param target the new and only logtarget.
     */
    public synchronized void replaceTargets(final LogTarget target) {
        if (target == null) {
            throw new NullPointerException();
        }
        this.logTargets = new LogTarget[]{target};
    }

    /**
     * A convenience method for logging a 'debug' message.
     *
     * @param message the message.
     */
    public static void debug(final Object message) {
        log(LogTarget.DEBUG, message);
    }

    /**
     * A convenience method for logging a 'debug' message.
     *
     * @param message the message.
     * @param e       the exception.
     */
    public static void debug(final Object message, final Exception e) {
        log(LogTarget.DEBUG, message, e);
    }

    /**
     * A convenience method for logging an 'info' message.
     *
     * @param message the message.
     */
    public static void info(final Object message) {
        log(LogTarget.INFO, message);
    }

    /**
     * A convenience method for logging an 'info' message.
     *
     * @param message the message.
     * @param e       the exception.
     */
    public static void info(final Object message, final Exception e) {
        log(LogTarget.INFO, message, e);
    }

    /**
     * A convenience method for logging a 'warning' message.
     *
     * @param message the message.
     */
    public static void warn(final Object message) {
        log(LogTarget.WARN, message);
    }

    /**
     * A convenience method for logging a 'warning' message.
     *
     * @param message the message.
     * @param e       the exception.
     */
    public static void warn(final Object message, final Exception e) {
        log(LogTarget.WARN, message, e);
    }

    /**
     * A convenience method for logging an 'error' message.
     *
     * @param message the message.
     */
    public static void error(final Object message) {
        log(LogTarget.ERROR, message);
    }

    /**
     * A convenience method for logging an 'error' message.
     *
     * @param message the message.
     * @param e       the exception.
     */
    public static void error(final Object message, final Exception e) {
        log(LogTarget.ERROR, message, e);
    }

    /**
     * Logs a message to the main log stream.  All attached log targets will also
     * receive this message. If the given log-level is higher than the given debug-level
     * in the main config file, no logging will be done.
     *
     * @param level   log level of the message.
     * @param message text to be logged.
     */
    protected void doLog(int level, final Object message) {
        if (level > 3) {
            level = 3;
        }
        if (level <= this.debuglevel) {
            for (int i = 0; i < this.logTargets.length; i++) {
                final LogTarget t = this.logTargets[i];
                t.log(level, message);
            }
        }
    }

    /**
     * Logs a message to the main log stream.  All attached log targets will also
     * receive this message. If the given log-level is higher than the given debug-level
     * in the main config file, no logging will be done.
     *
     * @param level   log level of the message.
     * @param message text to be logged.
     */
    public static void log(final int level, final Object message) {
        getInstance().doLog(level, message);
    }

    /**
     * Logs a message to the main log stream. All attached logTargets will also
     * receive this message. If the given log-level is higher than the given debug-level
     * in the main config file, no logging will be done.
     * <p/>
     * The exception's stacktrace will be appended to the log-stream
     *
     * @param level   log level of the message.
     * @param message text to be logged.
     * @param e       the exception, which should be logged.
     */
    public static void log(final int level, final Object message, final Exception e) {
        getInstance().doLog(level, message, e);
    }

    /**
     * Logs a message to the main log stream. All attached logTargets will also
     * receive this message. If the given log-level is higher than the given debug-level
     * in the main config file, no logging will be done.
     * <p/>
     * The exception's stacktrace will be appended to the log-stream
     *
     * @param level   log level of the message.
     * @param message text to be logged.
     * @param e       the exception, which should be logged.
     */
    protected void doLog(int level, final Object message, final Exception e) {
        if (level > 3) {
            level = 3;
        }

        if (level <= this.debuglevel) {
            for (int i = 0; i < this.logTargets.length; i++) {
                final LogTarget t = this.logTargets[i];
                t.log(level, message, e);
            }
        }
    }

    /**
     * Initializes the logging system. Implementors should
     * override this method to supply their own log configuration.
     */
    protected void init() {
        // this method is intentionally empty.
    }

    /**
     * Returns true, if the log level allows debug messages to be
     * printed.
     *
     * @return true, if messages with an log level of DEBUG are allowed.
     */
    public static boolean isDebugEnabled() {
        return getInstance().getDebuglevel() <= LogTarget.DEBUG;
    }

    /**
     * Returns true, if the log level allows informational
     * messages to be printed.
     *
     * @return true, if messages with an log level of INFO are allowed.
     */
    public static boolean isInfoEnabled() {
        return getInstance().getDebuglevel() <= LogTarget.INFO;
    }

    /**
     * Returns true, if the log level allows warning messages to be
     * printed.
     *
     * @return true, if messages with an log level of WARN are allowed.
     */
    public static boolean isWarningEnabled() {
        return getInstance().getDebuglevel() <= LogTarget.WARN;
    }

    /**
     * Returns true, if the log level allows error messages to be
     * printed.
     *
     * @return true, if messages with an log level of ERROR are allowed.
     */
    public static boolean isErrorEnabled() {
        return getInstance().getDebuglevel() <= LogTarget.ERROR;
    }

    /**
     * Creates a log context.
     * 
     * @param context  the class (<code>null</code> not permitted).
     * 
     * @return A log context.
     */
    public static LogContext createContext(final Class context) {
        return createContext(context.getName());
    }

    /**
     * Creates a log context.
     * 
     * @param context  the label for the context.
     * 
     * @return A log context.
     */
    public static LogContext createContext(final String context) {
        return getInstance().internalCreateContext(context);
    }

    /**
     * Creates a log context.
     * 
     * @param context  the name of the logging context (a common prefix).
     * 
     * @return A log context.
     */
    protected LogContext internalCreateContext(final String context) {
        synchronized (this.logContexts) {
            LogContext ctx = (LogContext) this.logContexts.get(context);
            if (ctx == null) {
                ctx = new LogContext(context);
                this.logContexts.put(context, ctx);
            }
            return ctx;
        }
    }
    
    public static void main(final String[] args) {
       System.out.println("1234");
       Log.log(2, "阿斯顿飞");
       Log.getInstance().addTarget(new PrintStreamLogTarget());
       //final TimeSeriesDemo demo = new TimeSeriesDemo("Time Series Demo 1");
       //demo.pack();
       //RefineryUtilities.centerFrameOnScreen(demo);
       //demo.setVisible(true);
       System.out.println("5678");
    }
    
}

⌨️ 快捷键说明

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