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