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

📄 logger.java

📁 java ftp 鞭策帮手 可一啊啊觉得上
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * 
 *  Copyright (C) 2000-2004 Enterprise Distributed Technologies Ltd
 *
 *  www.enterprisedt.com
 *
 *  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
 *
 *  Bug fixes, suggestions and comments should be sent to bruce@enterprisedt.com
 *
 *  Change Log:
 *
 *    $Log: Logger.java,v $
 *    Revision 1.22  2008-01-09 03:58:54  bruceb
 *    include stack trace
 *
 *    Revision 1.21  2007-05-29 03:08:53  bruceb
 *    remove comment conflict
 *
 *    Revision 1.20  2007-05-29 03:08:04  bruceb
 *    add code for enabling logging of thread names
 *
 *    Revision 1.19  2007-05-15 04:31:59  hans
 *    Made sure each Appender only gets added once in addAppender.
 *
 *    Revision 1.18  2007/04/26 04:22:53  hans
 *    Added removeAppender
 *
 *    Revision 1.17  2007/03/27 10:23:08  bruceb
 *    clearAllElements()
 *
 *    Revision 1.16  2007/03/26 05:23:32  bruceb
 *    add clearAppenders
 *
 *    Revision 1.15  2007/02/14 00:56:13  hans
 *    Added getLevel
 *
 *    Revision 1.14  2006/11/14 12:13:24  bruceb
 *    fix bug whereby ALL level was causing exception in log4j
 *
 *    Revision 1.13  2006/10/27 16:30:04  bruceb
 *    fixed javadoc
 *
 *    Revision 1.12  2006/10/12 12:38:58  bruceb
 *    synchronized methods
 *
 *    Revision 1.11  2006/09/08 07:49:35  hans
 *    Fixed error where isEnabledFor didn't work if log4j is being used.
 *    Also added debug methods with MessageFormat-type parameters.
 *
 *    Revision 1.10  2006/05/22 01:53:03  hans
 *    Added method for dumping byte-arrays as hex
 *
 *    Revision 1.9  2006/03/16 21:49:07  hans
 *    Added support for logging of exception causes
 *
 *    Revision 1.8  2005/02/04 12:29:08  bruceb
 *    add exception message to output
 *
 *    Revision 1.7  2004/10/20 21:03:09  bruceb
 *    catch SecurityExceptions
 *
 *    Revision 1.6  2004/09/17 12:27:11  bruceb
 *    1.1 compat
 *
 *    Revision 1.5  2004/08/31 13:54:50  bruceb
 *    remove compile warnings
 *
 *    Revision 1.4  2004/08/16 21:08:08  bruceb
 *    made cvsids public
 *
 *    Revision 1.3  2004/06/25 11:52:26  bruceb
 *    fixed logging bug
 *
 *    Revision 1.2  2004/05/08 21:13:51  bruceb
 *    renamed property
 *
 *    Revision 1.1  2004/05/01 16:55:42  bruceb
 *    first cut
 *
 *
 */
package com.enterprisedt.util.debug;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import java.util.Vector;

import com.enterprisedt.BaseIOException;

/**
 *  Logger class that mimics log4j Logger class. If log4j integration
 *  is desired, the "edtftp.log4j" property should be set to "true" and
 *  log4j classes must be in the classpath
 *
 *  @author      Bruce Blackshaw
 *  @version     $Revision: 1.22 $
 */
public class Logger { 
    
    /**
     *  Revision control id
     */
    public static String cvsId = "@(#)$Id: Logger.java,v 1.22 2008-01-09 03:58:54 bruceb Exp $";
    
    /**
     * Level of all loggers
     */
    private static Level globalLevel;
    
    /**
     * Log thread names of all loggers
     */
    private static boolean logThreadNames = false;
    
    /**
     * Timestamp formatter
     */
    private SimpleDateFormat format = new SimpleDateFormat("d MMM yyyy HH:mm:ss.S");
    
    /**
     * Hash of all loggers that exist
     */
    private static Hashtable loggers = new Hashtable(10);
    
    /**
     * Vector of all appenders
     */
    private static Vector appenders = new Vector(2);
    
    /**
     * Shall we use log4j or not?
     */
    private boolean useLog4j = false;
    
    /**
     * Log this logger's thread name
     */
    private boolean logThreadName = false;
    
    /**
     * Timestamp
     */
    private Date ts = new Date();
    
    /**
     * Class name for this logger
     */
    private String clazz;
    
    /**
     *  Log4j logging methods
     */
    private Method[][] logMethods = null;

    /**
     * Log4j toPriority method
     */
    private Method toLevelMethod = null;
    
    /**
     * Log4j isEnabledFor method
     */
    private Method isEnabledForMethod = null;
    
    /**
     * Logger log4j object
     */
    private Object logger = null;
    
    /**
     * Arg arrays for use in invoke
     */
    private Object[] argsPlain = new Object[1];
    
    /**
     * Arg arrays for use in invoke
     */
    private Object[] argsThrowable = new Object[2];
    
    /**
     * Determine the logging level
     */
    static {
        String level = Level.OFF.toString();
        try {
            System.getProperty("edtftp.log.level", Level.OFF.toString());
        }
        catch (SecurityException ex) {
            System.out.println("Could not read property 'edtftp.log.level' due to security permissions");
        }

        globalLevel = Level.getLevel(level);
    }
    
    /**
     * Constructor
     * 
     * @param clazz     class this logger is for
     * @param uselog4j  true if using log4j
     */
    private Logger(String clazz, boolean uselog4j) {
        this.clazz = clazz;
        this.useLog4j = uselog4j;
        if (uselog4j)
            setupLog4j();
    }
    
    /**
     * Attempt to set up log4j logging. Of course, the classes
     * must be in the classpath
     */
    private synchronized void setupLog4j() {
        logMethods = new Method[Level.LEVEL_COUNT][2];
        try {
            Class log4jLogger = Class.forName("org.apache.log4j.Logger");
            Class log4jLevel = Class.forName("org.apache.log4j.Level");
            Class log4jPriority = Class.forName("org.apache.log4j.Priority");
            
            // get static logger method & use to get our logger
            Class[] args = { String.class };
            Method getLogger = log4jLogger.getMethod("getLogger", args);
            Object[] invokeArgs = {clazz};
            logger = getLogger.invoke(null, invokeArgs);
            
            // get the logger's methods and store them
            Class[] plainArgs = {Object.class};
            Class[] throwableArgs = {Object.class,Throwable.class};
            logMethods[Level.FATAL_INT][0] = log4jLogger.getMethod("fatal", plainArgs);
            logMethods[Level.FATAL_INT][1] = log4jLogger.getMethod("fatal", throwableArgs);
            logMethods[Level.ERROR_INT][0] = log4jLogger.getMethod("error", plainArgs);
            logMethods[Level.ERROR_INT][1] = log4jLogger.getMethod("error", throwableArgs);
            logMethods[Level.WARN_INT][0] = log4jLogger.getMethod("warn", plainArgs);
            logMethods[Level.WARN_INT][1] = log4jLogger.getMethod("warn", throwableArgs);
            logMethods[Level.INFO_INT][0] = log4jLogger.getMethod("info", plainArgs);
            logMethods[Level.INFO_INT][1] = log4jLogger.getMethod("info", throwableArgs);
            logMethods[Level.DEBUG_INT][0] = log4jLogger.getMethod("debug", plainArgs);
            logMethods[Level.DEBUG_INT][1] = log4jLogger.getMethod("debug", throwableArgs);
            
            // get the toLevel and isEnabledFor methods
            Class[] toLevelArgs = {int.class};
            toLevelMethod = log4jLevel.getMethod("toLevel", toLevelArgs);
            Class[] isEnabledForArgs = {log4jPriority};
            isEnabledForMethod = log4jLogger.getMethod("isEnabledFor", isEnabledForArgs);
        } 
        catch (Exception ex) {
            useLog4j = false;
            error("Failed to initialize log4j logging", ex);
        } 
    }
    
    /**
     * Returns the logging level for all loggers.
     * 
     * @return current logging level.
     */
    public static synchronized Level getLevel() {
    		return globalLevel;
    }

    /**
     * Set all loggers to this level
     * 
     * @param level  new level
     */
    public static synchronized void setLevel(Level level) {
        globalLevel = level;
    }
    
    /**
     * Get a logger for the supplied class
     * 
     * @param clazz    full class name
     * @return  logger for class
     */
    public static Logger getLogger(Class clazz) {
        return getLogger(clazz.getName());
    }
           
    /**
     * Get a logger for the supplied class
     * 
     * @param clazz    full class name
     * @return  logger for class
     */
    public static synchronized Logger getLogger(String clazz) {
        Logger logger = (Logger)loggers.get(clazz);
        if (logger == null) {
            boolean useLog4j = false;
            try {
                String log4j = System.getProperty("edtftp.log.log4j");
                if (log4j != null && log4j.equalsIgnoreCase("true")) {
                    useLog4j = true;
                }
            }
            catch (SecurityException ex) {
                System.out.println("Could not read property 'edtftp.log.log4j' due to security permissions");
            }
            logger = new Logger(clazz, useLog4j);
            loggers.put(clazz, logger);
        }
        return logger;
    }
    
    /**
     * Add an appender to our list
     * 
     * @param newAppender
     */
    public static synchronized void addAppender(Appender newAppender) {
    	if (!appenders.contains(newAppender))
    		appenders.addElement(newAppender);
    }
    
    /**
     * Remove an appender to from list
     * 
     * @param appender
     */
    public static synchronized void removeAppender(Appender appender) {
    	appender.close();
        appenders.removeElement(appender);
    }
   
    /**
     * Clear all appenders
     */
    public static synchronized void clearAppenders() {
        appenders.removeAllElements();
    }
    
    /**
     * Close all appenders
     */
    public static synchronized void shutdown() {
        for (int i = 0; i < appenders.size(); i++) {
            Appender a = (Appender)appenders.elementAt(i);
            a.close();
        }        
    }
    
    /**
     * Set global flag for logging thread names as part of the logger names.
     * 
     * @param logThreadNames true if logging thread names, false otherwise
     */
    public static synchronized void logThreadNames(boolean logThreadNames) {
        Logger.logThreadNames = logThreadNames;
    }
    
    /**
     * Set flag for logging thread names as part of the logger names for this instance
     * of the logger.
     * 
     * @param logThreadName true if logging thread names, false otherwise
     */
    public synchronized void logThreadName(boolean logThreadName) {
        this.logThreadName = logThreadName;
    }
    
    /**
     * Log a message 
     * 
     * @param level     log level
     * @param message   message to log
     * @param t         throwable object
     */

⌨️ 快捷键说明

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