[Asterisk-java-cvs] CVS: asterisk-java/src/java/net/sf/asterisk/util/impl JavaLoggingLog.java,NONE,1
Brought to you by:
srt
From: Stefan R. <sr...@us...> - 2005-04-20 18:22:22
|
Update of /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/util/impl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30193/src/java/net/sf/asterisk/util/impl Added Files: JavaLoggingLog.java NullLog.java Log4JLogger.java package.html Log Message: Changed logging to use either log4j, java.util.logging disable logging depending on the classes available at runtime. --- NEW FILE: JavaLoggingLog.java --- /* * JavaLoggingLog.java * * Created on April 14, 2005, 4:28 PM */ package net.sf.asterisk.util.impl; import java.util.logging.Level; import java.util.logging.Logger; import net.sf.asterisk.util.Log; /** * Log implementation that uses the java.util.logging package. * * @author drach */ public class JavaLoggingLog implements Log { /** * The underlying commons-logging Log object to use. */ private Logger log; /** * Creates a new JavaLoggingLog obtained from java.util.logging for the * given class. * * @param clazz the class to log for. */ public JavaLoggingLog(Class clazz) { log = Logger.getLogger(clazz.getName()); } public void debug(Object obj) { log.fine(obj.toString()); } public void info(Object obj) { log.info(obj.toString()); } public void warn(Object obj) { log.warning(obj.toString()); } public void warn(Object obj, Throwable ex) { log.log(Level.WARNING, obj.toString(), ex); } public void error(Object obj) { log.severe(obj.toString()); } public void error(Object obj, Throwable ex) { log.log(Level.SEVERE, obj.toString(), ex); } } --- NEW FILE: NullLog.java --- /* * Copyright 2004-2005 Stefan Reuter * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package net.sf.asterisk.util.impl; import net.sf.asterisk.util.Log; /** * A Log implementation that does nothing. * * @author srt * @version $Id: NullLog.java,v 1.1 2005/04/20 18:22:13 srt Exp $ */ public class NullLog implements Log { /** * Creates a new NullLog. */ public NullLog() { } public void debug(Object obj) { } public void info(Object obj) { } public void warn(Object obj) { } public void warn(Object obj, Throwable ex) { } public void error(Object obj) { } public void error(Object obj, Throwable ex) { } } --- NEW FILE: Log4JLogger.java --- /* * Copyright 2001-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.asterisk.util.impl; import java.io.Serializable; import net.sf.asterisk.util.Log; import org.apache.log4j.Logger; import org.apache.log4j.Priority; import org.apache.log4j.Level; /** * <p> * Implementation of {@link Log} that maps directly to a Log4J <strong>Logger</strong>. * Initial configuration of the corresponding Logger instances should be done in * the usual manner, as outlined in the Log4J documentation. * </p> * * @author <a href="mailto:sa...@ap...">Scott Sanders</a> * @author Rod Waldhoff * @author Robert Burrell Donkin * @version $Id: Log4JLogger.java,v 1.1 2005/04/20 18:22:13 srt Exp $ */ public class Log4JLogger implements Log, Serializable { // ------------------------------------------------------------- Attributes /** * The serial version identifier. */ private static final long serialVersionUID = 3545240215095883829L; /** The fully qualified name of the Log4JLogger class. */ private static final String FQCN = Log4JLogger.class.getName(); private static final boolean is12 = Priority.class .isAssignableFrom(Level.class); /** Log to this logger */ private transient Logger logger = null; /** Logger name */ private String name = null; // ------------------------------------------------------------ Constructor public Log4JLogger() { } /** * Base constructor. */ public Log4JLogger(Class clazz) { this.name = clazz.getName(); this.logger = getLogger(); } // --------------------------------------------------------- Implementation /** * Log a message to the Log4j Logger with <code>TRACE</code> priority. * Currently logs to <code>DEBUG</code> level in Log4J. */ public void trace(Object message) { if (is12) { getLogger().log(FQCN, (Priority) Level.DEBUG, message, null); } else { getLogger().log(FQCN, Level.DEBUG, message, null); } } /** * Log an error to the Log4j Logger with <code>TRACE</code> priority. * Currently logs to <code>DEBUG</code> level in Log4J. */ public void trace(Object message, Throwable t) { if (is12) { getLogger().log(FQCN, (Priority) Level.DEBUG, message, t); } else { getLogger().log(FQCN, Level.DEBUG, message, t); } } /** * Log a message to the Log4j Logger with <code>DEBUG</code> priority. */ public void debug(Object message) { if (is12) { getLogger().log(FQCN, (Priority) Level.DEBUG, message, null); } else { getLogger().log(FQCN, Level.DEBUG, message, null); } } /** * Log an error to the Log4j Logger with <code>DEBUG</code> priority. */ public void debug(Object message, Throwable t) { if (is12) { getLogger().log(FQCN, (Priority) Level.DEBUG, message, t); } else { getLogger().log(FQCN, Level.DEBUG, message, t); } } /** * Log a message to the Log4j Logger with <code>INFO</code> priority. */ public void info(Object message) { if (is12) { getLogger().log(FQCN, (Priority) Level.INFO, message, null); } else { getLogger().log(FQCN, Level.INFO, message, null); } } /** * Log an error to the Log4j Logger with <code>INFO</code> priority. */ public void info(Object message, Throwable t) { if (is12) { getLogger().log(FQCN, (Priority) Level.INFO, message, t); } else { getLogger().log(FQCN, Level.INFO, message, t); } } /** * Log a message to the Log4j Logger with <code>WARN</code> priority. */ public void warn(Object message) { if (is12) { getLogger().log(FQCN, (Priority) Level.WARN, message, null); } else { getLogger().log(FQCN, Level.WARN, message, null); } } /** * Log an error to the Log4j Logger with <code>WARN</code> priority. */ public void warn(Object message, Throwable t) { if (is12) { getLogger().log(FQCN, (Priority) Level.WARN, message, t); } else { getLogger().log(FQCN, Level.WARN, message, t); } } /** * Log a message to the Log4j Logger with <code>ERROR</code> priority. */ public void error(Object message) { if (is12) { getLogger().log(FQCN, (Priority) Level.ERROR, message, null); } else { getLogger().log(FQCN, Level.ERROR, message, null); } } /** * Log an error to the Log4j Logger with <code>ERROR</code> priority. */ public void error(Object message, Throwable t) { if (is12) { getLogger().log(FQCN, (Priority) Level.ERROR, message, t); } else { getLogger().log(FQCN, Level.ERROR, message, t); } } /** * Log a message to the Log4j Logger with <code>FATAL</code> priority. */ public void fatal(Object message) { if (is12) { getLogger().log(FQCN, (Priority) Level.FATAL, message, null); } else { getLogger().log(FQCN, Level.FATAL, message, null); } } /** * Log an error to the Log4j Logger with <code>FATAL</code> priority. */ public void fatal(Object message, Throwable t) { if (is12) { getLogger().log(FQCN, (Priority) Level.FATAL, message, t); } else { getLogger().log(FQCN, Level.FATAL, message, t); } } /** * Return the native Logger instance we are using. */ public Logger getLogger() { if (logger == null) { logger = Logger.getLogger(name); } return (this.logger); } /** * Check whether the Log4j Logger used is enabled for <code>DEBUG</code> * priority. */ public boolean isDebugEnabled() { return getLogger().isDebugEnabled(); } /** * Check whether the Log4j Logger used is enabled for <code>ERROR</code> * priority. */ public boolean isErrorEnabled() { if (is12) { return getLogger().isEnabledFor((Priority) Level.ERROR); } else { return getLogger().isEnabledFor(Level.ERROR); } } /** * Check whether the Log4j Logger used is enabled for <code>FATAL</code> * priority. */ public boolean isFatalEnabled() { if (is12) { return getLogger().isEnabledFor((Priority) Level.FATAL); } else { return getLogger().isEnabledFor(Level.FATAL); } } /** * Check whether the Log4j Logger used is enabled for <code>INFO</code> * priority. */ public boolean isInfoEnabled() { return getLogger().isInfoEnabled(); } /** * Check whether the Log4j Logger used is enabled for <code>TRACE</code> * priority. For Log4J, this returns the value of * <code>isDebugEnabled()</code> */ public boolean isTraceEnabled() { return getLogger().isDebugEnabled(); } /** * Check whether the Log4j Logger used is enabled for <code>WARN</code> * priority. */ public boolean isWarnEnabled() { if (is12) { return getLogger().isEnabledFor((Priority) Level.WARN); } else { return getLogger().isEnabledFor(Level.WARN); } } } --- NEW FILE: package.html --- <html> <head> <!-- /* * Copyright 2004-2005 Stefan Reuter * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ --> </head> <body> <p>Provides private implementations for interfaces defined in the net.sf.asterisk.util package.</p> </body> </html> |