From a1718ca9ac52cd1b80c238faf229141608500d91 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Fri, 1 Feb 2008 12:29:38 +0000 Subject: [PATCH] Improvements to how SystemOutLogger and CommonsLogger log messages with exceptions, and avoid an infinite loop with certain log messages with exceptions - triggered by bug #44326 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@617487 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/changes.xml | 1 + src/documentation/content/xdocs/status.xml | 1 + .../org/apache/poi/util/CommonsLogger.java | 75 ++++++++++++++++++- src/java/org/apache/poi/util/POILogger.java | 25 +++++-- .../org/apache/poi/util/SystemOutLogger.java | 18 ++++- 5 files changed, 111 insertions(+), 9 deletions(-) diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index da8fe3be4c..8f5a097be1 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -36,6 +36,7 @@ + 44326 - Improvements to how SystemOutLogger and CommonsLogger log messages with exceptions, and avoid an infinite loop with certain log messages with exceptions Support for a completed Record based "pull" stream, via org.apache.poi.hssf.eventusermodel.HSSFRecordStream, to complement the existing "push" Event User Model listener stuff diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 65f80fa562..d282a68116 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -33,6 +33,7 @@ + 44326 - Improvements to how SystemOutLogger and CommonsLogger log messages with exceptions, and avoid an infinite loop with certain log messages with exceptions Support for a completed Record based "pull" stream, via org.apache.poi.hssf.eventusermodel.HSSFRecordStream, to complement the existing "push" Event User Model listener stuff diff --git a/src/java/org/apache/poi/util/CommonsLogger.java b/src/java/org/apache/poi/util/CommonsLogger.java index 4265a88af8..16a48f28f6 100644 --- a/src/java/org/apache/poi/util/CommonsLogger.java +++ b/src/java/org/apache/poi/util/CommonsLogger.java @@ -22,8 +22,6 @@ package org.apache.poi.util; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import java.util.*; - /** * A logger class that strives to make it as easy as possible for * developers to write log calls, while simultaneously making those @@ -53,7 +51,6 @@ public class CommonsLogger extends POILogger * @param level One of DEBUG, INFO, WARN, ERROR, FATAL * @param obj1 The object to log. */ - public void log(final int level, final Object obj1) { if(level==FATAL) @@ -98,6 +95,78 @@ public class CommonsLogger extends POILogger log.trace(obj1); } } + } + + /** + * Log a message + * + * @param level One of DEBUG, INFO, WARN, ERROR, FATAL + * @param obj1 The object to log. This is converted to a string. + * @param exception An exception to be logged + */ + public void log(final int level, final Object obj1, + final Throwable exception) + { + if(level==FATAL) + { + if(log.isFatalEnabled()) + { + if(obj1 != null) + log.fatal(obj1, exception); + else + log.fatal(exception); + } + } + else if(level==ERROR) + { + if(log.isErrorEnabled()) + { + if(obj1 != null) + log.error(obj1, exception); + else + log.error(exception); + } + } + else if(level==WARN) + { + if(log.isWarnEnabled()) + { + if(obj1 != null) + log.warn(obj1, exception); + else + log.warn(exception); + } + } + else if(level==INFO) + { + if(log.isInfoEnabled()) + { + if(obj1 != null) + log.info(obj1, exception); + else + log.info(exception); + } + } + else if(level==DEBUG) + { + if(log.isDebugEnabled()) + { + if(obj1 != null) + log.debug(obj1, exception); + else + log.debug(exception); + } + } + else + { + if(log.isTraceEnabled()) + { + if(obj1 != null) + log.trace(obj1, exception); + else + log.trace(exception); + } + } } diff --git a/src/java/org/apache/poi/util/POILogger.java b/src/java/org/apache/poi/util/POILogger.java index b2d358d4d8..514edf90ca 100644 --- a/src/java/org/apache/poi/util/POILogger.java +++ b/src/java/org/apache/poi/util/POILogger.java @@ -51,7 +51,24 @@ public abstract class POILogger abstract public void initialize(final String cat); + /** + * Log a message + * + * @param level One of DEBUG, INFO, WARN, ERROR, FATAL + * @param obj1 The object to log. This is converted to a string. + */ abstract public void log(final int level, final Object obj1); + + /** + * Log a message + * + * @param level One of DEBUG, INFO, WARN, ERROR, FATAL + * @param obj1 The object to log. This is converted to a string. + * @param exception An exception to be logged + */ + abstract public void log(final int level, final Object obj1, + final Throwable exception); + /** * Check if a logger is enabled to log at the specified level @@ -237,17 +254,15 @@ public abstract class POILogger } /** - * Log a message + * Log an exception, without a message * * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 The object to log. This is converted to a string. * @param exception An exception to be logged */ - public void log(final int level, final Object obj1, - final Throwable exception) + public void log(final int level, final Throwable exception) { - log(level , obj1, exception); + log(level, null, exception); } /** diff --git a/src/java/org/apache/poi/util/SystemOutLogger.java b/src/java/org/apache/poi/util/SystemOutLogger.java index 8b3dc50d9a..af678e186a 100644 --- a/src/java/org/apache/poi/util/SystemOutLogger.java +++ b/src/java/org/apache/poi/util/SystemOutLogger.java @@ -49,8 +49,24 @@ public class SystemOutLogger extends POILogger public void log(final int level, final Object obj1) { - if (check(level)) + log(level, obj1, null); + } + + /** + * Log a message + * + * @param level One of DEBUG, INFO, WARN, ERROR, FATAL + * @param obj1 The object to log. This is converted to a string. + * @param exception An exception to be logged + */ + public void log(final int level, final Object obj1, + final Throwable exception) { + if (check(level)) { System.out.println("["+cat+"] "+obj1); + if(exception != null) { + exception.printStackTrace(System.out); + } + } } /** -- 2.39.5