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

📄 log4j.java

📁 jxta官方例程
💻 JAVA
字号:
/**  Copyright (c) 2001 Sun Microsystems, Inc.  All rights*  reserved.**  Redistribution and use in source and binary forms, with or without*  modification, are permitted provided that the following conditions*  are met:**  1. Redistributions of source code must retain the above copyright*  notice, this list of conditions and the following disclaimer.**  2. 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.**  3. The end-user documentation included with the redistribution,*  if any, must include the following acknowledgment:*  "This product includes software developed by the*  Sun Microsystems, Inc. for Project JXTA."*  Alternately, this acknowledgment may appear in the software itself,*  if and wherever such third-party acknowledgments normally appear.**  4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"*  must not be used to endorse or promote products derived from this*  software without prior written permission. For written*  permission, please contact Project JXTA at http://www.jxta.org.**  5. Products derived from this software may not be called "JXTA",*  nor may "JXTA" appear in their name, without prior written*  permission of Sun.**  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 APACHE SOFTWARE FOUNDATION OR*  ITS 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.*  ====================================================================**  This software consists of voluntary contributions made by many*  individuals on behalf of Project JXTA.  For more*  information on Project JXTA, please see*  <http://www.jxta.org/>.**  This license is based on the BSD license adopted by the Apache Foundation.**  $Id: Log4J.java,v 1.4 2006/05/17 19:38:28 nano Exp $*/package net.jxta.myjxta.util;import org.apache.log4j.BasicConfigurator;import org.apache.log4j.Level;import org.apache.log4j.Logger;import org.apache.log4j.Priority;import org.apache.log4j.PropertyConfigurator;import org.apache.log4j.helpers.FileWatchdog;import org.apache.log4j.net.SyslogAppender;import org.apache.log4j.xml.DOMConfigurator;/** * * @version $Id: Log4J.java,v 1.4 2006/05/17 19:38:28 nano Exp $ * * @author james todd [gonzo at jxta dot org] * @author mike mcangus [mcangus at jxta dot org] */public class Log4J {    private static Logger LOG = null;    private static boolean alreadyConfigured = false;//    private static final HashMap formatterMap = new HashMap();    /**     * Provides a Log4J {@link org.apache.log4j.Level Level} that always logs unless logging is completely turned off.     */    public static final Level STATUS_LEVEL = new MyLevel(Priority.FATAL_INT + 10, "STATUS", SyslogAppender.LOG_LOCAL0);    private Log4J() {        // private constructor to ensure that this class is not instantiated.    }    /**     * Configures Log4J logging.     * <p>     * Attempts to use ConfigureAndWatch capabilities of the relevant     * {@link org.apache.log4j.spi.Configurator} class.     * <p>     * Looks first for log4j.properties, then for log4j.xml file to define the configuration.     * If neither file is found, then the default configuration from     * {@link org.apache.log4j.BasicConfigurator#configure() BasicConfigurator.configure()}     * is used.  Uses {@link Env#getLog4jConfigFilePath()} to locate the configuration file.     * <p>     * By default the configuration file (assuming one is found) is checked for updates every 60 seconds.     * This can be modified by using the the runtime parameter -Dlog4j.config.watchSeconds=&lt;seconds&gt;.     */    public static void initialize() {        if (alreadyConfigured) {            if (LOG.isEnabledFor(Level.DEBUG)) {                LOG.debug("Log4J has already been configured");            }            return;        }        String log4jConfigWatchSecondsString = System.getProperty("log4j.config.watchSeconds");        if ((log4jConfigWatchSecondsString == null ) || (log4jConfigWatchSecondsString.length() == 0)) {            configureAndWatch(FileWatchdog.DEFAULT_DELAY);   // Use default (60 seconds at the time of this comment)        } else {            try {                long log4jConfigWatchMillisecs = Long.parseLong(log4jConfigWatchSecondsString) * 1000L;                configureAndWatch(log4jConfigWatchMillisecs);            }  catch (NumberFormatException e) { // log4jConfigWatchMillisecs is not a long                configureAndWatch(FileWatchdog.DEFAULT_DELAY);  // Use default (60 seconds at the time of this comment)                LOG.warn("Received invalid log4j.config.watchSeconds from System.getProperty(): \"" +                         log4jConfigWatchSecondsString + "\"", e);                LOG.warn("Using default: " + FileWatchdog.DEFAULT_DELAY + " ms");            }        }        alreadyConfigured = true;        if (LOG.isEnabledFor(Level.DEBUG)) {            LOG.debug("Log4J configureAndWatch setup complete");        }    }//    /**//     * Returns a message to be logged for the default locale.//     *//     * @param messageId Identifies the message in the log4jText bundle.//     * @return the formatted message//     *//     * @deprecated//     *///    public static String formatMessage(String messageId) {//        return formatMessage(messageId, Locale.getDefault(), new Object[]{});//    }//    /**//     * Returns a message to be logged for the supplied locale.//     *//     * @param messageId Identifies the message in the log4jText bundle.//     * @param locale The locale to be used for localized messages.//     * @return the formatted message//     *//     * @deprecated//     *///    public static String formatMessage(String messageId, Locale locale) {//        return formatMessage(messageId, locale, new Object[]{});//    }//    /**//     * Returns a message to be logged for the default locale.//     *//     * @param messageId Identifies the message in the log4jText bundle.//     * @param parms Parameters to be substituted into the message.//     * @return the formatted message//     *//     * @deprecated//     *///    public static String formatMessage(String messageId, Object[] parms) {//        return formatMessage(messageId, Locale.getDefault(), parms);//    }//    /**//     * Returns a message to be logged for the supplied locale.//     *//     * @param messageId Identifies the message in the log4jText bundle.//     * @param locale The locale to be used for localized messages.//     * @param parms Parameters to be substituted into the message.//     * @return the formatted message//     *//     * @deprecated//     *///    public static String formatMessage(String messageId, Locale locale, Object[] parms) {//        ResourceBundle messages = ResourceBundle.getBundle(Constants.LOG4J_BUNDLE,locale);////        String message = messages.getString(messageId);//        if ((parms == null) || (parms.length == 0)) {//            return message;//        } else {//            MessageFormat formatter = (MessageFormat)formatterMap.get(locale);////            if (formatter == null) {//                formatter = new MessageFormat("",locale);//                formatterMap.put(locale, formatter);//            }////            formatter.applyPattern(message);//            return formatter.format(parms);//        }//    }    private static void configureAndWatch(long watchMillisecs) {        String log4jConfigFile = Env.getLog4jConfigFilePath();                if (log4jConfigFile != null) {            if (Env.isLog4jConfigFileXml()) {                DOMConfigurator.configureAndWatch(log4jConfigFile, watchMillisecs);            } else {                PropertyConfigurator.configureAndWatch(log4jConfigFile, watchMillisecs);            }            LOG = Logger.getLogger(Log4J.class);            if (LOG.isEnabledFor(Level.DEBUG)) {                LOG.debug("Log4J configured with config file " + log4jConfigFile);            }        } else {            // Since we aren't going to be able to pull our "real" settings, let's at least make sure we get some output             BasicConfigurator.configure();            LOG = Logger.getLogger(Log4J.class);            // Log an error with detail regarding the problem (and suspected cause)            LOG.error("Unable to find either log4j.xml or log4j.properties files in the classpath");        }    }    private static class MyLevel extends Level {        private MyLevel(int level, String name, int sysLogLevel) {            super(level, name, sysLogLevel);        }    }}

⌨️ 快捷键说明

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