/*
* $Header: /home/cvspublic/jakarta-slide/src/wrappers/log4j/Log4jLogger.java,v 1.10.2.1 2004/02/05 16:13:04 mholz Exp $
* $Revision: 1.10.2.1 $
* $Date: 2004/02/05 16:13:04 $
*
* ====================================================================
*
* Copyright 1999-2002 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 log4j;
import org.apache.log4j.Category;
import org.apache.log4j.Priority;
import org.apache.slide.util.logger.Logger;
/**
* Log4j logger implementation.
*
* @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
*/
public class Log4jLogger implements Logger {
// ----------------------------------------------------- Instance Variables
// ----------------------------------------------------- Static Initializer
static {
// BasicConfigurator.configure();
}
// ------------------------------------------------------------- Properties
/**
* Logger level setter.
*
* @param loggerLevel New logger level
*/
public void setLoggerLevel(String channel, int loggerLevel) {
Priority priority = toPriority(loggerLevel);
Category cat = getCategory(channel);
cat.setPriority(priority);
}
/**
* Logger level setter.
*
* @param loggerLevel New logger level
*/
public void setLoggerLevel(int loggerLevel) {
setLoggerLevel(DEFAULT_CHANNEL, loggerLevel);
}
/**
* Logger level getter.
*
* @return int logger level
*/
public int getLoggerLevel(String channel) {
Category cat = getCategory(channel);
return fromPriority(cat.getPriority());
}
/**
* Logger level getter.
*
* @return int logger level
*/
public int getLoggerLevel() {
return getLoggerLevel(DEFAULT_CHANNEL);
}
// --------------------------------------------------------- Logger Methods
/**
* Log an object and an associated throwable thru the specified channel and with the specified level.
*
* @param data object to log
* @param throwable throwable to be logged
* @param channel channel name used for logging
* @param level level used for logging
*/
public void log(Object data, Throwable throwable, String channel, int level) {
Category cat = getCategory(channel);
cat.log(toPriority(level), data, throwable);
}
/**
* Log an object thru the specified channel and with the specified level.
*
* @param data The object to log.
* @param channel The channel name used for logging.
* @param level The level used for logging.
*/
public void log(Object data, String channel, int level) {
Category cat = getCategory(channel);
if (data instanceof Throwable)
cat.log(toPriority(level), data, (Throwable)data);
else
cat.log(toPriority(level), data);
}
/**
* Log an object with the specified level.
*
* @param data The object to log.
* @param level The level used for logging.
*/
public void log(Object data, int level) {
this.log(data, DEFAULT_CHANNEL, level);
}
/**
* Log an object.
*
* @param data The object to log.
*/
public void log(Object data) {
this.log(data, DEFAULT_CHANNEL, Logger.DEBUG);
}
/**
* Check if the channel with the specified level is enabled for logging.
* This implementation ignores the channel specification
*
* @param channel The channel specification
* @param level The level specification
*/
public boolean isEnabled(String channel, int level) {
Category cat = getCategory(channel);
return cat.isEnabledFor(toPriority(level));
}
/**
* Check if the default channel with the specified level is enabled for logging.
*
* @param level The level specification
*/
public boolean isEnabled(int level) {
return isEnabled(DEFAULT_CHANNEL, level);
}
// ------------------------------------------------------ Private/Protected Methods
private Category getCategory(String channel) {
Category cat = Category.exists(channel);
if (cat == null) {
cat = Category.getInstance(channel);
}
return cat;
}
/**
* Convert Slide priority to log4j priority.
*/
protected Priority toPriority(int priority) {
Priority result = null;
switch (priority) {
case EMERGENCY:
result = Priority.FATAL;
break;
case CRITICAL:
result = Priority.FATAL;
break;
case ERROR:
result = Priority.ERROR;
break;
case WARNING:
result = Priority.WARN;
break;
case INFO:
result = Priority.INFO;
break;
case DEBUG:
result = Priority.DEBUG;
break;
}
if (result == null)
result = Priority.toPriority(priority);
return result;
}
/**
* Convert Slide priority to log4j priority.
*/
protected int fromPriority(Priority priority) {
int result = INFO;
if (priority.equals(Priority.FATAL)){
result = EMERGENCY;
}
else if (priority.equals(Priority.ERROR)){
result = ERROR;
}
else if (priority.equals(Priority.WARN)){
result = WARNING;
}
else if (priority.equals(Priority.INFO)){
result = INFO;
}
else if (priority.equals(Priority.DEBUG)){
result = DEBUG;
}
else {
result = INFO;
}
return result;
}
}