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.impl.ServiceSupport;
022    import org.apache.camel.util.ServiceHelper;
023    import org.apache.commons.logging.Log;
024    import org.apache.commons.logging.LogFactory;
025    
026    /**
027     * An {@link ErrorHandler} which uses commons-logging to dump the error
028     * 
029     * @version $Revision: 35332 $
030     */
031    public class LoggingErrorHandler extends ErrorHandlerSupport {
032        private Processor output;
033        private Log log;
034        private LoggingLevel level;
035    
036        public LoggingErrorHandler(Processor output) {
037            this(output, LogFactory.getLog(LoggingErrorHandler.class), LoggingLevel.INFO);
038        }
039    
040        public LoggingErrorHandler(Processor output, Log log, LoggingLevel level) {
041            this.output = output;
042            this.log = log;
043            this.level = level;
044        }
045    
046        @Override
047        public String toString() {
048            return "LoggingErrorHandler[" + output + "]";
049        }
050    
051        public void process(Exchange exchange) throws Exception {
052            try {
053                output.process(exchange);
054            } catch (Throwable e) {
055                if (!customProcessorForException(exchange, e)) {
056                    logError(exchange, e);
057                }
058            }
059        }
060    
061        // Properties
062        // -------------------------------------------------------------------------
063    
064        /**
065         * Returns the output processor
066         */
067        public Processor getOutput() {
068            return output;
069        }
070    
071        public LoggingLevel getLevel() {
072            return level;
073        }
074    
075        public void setLevel(LoggingLevel level) {
076            this.level = level;
077        }
078    
079        public Log getLog() {
080            return log;
081        }
082    
083        public void setLog(Log log) {
084            this.log = log;
085        }
086    
087        // Implementation methods
088        // -------------------------------------------------------------------------
089        protected void logError(Exchange exchange, Throwable e) {
090            switch (level) {
091            case DEBUG:
092                if (log.isDebugEnabled()) {
093                    log.debug(logMessage(exchange, e), e);
094                }
095                break;
096            case ERROR:
097                if (log.isErrorEnabled()) {
098                    log.error(logMessage(exchange, e), e);
099                }
100                break;
101            case FATAL:
102                if (log.isFatalEnabled()) {
103                    log.fatal(logMessage(exchange, e), e);
104                }
105                break;
106            case INFO:
107                if (log.isInfoEnabled()) {
108                    log.debug(logMessage(exchange, e), e);
109                }
110                break;
111            case TRACE:
112                if (log.isTraceEnabled()) {
113                    log.trace(logMessage(exchange, e), e);
114                }
115                break;
116            case WARN:
117                if (log.isWarnEnabled()) {
118                    log.warn(logMessage(exchange, e), e);
119                }
120                break;
121            default:
122                log.error("Unknown level: " + level + " when trying to log exchange: " + logMessage(exchange, e),
123                          e);
124            }
125        }
126    
127        protected Object logMessage(Exchange exchange, Throwable e) {
128            return e + " while processing exchange: " + exchange;
129        }
130    
131        protected void doStart() throws Exception {
132            ServiceHelper.startServices(output);
133        }
134    
135        protected void doStop() throws Exception {
136            ServiceHelper.stopServices(output);
137        }
138    }