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.builder;
018    
019    import org.apache.camel.LoggingLevel;
020    import org.apache.camel.Processor;
021    import org.apache.camel.processor.LoggingErrorHandler;
022    import org.apache.camel.spi.RouteContext;
023    import org.apache.camel.util.CamelLogger;
024    import org.slf4j.Logger;
025    import org.slf4j.LoggerFactory;
026    
027    /**
028     * Uses the {@link Logger} as an error handler, will log at <tt>ERROR</tt> level by default.
029     *
030     * @version 
031     */
032    public class LoggingErrorHandlerBuilder extends ErrorHandlerBuilderSupport {
033        private Logger log = LoggerFactory.getLogger(Logger.class);
034        private LoggingLevel level = LoggingLevel.ERROR;
035    
036        public LoggingErrorHandlerBuilder() {
037        }
038    
039        public LoggingErrorHandlerBuilder(final Logger log) {
040            this.log = log;
041        }
042    
043        public LoggingErrorHandlerBuilder(final Logger log, final LoggingLevel level) {
044            this.log = log;
045            this.level = level;
046        }
047    
048        public boolean supportTransacted() {
049            return false;
050        }
051    
052        public Processor createErrorHandler(final RouteContext routeContext, final Processor processor) {
053            CamelLogger logger = new CamelLogger(log, level);
054    
055            LoggingErrorHandler handler = new LoggingErrorHandler(routeContext.getCamelContext(), processor, logger, getExceptionPolicyStrategy());
056            configure(routeContext, handler);
057            return handler;
058        }
059    
060        public LoggingLevel getLevel() {
061            return level;
062        }
063    
064        public void setLevel(final LoggingLevel level) {
065            this.level = level;
066        }
067    
068        public Logger getLog() {
069            return log;
070        }
071    
072        public void setLog(final Logger log) {
073            this.log = log;
074        }
075    
076        public String getLogName() {
077            return log != null ? log.getName() : null;
078        }
079    
080        public void setLogName(String logName) {
081            this.log = LoggerFactory.getLogger(logName);
082        }
083    
084        public LoggingErrorHandlerBuilder level(final LoggingLevel level) {
085            this.level = level;
086            return this;
087        }
088    
089        public LoggingErrorHandlerBuilder log(final Logger log) {
090            this.log = log;
091            return this;
092        }
093    
094        public LoggingErrorHandlerBuilder logName(final String logName) {
095            setLogName(logName);
096            return this;
097        }
098    
099    }