From 23e71e6d58575c86bcbefb0b761f6732bb171153 Mon Sep 17 00:00:00 2001 From: Robert Burrell Donkin Date: Thu, 4 Nov 2004 23:03:59 +0000 Subject: [PATCH] Added new optional subcomponent consisting of non-core implementations. Initial contents MemoryLog, a log implementation intended for use when unit testing. Issue #27663. Contributed by Joerg Schaible. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/logging/trunk@139055 13f79535-47bb-0310-9956-ffa450edef68 --- build.xml | 18 +- optional/.cvsignore | 3 + optional/build.xml | 263 +++++++++++ optional/project.xml | 168 +++++++ optional/src/conf/MANIFEST.MF | 5 + .../commons/logging/impl/MemoryLog.java | 432 ++++++++++++++++++ .../org/apache/commons/logging/TestAll.java | 52 +++ .../commons/logging/impl/MemoryLogTest.java | 84 ++++ src/conf/MANIFEST.MF | 2 +- 9 files changed, 1024 insertions(+), 3 deletions(-) create mode 100644 optional/.cvsignore create mode 100644 optional/build.xml create mode 100644 optional/project.xml create mode 100644 optional/src/conf/MANIFEST.MF create mode 100644 optional/src/java/org/apache/commons/logging/impl/MemoryLog.java create mode 100644 optional/src/test/org/apache/commons/logging/TestAll.java create mode 100644 optional/src/test/org/apache/commons/logging/impl/MemoryLogTest.java diff --git a/build.xml b/build.xml index b98a642..ba7360e 100644 --- a/build.xml +++ b/build.xml @@ -21,7 +21,7 @@ @@ -64,7 +64,7 @@ - + @@ -82,6 +82,11 @@ + + + + + @@ -427,6 +432,15 @@ limitations under the License.-->'> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Preparing build directory... + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/optional/project.xml b/optional/project.xml new file mode 100644 index 0000000..188d58d --- /dev/null +++ b/optional/project.xml @@ -0,0 +1,168 @@ + + + + 3 + + Logging + commons-logging-optional + 1.0.5-dev + 2001 + Commons Logging (Optional Implementations) + + Commons Logging is a thin adapter allowing configurable bridging to other, + well known logging systems. This package contains non-core implementations. + + /images/logo.png + + http://jakarta.apache.org/commons/${pom.artifactId.substring(8)}/ + org.apache.commons.${pom.artifactId.substring(8)} + + + The Apache Software Foundation + http://jakarta.apache.org + http://jakarta.apache.org/images/original-jakarta-logo.gif + + + + + The Apache Software License, Version 2.0 + /LICENSE.txt + repo + + + + jakarta + http://issues.apache.org/bugzilla/ + jakarta.apache.org + /www/jakarta.apache.org/commons/${pom.artifactId.substring(8)}/ + /www/jakarta.apache.org/builds/jakarta-commons/${pom.artifactId.substring(8)}/ + + + + scm:cvs:pserver:anoncvs@cvs.apache.org:/home/cvspublic:jakarta-commons/${pom.artifactId.substring(8)} + http://cvs.apache.org/viewcvs/jakarta-commons/${pom.artifactId.substring(8)}/ + + + + + Commons Dev List + commons-dev-subscribe@jakarta.apache.org + commons-dev-unsubscribe@jakarta.apache.org + http://nagoya.apache.org/eyebrowse/SummarizeList?listName=commons-dev@jakarta.apache.org + + + Commons User List + commons-user-subscribe@jakarta.apache.org + commons-user-unsubscribe@jakarta.apache.org + http://nagoya.apache.org/eyebrowse/SummarizeList?listName=commons-user@jakarta.apache.org + + + + + + Morgan Delagrange + morgand + morgand at apache dot org + Apache + Java Developer + + + Rodney Waldhoff + rwaldhoff + rwaldhoff at apache org + Apache Software Foundation + + + Craig McClanahan + craigmcc + craigmcc at apache org + Apache Software Foundation + + + Scott Sanders + sanders + sanders at apache dot org + Apache Software Foundation + + + Robert Burrell Donkin + rdonkin + rdonkin at apache dot org + Apache Software Foundation + + + Peter Donald + donaldp + donaldp at apache dot org + + + + Costin Manolache + costin + costin at apache dot org + Apache Software Foundation + + + Richard Sitze + rsitze + rsitze at apache dot org + Apache Software Foundation + + + Juozas Baliuka + baliuka + baliuka@apache.org + + + Java Developer + + + + + + + + junit + 3.7 + + + + commons-logging + 1.0.4 + http://jakarta.apache.org/commons/logging.html + + + + + + commons-dev@jakarta.apache.org + src/java + src/test + + + + maven-javadoc-plugin + maven-jdepend-plugin + maven-junit-report-plugin + maven-jxr-plugin + maven-license-plugin + maven-tasklist-plugin + + + diff --git a/optional/src/conf/MANIFEST.MF b/optional/src/conf/MANIFEST.MF new file mode 100644 index 0000000..1c3129d --- /dev/null +++ b/optional/src/conf/MANIFEST.MF @@ -0,0 +1,5 @@ +Extension-Name: org.apache.commons.logging-optional +Specification-Vendor: Apache Software Foundation +Specification-Version: 1.0 +Implementation-Vendor: Apache Software Foundation +Implementation-Version: 1.0.5 diff --git a/optional/src/java/org/apache/commons/logging/impl/MemoryLog.java b/optional/src/java/org/apache/commons/logging/impl/MemoryLog.java new file mode 100644 index 0000000..4c080a5 --- /dev/null +++ b/optional/src/java/org/apache/commons/logging/impl/MemoryLog.java @@ -0,0 +1,432 @@ +/* + * Copyright 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.apache.commons.logging.impl; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.List; + +import org.apache.commons.logging.Log; + +/** + *

Memory implementation of Log that keeps all log message as + * entries in memory. The class is designed to be used in unit tests. + * The default log level is TRACE.

+ *

The code borrows heavily from the SimpleLog class.

+ * @author Jörg Schaible + * @version $Id: MemoryLog.java,v 1.1 2004/11/04 23:01:39 rdonkin Exp $ + */ +public class MemoryLog implements Log { + + // ------------------------------------------------------- Class Attributes + + /** + * A class for a log entry. + */ + public static class Entry { + + private final Date date; + private final String name; + private final int level; + private final Object message; + private final Throwable throwable; + + /** + * Construct a log entry. + * @param name the logger's name + * @param level the log level + * @param message the message to log + * @param t the throwable attending the log + */ + private Entry(String name, int level, Object message, Throwable t) { + this.date = new Date(); + this.name = name; + this.level = level; + this.message = message; + this.throwable = t; + } + + /** + * @return Returns the logging date. + */ + public Date getDate() { + return date; + } + + /** + * @return Returns the logger's name. + */ + public String getLogName() { + return name; + } + + /** + * @return Returns the log message. + */ + public Object getMessage() { + return message; + } + /** + * @return Returns the attendent {@link java.lang.Throwable} of the log or null. + */ + public Throwable getThrowable() { + return throwable; + } + + /** + * @return Returns the log level. + */ + public int getLevel() { + return level; + } + } + + /** The list with all log entries. */ + private static final List logEntries = Collections.synchronizedList(new ArrayList()); + + + // ---------------------------------------------------- Log Level Constants + + /** "Trace" level logging. */ + public static final int LOG_LEVEL_TRACE = 1; + /** "Debug" level logging. */ + public static final int LOG_LEVEL_DEBUG = 2; + /** "Info" level logging. */ + public static final int LOG_LEVEL_INFO = 3; + /** "Warn" level logging. */ + public static final int LOG_LEVEL_WARN = 4; + /** "Error" level logging. */ + public static final int LOG_LEVEL_ERROR = 5; + /** "Fatal" level logging. */ + public static final int LOG_LEVEL_FATAL = 6; + + /** Enable all logging levels */ + public static final int LOG_LEVEL_ALL = (LOG_LEVEL_TRACE - 1); + + /** Enable no logging levels */ + public static final int LOG_LEVEL_OFF = (LOG_LEVEL_FATAL + 1); + + + // ------------------------------------------------------------- Attributes + + /** The name of this simple log instance */ + protected String logName = null; + /** The current log level */ + protected int currentLogLevel; + + + // ------------------------------------------------------------ Constructor + + /** + * Construct a simple log with given name. + * + * @param name log name + */ + public MemoryLog(String name) { + + logName = name; + + // Set initial log level + setLevel(MemoryLog.LOG_LEVEL_TRACE); + } + + + // -------------------------------------------------------- Properties + + /** + *

Set logging level.

+ * + * @param currentLogLevel new logging level + */ + public void setLevel(int currentLogLevel) { + + this.currentLogLevel = currentLogLevel; + + } + + + /** + * @return Returns the logging level. + */ + public int getLevel() { + + return currentLogLevel; + } + + + // -------------------------------------------------------- Logging Methods + + + /** + *

Do the actual logging. + * This method assembles the message + * and then calls write() to cause it to be written.

+ * + * @param type One of the LOG_LEVEL_XXX constants defining the log level + * @param message The message itself (typically a String) + * @param t The exception whose stack trace should be logged + */ + protected void log(int type, Object message, Throwable t) { + + if(isLevelEnabled(type)) { + Entry entry = new Entry(logName, type, message, t); + logEntries.add(entry); + } + } + + + /** + * @param logLevel is this level enabled? + * @return Returns true if the current level is enabled. + */ + protected boolean isLevelEnabled(int logLevel) { + // log level are numerically ordered so can use simple numeric + // comparison + return (logLevel >= currentLogLevel); + } + + + /** + * @return Returns the log entries. + */ + public static List getLogEntries() { + return Collections.unmodifiableList(logEntries); + } + + + /** + * Reset the MemoryLog and clear the log entries. + */ + public static void reset() { + logEntries.clear(); + } + + + // -------------------------------------------------------- Log Implementation + + + /** + *

Log a message with debug log level.

+ */ + public final void debug(Object message) { + + if (isLevelEnabled(MemoryLog.LOG_LEVEL_DEBUG)) { + log(MemoryLog.LOG_LEVEL_DEBUG, message, null); + } + } + + + /** + *

Log an error with debug log level.

+ */ + public final void debug(Object message, Throwable t) { + + if (isLevelEnabled(MemoryLog.LOG_LEVEL_DEBUG)) { + log(MemoryLog.LOG_LEVEL_DEBUG, message, t); + } + } + + + /** + *

Log a message with trace log level.

+ */ + public final void trace(Object message) { + + if (isLevelEnabled(MemoryLog.LOG_LEVEL_TRACE)) { + log(MemoryLog.LOG_LEVEL_TRACE, message, null); + } + } + + + /** + *

Log an error with trace log level.

+ */ + public final void trace(Object message, Throwable t) { + + if (isLevelEnabled(MemoryLog.LOG_LEVEL_TRACE)) { + log(MemoryLog.LOG_LEVEL_TRACE, message, t); + } + } + + + /** + *

Log a message with info log level.

+ */ + public final void info(Object message) { + + if (isLevelEnabled(MemoryLog.LOG_LEVEL_INFO)) { + log(MemoryLog.LOG_LEVEL_INFO,message,null); + } + } + + + /** + *

Log an error with info log level.

+ */ + public final void info(Object message, Throwable t) { + + if (isLevelEnabled(MemoryLog.LOG_LEVEL_INFO)) { + log(MemoryLog.LOG_LEVEL_INFO, message, t); + } + } + + + /** + *

Log a message with warn log level.

+ */ + public final void warn(Object message) { + + if (isLevelEnabled(MemoryLog.LOG_LEVEL_WARN)) { + log(MemoryLog.LOG_LEVEL_WARN, message, null); + } + } + + + /** + *

Log an error with warn log level.

+ */ + public final void warn(Object message, Throwable t) { + + if (isLevelEnabled(MemoryLog.LOG_LEVEL_WARN)) { + log(MemoryLog.LOG_LEVEL_WARN, message, t); + } + } + + + /** + *

Log a message with error log level.

+ */ + public final void error(Object message) { + + if (isLevelEnabled(MemoryLog.LOG_LEVEL_ERROR)) { + log(MemoryLog.LOG_LEVEL_ERROR, message, null); + } + } + + + /** + *

Log an error with error log level.

+ */ + public final void error(Object message, Throwable t) { + + if (isLevelEnabled(MemoryLog.LOG_LEVEL_ERROR)) { + log(MemoryLog.LOG_LEVEL_ERROR, message, t); + } + } + + + /** + *

Log a message with fatal log level.

+ */ + public final void fatal(Object message) { + + if (isLevelEnabled(MemoryLog.LOG_LEVEL_FATAL)) { + log(MemoryLog.LOG_LEVEL_FATAL, message, null); + } + } + + + /** + *

Log an error with fatal log level.

+ */ + public final void fatal(Object message, Throwable t) { + + if (isLevelEnabled(MemoryLog.LOG_LEVEL_FATAL)) { + log(MemoryLog.LOG_LEVEL_FATAL, message, t); + } + } + + + /** + *

Are debug messages currently enabled?

+ * + *

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

+ */ + public final boolean isDebugEnabled() { + + return isLevelEnabled(MemoryLog.LOG_LEVEL_DEBUG); + } + + + /** + *

Are error messages currently enabled?

+ * + *

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

+ */ + public final boolean isErrorEnabled() { + + return isLevelEnabled(MemoryLog.LOG_LEVEL_ERROR); + } + + + /** + *

Are fatal messages currently enabled?

+ * + *

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

+ */ + public final boolean isFatalEnabled() { + + return isLevelEnabled(MemoryLog.LOG_LEVEL_FATAL); + } + + + /** + *

Are info messages currently enabled?

+ * + *

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

+ */ + public final boolean isInfoEnabled() { + + return isLevelEnabled(MemoryLog.LOG_LEVEL_INFO); + } + + + /** + *

Are trace messages currently enabled?

+ * + *

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

+ */ + public final boolean isTraceEnabled() { + + return isLevelEnabled(MemoryLog.LOG_LEVEL_TRACE); + } + + + /** + *

Are warn messages currently enabled?

+ * + *

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

+ */ + public final boolean isWarnEnabled() { + + return isLevelEnabled(MemoryLog.LOG_LEVEL_WARN); + } +} diff --git a/optional/src/test/org/apache/commons/logging/TestAll.java b/optional/src/test/org/apache/commons/logging/TestAll.java new file mode 100644 index 0000000..e0fdcbc --- /dev/null +++ b/optional/src/test/org/apache/commons/logging/TestAll.java @@ -0,0 +1,52 @@ +/* + * Copyright 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.apache.commons.logging; + +import junit.framework.*; +import org.apache.commons.logging.impl.MemoryLogTest; + +/** + *

The build script calls just one TestSuite - this one! + * All tests should be written into separate TestSuite's + * and added to this. Don't clutter this class with implementations.

+ * + * @version $Revision: 1.1 $ + */ +public class TestAll extends TestCase { + + public TestAll(String testName) { + super(testName); + } + + + public static Test suite() { + TestSuite suite = new TestSuite(); + + suite.addTest(MemoryLogTest.suite()); + + return suite; + } + + /** + * This allows the tests to run as a standalone application. + */ + public static void main(String args[]) { + String[] testCaseName = { TestAll.class.getName() }; + junit.textui.TestRunner.main(testCaseName); + } +} diff --git a/optional/src/test/org/apache/commons/logging/impl/MemoryLogTest.java b/optional/src/test/org/apache/commons/logging/impl/MemoryLogTest.java new file mode 100644 index 0000000..42cb50d --- /dev/null +++ b/optional/src/test/org/apache/commons/logging/impl/MemoryLogTest.java @@ -0,0 +1,84 @@ +/* + * 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.apache.commons.logging.impl; + +import java.util.List; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.impl.MemoryLog; + +import junit.framework.*; + +/** + * Test the MemoryLog. + * @author Jörg Schaible + */ +public class MemoryLogTest + extends TestCase { + + public MemoryLogTest(String testName) { + super(testName); + } + + /** + * @see TestCase#setUp() + */ + protected void setUp() throws Exception { + super.setUp(); + MemoryLog.reset(); + } + + public Log getLogObject() + { + return (Log) new MemoryLog(this.getClass().getName()); + } + + public final void testGetLogEntries() + { + MemoryLog log = (MemoryLog)getLogObject(); + log.setLevel(MemoryLog.LOG_LEVEL_DEBUG); + log.trace("trace"); + log.debug("debug"); + log.info("info"); + log.warn("warn"); + log.error("error", new RuntimeException("error")); + log.fatal("fatal", new RuntimeException("fatal")); + List list = MemoryLog.getLogEntries(); + assertEquals(5, list.size()); + assertEquals("debug",((MemoryLog.Entry)list.get(0)).getMessage()); + assertEquals("info",((MemoryLog.Entry)list.get(1)).getMessage()); + assertEquals("warn",((MemoryLog.Entry)list.get(2)).getMessage()); + assertEquals("error",((MemoryLog.Entry)list.get(3)).getMessage()); + assertEquals("error",((MemoryLog.Entry)list.get(3)).getThrowable().getMessage()); + assertEquals("fatal",((MemoryLog.Entry)list.get(4)).getMessage()); + assertEquals("fatal",((MemoryLog.Entry)list.get(4)).getThrowable().getMessage()); + MemoryLog.reset(); + assertEquals(0, MemoryLog.getLogEntries().size()); + } + + public static void main(String[] args) { + junit.textui.TestRunner.run(MemoryLogTest.suite()); + } + + public static Test suite() { + TestSuite suite = new TestSuite(); + + suite.addTestSuite(MemoryLogTest.class); + + return suite; + } +} diff --git a/src/conf/MANIFEST.MF b/src/conf/MANIFEST.MF index 7029182..afc698e 100644 --- a/src/conf/MANIFEST.MF +++ b/src/conf/MANIFEST.MF @@ -2,4 +2,4 @@ Extension-Name: org.apache.commons.logging Specification-Vendor: Apache Software Foundation Specification-Version: 1.0 Implementation-Vendor: Apache Software Foundation -Implementation-Version: 1.0.4 +Implementation-Version: 1.0.5