Coverage Report - org.apache.camel.processor.LoggingErrorHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
LoggingErrorHandler
15% 
0% 
0
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.processor;
 18  
 
 19  
 import org.apache.camel.Exchange;
 20  
 import org.apache.camel.Processor;
 21  
 import org.apache.camel.impl.ServiceSupport;
 22  
 import org.apache.camel.util.ServiceHelper;
 23  
 import org.apache.commons.logging.Log;
 24  
 import org.apache.commons.logging.LogFactory;
 25  
 
 26  
 /**
 27  
  * An {@link ErrorHandler} which uses commons-logging to dump the error
 28  
  * 
 29  
  * @version $Revision: 564295 $
 30  
  */
 31  
 public class LoggingErrorHandler extends ErrorHandlerSupport {
 32  
     private Processor output;
 33  
     private Log log;
 34  
     private LoggingLevel level;
 35  
 
 36  
     public LoggingErrorHandler(Processor output) {
 37  0
         this(output, LogFactory.getLog(LoggingErrorHandler.class), LoggingLevel.INFO);
 38  0
     }
 39  
 
 40  3
     public LoggingErrorHandler(Processor output, Log log, LoggingLevel level) {
 41  3
         this.output = output;
 42  3
         this.log = log;
 43  3
         this.level = level;
 44  3
     }
 45  
 
 46  
     @Override
 47  
     public String toString() {
 48  6
         return "LoggingErrorHandler[" + output + "]";
 49  
     }
 50  
 
 51  
     public void process(Exchange exchange) throws Exception {
 52  
         try {
 53  0
             output.process(exchange);
 54  0
         } catch (Throwable e) {
 55  0
             if (!customProcessorForException(exchange, e)) {
 56  0
                 logError(exchange, e);
 57  
             }
 58  0
         }
 59  0
     }
 60  
 
 61  
     // Properties
 62  
     // -------------------------------------------------------------------------
 63  
 
 64  
     /**
 65  
      * Returns the output processor
 66  
      */
 67  
     public Processor getOutput() {
 68  3
         return output;
 69  
     }
 70  
 
 71  
     public LoggingLevel getLevel() {
 72  0
         return level;
 73  
     }
 74  
 
 75  
     public void setLevel(LoggingLevel level) {
 76  0
         this.level = level;
 77  0
     }
 78  
 
 79  
     public Log getLog() {
 80  0
         return log;
 81  
     }
 82  
 
 83  
     public void setLog(Log log) {
 84  0
         this.log = log;
 85  0
     }
 86  
 
 87  
     // Implementation methods
 88  
     // -------------------------------------------------------------------------
 89  
     protected void logError(Exchange exchange, Throwable e) {
 90  0
         switch (level) {
 91  
         case DEBUG:
 92  0
             if (log.isDebugEnabled()) {
 93  0
                 log.debug(logMessage(exchange, e), e);
 94  0
             }
 95  
             break;
 96  
         case ERROR:
 97  0
             if (log.isErrorEnabled()) {
 98  0
                 log.error(logMessage(exchange, e), e);
 99  0
             }
 100  
             break;
 101  
         case FATAL:
 102  0
             if (log.isFatalEnabled()) {
 103  0
                 log.fatal(logMessage(exchange, e), e);
 104  0
             }
 105  
             break;
 106  
         case INFO:
 107  0
             if (log.isInfoEnabled()) {
 108  0
                 log.debug(logMessage(exchange, e), e);
 109  0
             }
 110  
             break;
 111  
         case TRACE:
 112  0
             if (log.isTraceEnabled()) {
 113  0
                 log.trace(logMessage(exchange, e), e);
 114  0
             }
 115  
             break;
 116  
         case WARN:
 117  0
             if (log.isWarnEnabled()) {
 118  0
                 log.warn(logMessage(exchange, e), e);
 119  0
             }
 120  
             break;
 121  
         default:
 122  0
             log.error("Unknown level: " + level + " when trying to log exchange: " + logMessage(exchange, e),
 123  
                       e);
 124  
         }
 125  0
     }
 126  
 
 127  
     protected Object logMessage(Exchange exchange, Throwable e) {
 128  0
         return e + " while processing exchange: " + exchange;
 129  
     }
 130  
 
 131  
     protected void doStart() throws Exception {
 132  0
         ServiceHelper.startServices(output);
 133  0
     }
 134  
 
 135  
     protected void doStop() throws Exception {
 136  0
         ServiceHelper.stopServices(output);
 137  0
     }
 138  
 }