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 */
017package org.apache.camel.processor;
018
019import org.apache.camel.AsyncCallback;
020import org.apache.camel.AsyncProcessor;
021import org.apache.camel.Exchange;
022import org.apache.camel.LoggingLevel;
023import org.apache.camel.Processor;
024import org.apache.camel.spi.ExchangeFormatter;
025import org.apache.camel.util.AsyncProcessorHelper;
026import org.apache.camel.util.CamelLogger;
027
028/**
029 * A {@link Processor} which just logs to a {@link CamelLogger} object which can be used
030 * as an exception handler instead of using a dead letter queue.
031 * <p/>
032 * The name <tt>CamelLogger</tt> has been chosen to avoid any name clash with log kits
033 * which has a <tt>Logger</tt> class.
034 *
035 * @version 
036 */
037public class CamelLogProcessor implements AsyncProcessor {
038    private CamelLogger log;
039    private ExchangeFormatter formatter;
040
041    public CamelLogProcessor() {
042        this(new CamelLogger(CamelLogProcessor.class.getName()));
043    }
044    
045    public CamelLogProcessor(CamelLogger log) {
046        this.formatter = new ToStringExchangeFormatter();
047        this.log = log;
048    }
049
050    public CamelLogProcessor(CamelLogger log, ExchangeFormatter formatter) {
051        this(log);
052        this.formatter = formatter;
053    }
054
055    @Override
056    public String toString() {
057        return "Logger[" + log + "]";
058    }
059
060    public void process(Exchange exchange) throws Exception {
061        AsyncProcessorHelper.process(this, exchange);
062    }
063
064    public boolean process(Exchange exchange, AsyncCallback callback) {
065        if (log.shouldLog()) {
066            log.log(formatter.format(exchange));
067        }
068        callback.done(true);
069        return true;
070    }
071
072    public void process(Exchange exchange, Throwable exception) {
073        if (log.shouldLog()) {
074            log.log(formatter.format(exchange), exception);
075        }
076    }
077
078    public void process(Exchange exchange, String message) {
079        if (log.shouldLog()) {
080            log.log(formatter.format(exchange) + message);
081        }
082    }
083
084    public CamelLogger getLogger() {
085        return log;
086    }
087    
088    public void setLogName(String logName) {
089        log.setLogName(logName);
090    }
091    
092    public void setLevel(LoggingLevel level) {
093        log.setLevel(level);
094    }
095
096    public void setMarker(String marker) {
097        log.setMarker(marker);
098    }
099
100    /**
101     * {@link ExchangeFormatter} that calls <tt>toString</tt> on the {@link Exchange}.
102     */
103    static class ToStringExchangeFormatter implements ExchangeFormatter {
104        public String format(Exchange exchange) {
105            return exchange.toString();
106        }
107    }
108}