Coverage Report - org.apache.camel.processor.LoggingErrorHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
LoggingErrorHandler
15% 
0% 
0
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.processor;
 19  
 
 20  
 import org.apache.camel.Exchange;
 21  
 import org.apache.camel.Processor;
 22  
 import org.apache.camel.impl.ServiceSupport;
 23  
 import org.apache.camel.util.ServiceHelper;
 24  
 import org.apache.commons.logging.Log;
 25  
 import org.apache.commons.logging.LogFactory;
 26  
 
 27  
 /**
 28  
  * An {@link ErrorHandler} which uses commons-logging to dump the error
 29  
  *
 30  
  * @version $Revision: 534145 $
 31  
  */
 32  
 public class LoggingErrorHandler extends ServiceSupport implements ErrorHandler {
 33  
     private Processor output;
 34  
     private Log log;
 35  
     private LoggingLevel level;
 36  
 
 37  
     public LoggingErrorHandler(Processor output) {
 38  0
         this(output, LogFactory.getLog(LoggingErrorHandler.class), LoggingLevel.INFO);
 39  0
     }
 40  
 
 41  1
     public LoggingErrorHandler(Processor output, Log log, LoggingLevel level) {
 42  1
         this.output = output;
 43  1
         this.log = log;
 44  1
         this.level = level;
 45  1
     }
 46  
 
 47  
     @Override
 48  
     public String toString() {
 49  2
         return "LoggingErrorHandler[" + output + "]";
 50  
     }
 51  
 
 52  
     public void process(Exchange exchange) throws Exception {
 53  
         try {
 54  0
             output.process(exchange);
 55  
         }
 56  0
         catch (RuntimeException e) {
 57  0
             logError(exchange, e);
 58  0
         }
 59  0
     }
 60  
 
 61  
     // Properties
 62  
     //-------------------------------------------------------------------------
 63  
 
 64  
     /**
 65  
      * Returns the output processor
 66  
      */
 67  
     public Processor getOutput() {
 68  1
         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, RuntimeException 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), e);
 123  
         }
 124  0
     }
 125  
 
 126  
     protected Object logMessage(Exchange exchange, RuntimeException e) {
 127  0
         return e + " while processing exchange: " + exchange;
 128  
     }
 129  
 
 130  
     protected void doStart() throws Exception {
 131  0
         ServiceHelper.startServices(output);
 132  0
     }
 133  
 
 134  
     protected void doStop() throws Exception {
 135  0
         ServiceHelper.stopServices(output);
 136  0
     }
 137  
 }