From 2efaa43defc14012e24f2a8e6ea24820e7369cd7 Mon Sep 17 00:00:00 2001 From: ribeaudc <ribeaudc> Date: Tue, 29 May 2007 08:07:05 +0000 Subject: [PATCH] add ExtendedLogger to LogFactory. SVN: 220 --- .../cisd/common/logging/ExtendedLogger.java | 73 +++++++++++++++++++ .../cisd/common/logging/LogFactory.java | 34 ++++++++- 2 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 common/source/java/ch/systemsx/cisd/common/logging/ExtendedLogger.java diff --git a/common/source/java/ch/systemsx/cisd/common/logging/ExtendedLogger.java b/common/source/java/ch/systemsx/cisd/common/logging/ExtendedLogger.java new file mode 100644 index 00000000000..9903e5fb57f --- /dev/null +++ b/common/source/java/ch/systemsx/cisd/common/logging/ExtendedLogger.java @@ -0,0 +1,73 @@ +/* + * Copyright 2007 ETH Zuerich, CISD + * + * 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 ch.systemsx.cisd.common.logging; + +import org.apache.log4j.Logger; + +/** + * This <code>Logger</code> extension checks the priority level before making the logging output. + * + * @author Christian Ribeaud + */ +public final class ExtendedLogger extends Logger +{ + + public ExtendedLogger(String name) + { + super(name); + } + + /////////////////////////////////////////////////////// + // Logger + /////////////////////////////////////////////////////// + + @Override + public final void info(Object message) + { + if (isInfoEnabled()) + { + super.info(message); + } + } + + @Override + public final void info(Object message, Throwable t) + { + if (isInfoEnabled()) + { + super.info(message, t); + } + } + + @Override + public final void debug(Object message) + { + if (isDebugEnabled()) + { + super.debug(message); + } + } + + @Override + public final void debug(Object message, Throwable t) + { + if (isDebugEnabled()) + { + super.debug(message, t); + } + } +} diff --git a/common/source/java/ch/systemsx/cisd/common/logging/LogFactory.java b/common/source/java/ch/systemsx/cisd/common/logging/LogFactory.java index 9308871be21..1e357751586 100644 --- a/common/source/java/ch/systemsx/cisd/common/logging/LogFactory.java +++ b/common/source/java/ch/systemsx/cisd/common/logging/LogFactory.java @@ -17,6 +17,7 @@ package ch.systemsx.cisd.common.logging; import org.apache.log4j.Logger; +import org.apache.log4j.spi.LoggerFactory; /** * This class is used to create loggers (using <code>log4j</code>). @@ -25,11 +26,12 @@ import org.apache.log4j.Logger; */ public final class LogFactory { - - private LogFactory() { + + private LogFactory() + { // Can not be instantiated. } - + /** * @return The logger name for the given {@link LogCategory} and {@link Class}. It will contain the name of the * <var>category</var>, followed by the canonical name of <var>clazz</var>. @@ -48,4 +50,30 @@ public final class LogFactory return Logger.getLogger(getLoggerName(category, clazz)); } + /** + * Returns the logger for the given {@link LogCategory} and {@link Class}. The name of the logger will contain the + * name of the <var>category</var>, followed by the canonical name of <var>clazz</var>. + * <p> + * The returned version of <code>Logger</code> checks the priority level before doing the logging output. + * </p> + */ + public final static Logger getExtendedLogger(LogCategory category, Class clazz) { + return Logger.getLogger(getLoggerName(category, clazz), new ExtendedLoggerFactory()); + } + + /////////////////////////////////////////////////////// + // Helper Classes + /////////////////////////////////////////////////////// + + private final static class ExtendedLoggerFactory implements LoggerFactory { + + /////////////////////////////////////////////////////// + // LoggerFactory + /////////////////////////////////////////////////////// + + public Logger makeNewLoggerInstance(String name) + { + return new ExtendedLogger(name); + } + } } -- GitLab