All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.asteriskjava.util.internal.Log4JLogger Maven / Gradle / Ivy

There is a newer version: 3.41.0
Show newest version
/*
 * 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 org.asteriskjava.util.internal;

import java.io.Serializable;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.asteriskjava.util.Log;

/**
 * Implementation of {@link Log} that maps directly to a Log4J
 * Logger.
 * 

* Initial configuration of the corresponding Logger instances should be done in * the usual manner, as outlined in the Log4J documentation. *

* More or less "stolen" from Apache's commons-logging. * * @author Scott Sanders * @author Rod Waldhoff * @author Robert Burrell Donkin * @version $Id$ */ 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; // NOPMD by srt on 7/5/06 11:18 PM /** 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 TRACE priority. * Currently logs to DEBUG level in Log4J. */ public void trace(Object message) { if (IS12) { getLogger().log(FQCN, Level.DEBUG, message, null); } else { getLogger().log(FQCN, Level.DEBUG, message, null); } } /** * Log an error to the Log4j Logger with TRACE priority. * Currently logs to DEBUG level in Log4J. */ public void trace(Object message, Throwable t) { if (IS12) { getLogger().log(FQCN, Level.DEBUG, message, t); } else { getLogger().log(FQCN, Level.DEBUG, message, t); } } /** * Log a message to the Log4j Logger with DEBUG priority. */ public void debug(Object message) { if (IS12) { getLogger().log(FQCN, Level.DEBUG, message, null); } else { getLogger().log(FQCN, Level.DEBUG, message, null); } } /** * Log an error to the Log4j Logger with DEBUG priority. */ public void debug(Object message, Throwable t) { if (IS12) { getLogger().log(FQCN, Level.DEBUG, message, t); } else { getLogger().log(FQCN, Level.DEBUG, message, t); } } /** * Log a message to the Log4j Logger with INFO priority. */ public void info(Object message) { if (IS12) { getLogger().log(FQCN, Level.INFO, message, null); } else { getLogger().log(FQCN, Level.INFO, message, null); } } /** * Log an error to the Log4j Logger with INFO priority. */ public void info(Object message, Throwable t) { if (IS12) { getLogger().log(FQCN, Level.INFO, message, t); } else { getLogger().log(FQCN, Level.INFO, message, t); } } /** * Log a message to the Log4j Logger with WARN priority. */ public void warn(Object message) { if (IS12) { getLogger().log(FQCN, Level.WARN, message, null); } else { getLogger().log(FQCN, Level.WARN, message, null); } } /** * Log an error to the Log4j Logger with WARN priority. */ public void warn(Object message, Throwable t) { if (IS12) { getLogger().log(FQCN, Level.WARN, message, t); } else { getLogger().log(FQCN, Level.WARN, message, t); } } /** * Log a message to the Log4j Logger with ERROR priority. */ public void error(Object message) { if (IS12) { getLogger().log(FQCN, Level.ERROR, message, null); } else { getLogger().log(FQCN, Level.ERROR, message, null); } } /** * Log an error to the Log4j Logger with ERROR priority. */ public void error(Object message, Throwable t) { if (IS12) { getLogger().log(FQCN, Level.ERROR, message, t); } else { getLogger().log(FQCN, Level.ERROR, message, t); } } /** * Log a message to the Log4j Logger with FATAL priority. */ public void fatal(Object message) { if (IS12) { getLogger().log(FQCN, Level.FATAL, message, null); } else { getLogger().log(FQCN, Level.FATAL, message, null); } } /** * Log an error to the Log4j Logger with FATAL priority. */ public void fatal(Object message, Throwable t) { if (IS12) { getLogger().log(FQCN, Level.FATAL, message, t); } else { getLogger().log(FQCN, Level.FATAL, message, t); } } /** * Return the native Logger instance we are using. */ public final Logger getLogger() { if (logger == null) { logger = Logger.getLogger(name); } return this.logger; } /** * Check whether the Log4j Logger used is enabled for DEBUG * priority. */ public boolean isDebugEnabled() { return getLogger().isDebugEnabled(); } /** * Check whether the Log4j Logger used is enabled for ERROR * priority. */ public boolean isErrorEnabled() { if (IS12) { return getLogger().isEnabledFor(Level.ERROR); } return getLogger().isEnabledFor(Level.ERROR); } /** * Check whether the Log4j Logger used is enabled for FATAL * priority. */ public boolean isFatalEnabled() { if (IS12) { return getLogger().isEnabledFor(Level.FATAL); } return getLogger().isEnabledFor(Level.FATAL); } /** * Check whether the Log4j Logger used is enabled for INFO * priority. */ public boolean isInfoEnabled() { return getLogger().isInfoEnabled(); } /** * Check whether the Log4j Logger used is enabled for TRACE * priority. For Log4J, this returns the value of * isDebugEnabled() */ public boolean isTraceEnabled() { return getLogger().isDebugEnabled(); } /** * Check whether the Log4j Logger used is enabled for WARN * priority. */ public boolean isWarnEnabled() { if (IS12) { return getLogger().isEnabledFor(Level.WARN); } return getLogger().isEnabledFor(Level.WARN); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy