cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vgritse...@apache.org
Subject svn commit: r586236 - in /cocoon/trunk/core/cocoon-util/src: main/java/org/apache/cocoon/util/Deprecation.java main/java/org/apache/cocoon/util/TraxErrorHandler.java test/java/org/apache/cocoon/util/test/DeprecationTestCase.java
Date Fri, 19 Oct 2007 02:42:58 GMT
Author: vgritsenko
Date: Thu Oct 18 19:42:58 2007
New Revision: 586236

URL: http://svn.apache.org/viewvc?rev=586236&view=rev
Log:
switching to new AbstractLogEnabled

Modified:
    cocoon/trunk/core/cocoon-util/src/main/java/org/apache/cocoon/util/Deprecation.java
    cocoon/trunk/core/cocoon-util/src/main/java/org/apache/cocoon/util/TraxErrorHandler.java
    cocoon/trunk/core/cocoon-util/src/test/java/org/apache/cocoon/util/test/DeprecationTestCase.java

Modified: cocoon/trunk/core/cocoon-util/src/main/java/org/apache/cocoon/util/Deprecation.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-util/src/main/java/org/apache/cocoon/util/Deprecation.java?rev=586236&r1=586235&r2=586236&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-util/src/main/java/org/apache/cocoon/util/Deprecation.java (original)
+++ cocoon/trunk/core/cocoon-util/src/main/java/org/apache/cocoon/util/Deprecation.java Thu
Oct 18 19:42:58 2007
@@ -16,24 +16,25 @@
  */
 package org.apache.cocoon.util;
 
-import org.apache.avalon.framework.logger.ConsoleLogger;
-import org.apache.avalon.framework.logger.Logger;
 import org.apache.commons.lang.enums.ValuedEnum;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.impl.SimpleLog;
 
 /**
  * This class provides a special static "deprecation" logger.
  * All deprecated code should use this logger to log messages into the 
  * deprecation logger. This makes it easier for users to find out if they're 
  * using deprecated stuff.
- * <p>
- * Additionally, it is possible to set the forbidden level of deprecation messages (default
+ *
+ * <p>Additionally, it is possible to set the forbidden level of deprecation messages
(default
  * is to forbid ERROR, i.e. allow up to WARN). Messages equal to or above the forbidden level
  * will lead to throwing a {@link DeprecationException}. Setting the forbidden level to
  * FATAL_ERROR allows running legacy applications using deprecated features (tolerant mode),
and
  * setting the forbidden level to DEBUG will run in strict mode, forbidding all deprecations.
- * <p>
- * Note that according to the above, issuing a fatalError log always raises an exception,
and
- * can therefore be used when detecting old features that have been totally removed.
+ *
+ * <p>Note that according to the above, issuing a <code>fatal</code> log
message always raises
+ * an exception, and can therefore be used when detecting old features that have been completely
+ * removed.
  *
  * @version $Id$
  */
@@ -42,15 +43,20 @@
     /**
      * The deprecation logger.
      */
-    public static final Logger logger = new LoggerWrapper(new ConsoleLogger());
-    
+    public static final Log logger = new LoggerWrapper(getLog());
+
+    private static Log getLog() {
+        SimpleLog log = new SimpleLog("cocoon.deprecation");
+        log.setLevel(SimpleLog.LOG_LEVEL_ALL);
+        return log;
+    }
+
     private static final int DEBUG_VALUE = 0;
     private static final int INFO_VALUE = 1;
     private static final int WARN_VALUE = 2;
     private static final int ERROR_VALUE = 3;
-    private static final int FATAL_VALUE = 3;
-    private static final int FATAL_ERROR_VALUE = 4;
-    
+    private static final int FATAL_VALUE = 4;
+
     /**
      * Debug deprecation messages indicate features that are no more considered "current"
      * or "best practice", but for which no removal is currently foreseen.
@@ -78,10 +84,11 @@
     public static final LogLevel ERROR = new LogLevel("ERROR", ERROR_VALUE);
 
     /**
-     * Fatal error deprecation messages indicate features that used to exist but have been
removed
+     * Fatal deprecation messages indicate features that used to exist but have been removed
      * in the current version. Logging such a message always throws a {@link DeprecationException}.
      */
-    public static final LogLevel FATAL_ERROR = new LogLevel("FATAL_ERROR", FATAL_ERROR_VALUE);
+    public static final LogLevel FATAL = new LogLevel("FATAL", FATAL_VALUE);
+
     
     public static final class LogLevel extends ValuedEnum {
         private LogLevel(String text, int value) {
@@ -93,10 +100,11 @@
         }
     }
 
-    public static void setLogger(Logger newLogger) {
+
+    public static void setLogger(Log newLogger) {
         // Note: the "logger" attribute is not of type LoggerWrapper so that it appears
         // as a standard Logger in the javadocs.
-        ((LoggerWrapper)logger).setLogger(newLogger);
+        ((LoggerWrapper) logger).setLogger(newLogger);
     }
     
     public static void setForbiddenLevel(LogLevel level) {
@@ -104,104 +112,132 @@
         if (level == null) {
             level = ERROR;
         }
-        ((LoggerWrapper)logger).setForbiddenLevel(level);
+        ((LoggerWrapper) logger).setForbiddenLevel(level);
     }
-    
+
+
     /**
      * Wraps a logger, and throws an DeprecatedException if the message level is
      * higher than the allowed one.
      */
-    private static class LoggerWrapper implements Logger {
+    private static class LoggerWrapper implements Log {
         
-        private Logger realLogger;
+        private Log delegate;
         // up to warn is allowed
         private int forbiddenLevel = ERROR_VALUE;
+
         
-        public LoggerWrapper(Logger logger) {
-            this.realLogger = logger;
+        private LoggerWrapper(Log logger) {
+            this.delegate = logger;
         }
         
-        public void setLogger(Logger logger) {
+        private void setLogger(Log logger) {
             // Unwrap a wrapped logger
-            while(logger instanceof LoggerWrapper) {
-                logger = ((LoggerWrapper)logger).realLogger;
+            while (logger instanceof LoggerWrapper) {
+                logger = ((LoggerWrapper) logger).delegate;
             }
-            this.realLogger = logger;
+            this.delegate = logger;
         }
         
-        public void setForbiddenLevel(LogLevel level) {
+        private void setForbiddenLevel(LogLevel level) {
             this.forbiddenLevel = level.getValue();
         }
         
-        private void throwException(int level, String message) {
+        private void throwException(int level, Object message) {
             if (level >= this.forbiddenLevel) {
-                throw new DeprecationException(message);
+                throw new DeprecationException(String.valueOf(message));
             }
         }
         
         private boolean isThrowingException(int level) {
             return level >= this.forbiddenLevel;
         }
-        
-        public void debug(String message) {
-            realLogger.debug(message);
+
+
+        public void trace(Object message) {
+            delegate.trace(message);
+            throwException(DEBUG_VALUE, message);
+        }
+
+        public void trace(Object message, Throwable thr) {
+            delegate.trace(message, thr);
             throwException(DEBUG_VALUE, message);
         }
-        public void debug(String message, Throwable thr) {
-            realLogger.debug(message, thr);
+
+        public void debug(Object message) {
+            delegate.debug(message);
             throwException(DEBUG_VALUE, message);
         }
-        public void info(String message) {
-            realLogger.info(message);
+
+        public void debug(Object message, Throwable thr) {
+            delegate.debug(message, thr);
+            throwException(DEBUG_VALUE, message);
+        }
+
+        public void info(Object message) {
+            delegate.info(message);
             throwException(INFO_VALUE, message);
         }
-        public void info(String message, Throwable thr) {
-            realLogger.info(message, thr);
+
+        public void info(Object message, Throwable thr) {
+            delegate.info(message, thr);
             throwException(INFO_VALUE, message);
         }
-        public void warn(String message) {
-            realLogger.warn(message);
+
+        public void warn(Object message) {
+            delegate.warn(message);
             throwException(WARN_VALUE, message);
         }
-        public void warn(String message, Throwable thr) {
-            realLogger.warn(message, thr);
+
+        public void warn(Object message, Throwable thr) {
+            delegate.warn(message, thr);
             throwException(WARN_VALUE, message);
         }
-        public void error(String message) {
-            realLogger.error(message);
+
+        public void error(Object message) {
+            delegate.error(message);
             throwException(ERROR_VALUE, message);
         }
-        public void error(String message, Throwable thr) {
-            realLogger.error(message, thr);
+
+        public void error(Object message, Throwable thr) {
+            delegate.error(message, thr);
             throwException(ERROR_VALUE, message);
         }
-        public void fatalError(String message) {
-            realLogger.fatalError(message);
+
+        public void fatal(Object message) {
+            delegate.fatal(message);
             throwException(FATAL_VALUE, message);
         }
-        public void fatalError(String message, Throwable thr) {
-            realLogger.fatalError(message, thr);
+
+        public void fatal(Object message, Throwable thr) {
+            delegate.fatal(message, thr);
             throwException(FATAL_VALUE, message);
         }
+
+        public boolean isTraceEnabled() {
+            return isThrowingException(DEBUG_VALUE) || delegate.isTraceEnabled();
+        }
+
         public boolean isDebugEnabled() {
             // Enable level also if it is set to throw an exception, so that
             // logging the message occurs, and then throws it.
-            return isThrowingException(DEBUG_VALUE) || realLogger.isDebugEnabled();
+            return isThrowingException(DEBUG_VALUE) || delegate.isDebugEnabled();
         }
+
         public boolean isInfoEnabled() {
-            return isThrowingException(INFO_VALUE) || realLogger.isInfoEnabled();
+            return isThrowingException(INFO_VALUE) || delegate.isInfoEnabled();
         }
+
         public boolean isWarnEnabled() {
-            return isThrowingException(WARN_VALUE) || realLogger.isWarnEnabled();
+            return isThrowingException(WARN_VALUE) || delegate.isWarnEnabled();
         }
+
         public boolean isErrorEnabled() {
-            return isThrowingException(ERROR_VALUE) || realLogger.isErrorEnabled();
+            return isThrowingException(ERROR_VALUE) || delegate.isErrorEnabled();
         }
-        public boolean isFatalErrorEnabled() {
+
+        public boolean isFatalEnabled() {
             return true;
-        }
-        public Logger getChildLogger(String message) {
-            return realLogger.getChildLogger(message);
         }
     }
 }

Modified: cocoon/trunk/core/cocoon-util/src/main/java/org/apache/cocoon/util/TraxErrorHandler.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-util/src/main/java/org/apache/cocoon/util/TraxErrorHandler.java?rev=586236&r1=586235&r2=586236&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-util/src/main/java/org/apache/cocoon/util/TraxErrorHandler.java
(original)
+++ cocoon/trunk/core/cocoon-util/src/main/java/org/apache/cocoon/util/TraxErrorHandler.java
Thu Oct 18 19:42:58 2007
@@ -16,68 +16,79 @@
  */
 package org.apache.cocoon.util;
 
-import org.apache.avalon.framework.logger.Logger;
-
 import javax.xml.transform.ErrorListener;
 import javax.xml.transform.SourceLocator;
 import javax.xml.transform.TransformerException;
 
+import org.apache.avalon.framework.logger.Logger;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.commons.logging.impl.AvalonLogger;
+
 /**
- * This ErrorListener simply logs the exception and in
- * case of an fatal-error the exception is rethrown.
- * Warnings and errors are ignored.
+ * This error handler simply logs all exception and, in case of a fatal error,
+ * the exception is rethrown. Warnings and errors are not re-thrown.
  *
  * @version $Id$
  */
 public class TraxErrorHandler implements ErrorListener {
 
+    private final Log logger;
+    
     private StringBuffer warnings = new StringBuffer("Errors in XSLT transformation:\n");
-    private Logger logger = null;
 
-    public TraxErrorHandler( Logger logger ) {
+
+    public TraxErrorHandler() {
+        this.logger = LogFactory.getLog(getClass());
+    }
+
+    public TraxErrorHandler(Log logger) {
         this.logger = logger;
     }
 
-    public void warning( TransformerException exception )
-            throws TransformerException
-    {
-        final String message = getMessage( exception );
-        if ( this.logger != null ) {
-            this.logger.warn( message );
+    /** @deprecated Use {@link #TraxErrorHandler(Log)}. } */
+    public TraxErrorHandler(Logger logger) {
+        this.logger = new AvalonLogger(logger);
+    }
+
+    public void warning(TransformerException exception)
+    throws TransformerException {
+        final String message = getMessage(exception);
+        if (this.logger != null) {
+            this.logger.warn(message);
         } else {
-            System.out.println( "WARNING: " + message );
+            System.out.println("WARNING: " + message);
         }
         warnings.append("Warning: ");
         warnings.append(message);
         warnings.append("\n");
     }
 
-    public void error( TransformerException exception )
-            throws TransformerException
-    {
-        final String message = getMessage( exception );
-        if ( this.logger != null ) {
-            this.logger.error( message, exception );
+    public void error(TransformerException exception)
+    throws TransformerException {
+        final String message = getMessage(exception);
+        if (this.logger != null) {
+            this.logger.error(message, exception);
         } else {
-            System.out.println( "ERROR: " + message );
+            System.out.println("ERROR: " + message);
         }
         warnings.append("Error: ");
         warnings.append(message);
         warnings.append("\n");
     }
 
-    public void fatalError( TransformerException exception )
-            throws TransformerException
-    {
-        final String message = getMessage( exception );
-        if ( this.logger != null ) {
-            this.logger.fatalError( message, exception );
+    public void fatalError(TransformerException exception)
+    throws TransformerException {
+        final String message = getMessage(exception);
+        if (this.logger != null) {
+            this.logger.fatal(message, exception);
         } else {
-            System.out.println( "FATAL-ERROR: " + message );
+            System.out.println("FATAL-ERROR: " + message);
         }
         warnings.append("Fatal: ");
         warnings.append(message);
         warnings.append("\n");
+
         try {
             throw new TransformerException(warnings.toString());
         } finally {
@@ -85,18 +96,17 @@
         }
     }
 
-    private String getMessage( TransformerException exception ) {
-
+    private String getMessage(TransformerException exception) {
         SourceLocator locator = exception.getLocator();
-        if ( null != locator ) {
-            String id = ( !locator.getPublicId().equals(locator.getPublicId()))
+        if (locator != null) {
+            String id = (!locator.getPublicId().equals(locator.getPublicId()))
                     ? locator.getPublicId()
-                    : ( null != locator.getSystemId() )
+                    : (null != locator.getSystemId())
                     ? locator.getSystemId() : "SystemId Unknown";
             return "File " + id
-                    + "; Line " + locator.getLineNumber()
-                    + "; Column " + locator.getColumnNumber()
-                    + "; " + exception.getMessage();
+                   + "; Line " + locator.getLineNumber()
+                   + "; Column " + locator.getColumnNumber()
+                   + "; " + exception.getMessage();
         }
         return exception.getMessage();
     }

Modified: cocoon/trunk/core/cocoon-util/src/test/java/org/apache/cocoon/util/test/DeprecationTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-util/src/test/java/org/apache/cocoon/util/test/DeprecationTestCase.java?rev=586236&r1=586235&r2=586236&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-util/src/test/java/org/apache/cocoon/util/test/DeprecationTestCase.java
(original)
+++ cocoon/trunk/core/cocoon-util/src/test/java/org/apache/cocoon/util/test/DeprecationTestCase.java
Thu Oct 18 19:42:58 2007
@@ -16,13 +16,14 @@
  */
 package org.apache.cocoon.util.test;
 
-import junit.framework.TestCase;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.impl.SimpleLog;
 
-import org.apache.avalon.framework.logger.ConsoleLogger;
-import org.apache.avalon.framework.logger.Logger;
 import org.apache.cocoon.util.Deprecation;
 import org.apache.cocoon.util.DeprecationException;
 
+import junit.framework.TestCase;
+
 /**
  * Test Cases for the Deprecation class.
  * @see org.apache.cocoon.util.Deprecation
@@ -30,19 +31,22 @@
  * @version $Id$
  */
 public class DeprecationTestCase extends TestCase {
+
+    private Log originalLogger;
+    private SimpleLog consoleLogger;
+
+
     public DeprecationTestCase(String name) {
         super(name);
     }
-    
-    private Logger originalLogger;
-    private Logger consoleLogger;
-    
+
     public void setUp() throws Exception {
         super.setUp();
         originalLogger = Deprecation.logger;
         // Setup a disabled logger: avoids polluting the test output, and also test
         // that isXXXEnabled also matches the forbidden deprecation level
-        consoleLogger = new ConsoleLogger(ConsoleLogger.LEVEL_DISABLED);
+        consoleLogger = new SimpleLog("test");
+        consoleLogger.setLevel(SimpleLog.LOG_LEVEL_OFF);
         Deprecation.setLogger(consoleLogger);
         Deprecation.setForbiddenLevel(Deprecation.ERROR);
     }



Mime
View raw message