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

📄 log.java

📁 通用添加日志源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* ========================================================================
 * JCommon : a free general purpose class library for the Java(tm) platform
 * ========================================================================
 *
 * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
 * 
 * Project Info:  http://www.jfree.org/jcommon/index.html
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 * 
 * --------
 * Log.java
 * --------
 * (C)opyright 2002-2004, by Thomas Morgner and Contributors.
 *
 * Original Author:  Thomas Morgner (taquera@sherito.org);
 * Contributor(s):   David Gilbert (for Object Refinery Limited);
 *
 * $Id: Log.java,v 1.17 2004/06/01 13:38:07 taqua Exp $
 *
 * Changes
 * -------
 * 29-Apr-2003 : Destilled from the JFreeReport project and moved into JCommon
 * 11-Jun-2003 : Removing LogTarget did not work. 
 * 
 */

package com.common.commonLog;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

/**
 * A simple logging facility. Create a class implementing the {@link org.jfree.util.LogTarget}
 * interface to use this feature.
 *
 * @author Thomas Morgner
 */
public class Log {

    /**
     * A simple message class.
     */
    public static class SimpleMessage {

        /**
         * The message.
         */
        private String message;

        /**
         * The parameters.
         */
        private Object[] param;

        /**
         * Creates a new message.
         *
         * @param message the message text.
         * @param param1  parameter 1.
         */
        public SimpleMessage(final String message, final Object param1) {
        	System.out.println("message=" + message);
            this.message = message;
            this.param = new Object[]{param1};
        }

        /**
         * Creates a new message.
         *
         * @param message the message text.
         * @param param1  parameter 1.
         * @param param2  parameter 2.
         */
        public SimpleMessage(final String message, final Object param1,
                             final Object param2) {
            this.message = message;
            this.param = new Object[]{param1, param2};
        }

        /**
         * Creates a new message.
         *
         * @param message the message text.
         * @param param1  parameter 1.
         * @param param2  parameter 2.
         * @param param3  parameter 3.
         */
        public SimpleMessage(final String message, final Object param1,
                             final Object param2, final Object param3) {
            this.message = message;
            this.param = new Object[]{param1, param2, param3};
        }

        /**
         * Creates a new message.
         *
         * @param message the message text.
         * @param param1  parameter 1.
         * @param param2  parameter 2.
         * @param param3  parameter 3.
         * @param param4  parameter 4.
         */
        public SimpleMessage(final String message, final Object param1,
                             final Object param2, final Object param3,
                             final Object param4) {
            this.message = message;
            this.param = new Object[]{param1, param2, param3, param4};
        }

        /**
         * Creates a new message.
         *
         * @param message the message text.
         * @param param   the parameters.
         */
        public SimpleMessage(final String message, final Object[] param) {
            this.message = message;
            this.param = param;
        }

        /**
         * Returns a string representation of the message (useful for debugging).
         *
         * @return the string.
         */
        public String toString() {
            final StringBuffer b = new StringBuffer();
            b.append(this.message);
            if (this.param != null) {
                for (int i = 0; i < this.param.length; i++) {
                    b.append(this.param[i]);
                }
            }
            return b.toString();
        }
    }


    /**
     * The logging threshold.
     */
    private int debuglevel;

    /**
     * Storage for the log targets.
     */
    private LogTarget[] logTargets;

    /** The log contexts. */
    private HashMap logContexts;

    /**
     * the singleton instance of the Log system.
     */
    private static Log singleton;

    /**
     * Creates a new Log instance. The Log is used to manage the log targets.
     */
    protected Log() {
        this.logContexts = new HashMap();
        this.logTargets = new LogTarget[0];
        this.debuglevel = 100;
    }

    /**
     * Returns the singleton Log instance. A new instance is created if necessary.
     *
     * @return the singleton instance.
     */
    public static synchronized Log getInstance() {
        if (singleton == null) {
            singleton = new Log();
        }
        return singleton;
    }

    /**
     * Redefines or clears the currently used log instance.
     *
     * @param log the new log instance or null, to return to the default implementation.
     */
    protected static synchronized void defineLog(final Log log) {
        singleton = log;
    }

    /**
     * Returns the currently defined debug level. The higher the level, the more details
     * are printed.
     *
     * @return the debug level.
     */
    public int getDebuglevel() {
        return this.debuglevel;
    }

    /**
     * Defines the debug level for the log system.
     *
     * @param debuglevel the new debug level
     * @see #getDebuglevel()
     */
    protected void setDebuglevel(final int debuglevel) {
        this.debuglevel = debuglevel;
    }

    /**
     * Adds a log target to this facility. Log targets get informed, via the LogTarget interface,
     * whenever a message is logged with this class.
     *
     * @param target the target.
     */
    public synchronized void addTarget(final LogTarget target) {
        if (target == null) {
        	throw new NullPointerException();
        }
        System.out.println("enter ok");
        final LogTarget[] data = new LogTarget[this.logTargets.length + 1];
        System.arraycopy(this.logTargets, 0, data, 0, this.logTargets.length);
        data[this.logTargets.length] = target;
        this.logTargets = data;
    }

    /**
     * Removes a log target from this facility.
     *
     * @param target the target to remove.
     */
    public synchronized void removeTarget(final LogTarget target) {
        if (target == null) {
            throw new NullPointerException();
        }
        final ArrayList l = new ArrayList();
        l.addAll(Arrays.asList(this.logTargets));
        l.remove(target);

        final LogTarget[] targets = new LogTarget[l.size()];
        this.logTargets = (LogTarget[]) l.toArray(targets);
    }

    /**
     * Returns the registered logtargets.
     *
     * @return the logtargets.
     */
    public LogTarget[] getTargets() {
        final LogTarget[] targets = new LogTarget[this.logTargets.length];
        System.arraycopy(targets, 0, this.logTargets, 0, this.logTargets.length);

⌨️ 快捷键说明

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