From 5b72a5551e12f6be9b1601f3d22f1d3089ebe575 Mon Sep 17 00:00:00 2001 From: Robert Burrell Donkin Date: Thu, 3 Jan 2002 18:52:25 +0000 Subject: [PATCH] Abstract log implementation initial commit git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/logging/trunk@138821 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/commons/logging/AbstractLog.java | 383 ++++++++++++++++++ 1 file changed, 383 insertions(+) create mode 100644 src/java/org/apache/commons/logging/AbstractLog.java diff --git a/src/java/org/apache/commons/logging/AbstractLog.java b/src/java/org/apache/commons/logging/AbstractLog.java new file mode 100644 index 0000000..6ff0f00 --- /dev/null +++ b/src/java/org/apache/commons/logging/AbstractLog.java @@ -0,0 +1,383 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/java/org/apache/commons/logging/Attic/AbstractLog.java,v 1.1 2002/01/03 18:52:25 rdonkin Exp $ + * $Revision: 1.1 $ + * $Date: 2002/01/03 18:52:25 $ + * + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 1999-2001 The Apache Software Foundation. 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 acknowlegement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. + * + * 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 the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + + +package org.apache.commons.logging; + + +/** + *

This is an abstract implementation of the Log interface. + * It provides the following common services for the actual concrete implementations: + * + *

Log Level Property

+ *

Property getter and setter for log levels.

+ * + *

Log Level Enforcement

+ *

The current log level is checked and then the message will be passed onto the + * subclass implementation if the level is currently enabled.

+ * + * @author Robert Burrell Donkin + * @version $Revision: 1.1 $ + */ +public abstract class AbstractLog implements Log { + + // --------------------------------------------------------- Attributes + + /** Default log level is currently OFF */ + private int currentLogLevel = Log.OFF; + + + // --------------------------------------------------------- Properties + + /** + *

Set logging level.

+ * + * @param level new logging level + */ + public void setLevel(int currentLogLevel) { + + this.currentLogLevel = currentLogLevel; + } + + /** + *

Get logging level.

+ */ + public int getLevel() { + + return currentLogLevel; + } + + /** + *

Are debug messages currently enabled?

+ * + *

This allows expensive operations such as String concatination + * to be avoided when the message will be ignored by the logger.

+ * + *

This implementation checks that the log level is debug or lower. + * If it is, then it passes the request onto {@link #isDebugEnabledImpl}. + */ + public final boolean isDebugEnabled() { + + if (isLevelEnabled(Log.DEBUG)) { + return isDebugEnabledImpl(); + } + + return false; + } + + /** + *

Are info messages currently enabled?

+ * + *

This allows expensive operations such as String concatination + * to be avoided when the message will be ignored by the logger.

+ * + *

This implementation checks that the log level is debug or lower. + * If it is, then it passes the request onto {@link #isInfoEnabledImpl}. + */ + public final boolean isInfoEnabled() { + + if (isLevelEnabled(Log.INFO)) { + return isInfoEnabledImpl(); + } + + return false; + } + + + // --------------------------------------------------------- Logging Methods + + /** + *

Log a message with debug log level.

+ * + *

If debug log level is enabled, + * then {@link #debugImpl(Object message)} is called.

+ */ + public final void debug(Object message) { + + if (isLevelEnabled(Log.DEBUG)) { + debugImpl(message); + } + } + + /** + *

Log an error with debug log level.

+ * + *

If debug log level is enabled, + * then {@link #debugImpl(Object message, Throwable t)} is called.

+ */ + public final void debug(Object message, Throwable t) { + + if (isLevelEnabled(Log.DEBUG)) { + debugImpl(message, t); + } + } + + /** + *

Log a message with info log level.

+ * + *

If info log level is enabled, + * then {@link #infoImpl(Object message)} is called.

+ */ + public final void info(Object message) { + + if (isLevelEnabled(Log.INFO)) { + infoImpl(message); + } + } + + /** + *

Log an error with info log level.

+ * + *

If info log level is enabled, + * then {@link #infoImpl(Object message, Throwable t)} is called.

+ */ + public final void info(Object message, Throwable t) { + + if (isLevelEnabled(Log.INFO)) { + infoImpl(message, t); + } + } + + /** + *

Log a message with warn log level.

+ * + *

If warn log level is enabled, + * then {@link #warnImpl(Object message)} is called.

+ */ + public final void warn(Object message) { + + if (isLevelEnabled(Log.WARN)) { + warnImpl(message); + } + } + + /** + *

Log an error with warn log level.

+ * + *

If warn log level is enabled, + * then {@link #warnImpl(Object message, Throwable t)} is called.

+ */ + public final void warn(Object message, Throwable t) { + + if (isLevelEnabled(Log.WARN)) { + warnImpl(message, t); + } + } + + /** + *

Log a message with error log level.

+ * + *

If error log level is enabled, + * then {@link #errorImpl(Object message)} is called.

+ */ + public final void error(Object message) { + + if (isLevelEnabled(Log.ERROR)) { + errorImpl(message); + } + } + + /** + *

Log an error with error log level.

+ * + *

If error log level is enabled, + * then {@link #errorImpl(Object message, Throwable t)} is called.

+ */ + public final void error(Object message, Throwable t) { + + if (isLevelEnabled(Log.ERROR)) { + errorImpl(message, t); + } + } + + /** + *

Log a message with fatal log level.

+ * + *

If fatal log level is enabled, + * then {@link #fatalImpl(Object message)} is called.

+ */ + public final void fatal(Object message) { + + if (isLevelEnabled(Log.FATAL)) { + fatalImpl(message); + } + } + + /** + *

Log an error with fatal log level.

+ * + *

If fatal log level is enabled, + * then {@link #fatalImpl(Object message, Throwable t)} is called.

+ */ + public final void fatal(Object message, Throwable t) { + + if (isLevelEnabled(Log.FATAL)) { + fatalImpl(message, t); + } + } + + + + // --------------------------------------------------------- Decorated Implementation + + /** + *

[OVERRIDE] Log a message with debug log level. + * Subclasses should override this method to implement the logging.

+ */ + protected abstract void debugImpl(Object message); + + /** + *

[OVERRIDE] Log an error with debug log level. + * Subclasses should override this method to implement the logging.

+ */ + protected abstract void debugImpl(Object message, Throwable t); + + + /** + *

[OVERRIDE] Log a message with info log level . + * Subclasses should override this method to implement the logging.

+ */ + protected abstract void infoImpl(Object message); + + /** + *

[OVERRIDE] Log an error with info log level. + * Subclasses should override this method to implement the logging.

+ */ + protected abstract void infoImpl(Object message, Throwable t); + + + /** + *

[OVERRIDE] Log a message with warn log level. + * Subclasses should override this method to implement the logging.

+ */ + protected abstract void warnImpl(Object message); + + /** + *

[OVERRIDE] Log an error with warn log level. + * Subclasses should override this method to implement the logging.

+ */ + protected abstract void warnImpl(Object message, Throwable t); + + + /** + *

[OVERRIDE] Log a message with error log level. + * Subclasses should override this method to implement the logging.

+ */ + protected abstract void errorImpl(Object message); + + /** + *

[OVERRIDE] Log an error with error log level. + * Subclasses should override this method to implement the logging.

+ */ + protected abstract void errorImpl(Object message, Throwable t); + + + /** + *

[OVERRIDE] Log a message with fatal log level. + * Subclasses should override this method to implement the logging.

+ */ + protected abstract void fatalImpl(Object message); + + + /** + *

[OVERRIDE] Log an error with fatal log level. + * Subclasses should override this method to implement the logging.

+ */ + protected abstract void fatalImpl(Object message, Throwable t); + + /** + *

Are debug messages currently enabled?

+ * + *

Subclasses should override this method if their logger provides + * a special implementation.

+ * + * @return true + */ + protected boolean isDebugEnabledImpl() { + return true; + } + + /** + *

Are info messages currently enabled?

+ * + *

Subclasses should override this method if their logger provides + * a special implementation.

+ * + * @return true + */ + protected boolean isInfoEnabledImpl() { + return true; + } + + + + // --------------------------------------------------------- Implementation Methods + + /** + * Is the given log level currently enabled? + * + * @param logLevel is this level enabled? + */ + protected boolean isLevelEnabled(int logLevel) { + // log level are numerically ordered so can use simple numeric comparison + return (logLevel >= currentLogLevel); + } +}