001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.processor;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.Processor;
021    import org.apache.camel.util.ServiceHelper;
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    
025    /**
026     * An {@link ErrorHandler} which uses commons-logging to dump the error
027     * 
028     * @version $Revision: 659842 $
029     */
030    public class LoggingErrorHandler extends ErrorHandlerSupport {
031        private Processor output;
032        private Log log;
033        private LoggingLevel level;
034    
035        public LoggingErrorHandler(Processor output) {
036            this(output, LogFactory.getLog(LoggingErrorHandler.class), LoggingLevel.INFO);
037        }
038    
039        public LoggingErrorHandler(Processor output, Log log, LoggingLevel level) {
040            this.output = output;
041            this.log = log;
042            this.level = level;
043        }
044    
045        @Override
046        public String toString() {
047            return "LoggingErrorHandler[" + output + "]";
048        }
049    
050        public void process(Exchange exchange) throws Exception {
051            try {
052                output.process(exchange);
053            } catch (Throwable e) {
054                if (!customProcessorForException(exchange, e)) {
055                    logError(exchange, e);
056                }
057            }
058        }
059    
060        // Properties
061        // -------------------------------------------------------------------------
062    
063        /**
064         * Returns the output processor
065         */
066        public Processor getOutput() {
067            return output;
068        }
069    
070        public LoggingLevel getLevel() {
071            return level;
072        }
073    
074        public void setLevel(LoggingLevel level) {
075            this.level = level;
076        }
077    
078        public Log getLog() {
079            return log;
080        }
081    
082        public void setLog(Log log) {
083            this.log = log;
084        }
085    
086        // Implementation methods
087        // -------------------------------------------------------------------------
088        protected void logError(Exchange exchange, Throwable e) {
089            switch (level) {
090            case DEBUG:
091                if (log.isDebugEnabled()) {
092                    log.debug(logMessage(exchange, e), e);
093                }
094                break;
095            case ERROR:
096                if (log.isErrorEnabled()) {
097                    log.error(logMessage(exchange, e), e);
098                }
099                break;
100            case FATAL:
101                if (log.isFatalEnabled()) {
102                    log.fatal(logMessage(exchange, e), e);
103                }
104                break;
105            case INFO:
106                if (log.isInfoEnabled()) {
107                    log.debug(logMessage(exchange, e), e);
108                }
109                break;
110            case TRACE:
111                if (log.isTraceEnabled()) {
112                    log.trace(logMessage(exchange, e), e);
113                }
114                break;
115            case WARN:
116                if (log.isWarnEnabled()) {
117                    log.warn(logMessage(exchange, e), e);
118                }
119                break;
120            default:
121                log.error("Unknown level: " + level + " when trying to log exchange: " + logMessage(exchange, e),
122                          e);
123            }
124        }
125    
126        protected Object logMessage(Exchange exchange, Throwable e) {
127            return e + " while processing exchange: " + exchange;
128        }
129    
130        protected void doStart() throws Exception {
131            ServiceHelper.startServices(output);
132        }
133    
134        protected void doStop() throws Exception {
135            ServiceHelper.stopServices(output);
136        }
137    }