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 java.util.Set;
020
021import org.apache.camel.AsyncCallback;
022import org.apache.camel.AsyncProcessor;
023import org.apache.camel.Exchange;
024import org.apache.camel.LoggingLevel;
025import org.apache.camel.Processor;
026import org.apache.camel.spi.ExchangeFormatter;
027import org.apache.camel.spi.IdAware;
028import org.apache.camel.spi.LogListener;
029import org.apache.camel.spi.MaskingFormatter;
030import org.apache.camel.util.AsyncProcessorHelper;
031import org.apache.camel.util.CamelLogger;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034
035/**
036 * A {@link Processor} which just logs to a {@link CamelLogger} object which can be used
037 * as an exception handler instead of using a dead letter queue.
038 * <p/>
039 * The name <tt>CamelLogger</tt> has been chosen to avoid any name clash with log kits
040 * which has a <tt>Logger</tt> class.
041 *
042 * @version 
043 */
044public class CamelLogProcessor implements AsyncProcessor, IdAware {
045
046    private static final Logger LOG = LoggerFactory.getLogger(CamelLogProcessor.class);
047    private String id;
048    private CamelLogger log;
049    private ExchangeFormatter formatter;
050    private MaskingFormatter maskingFormatter;
051    private Set<LogListener> listeners;
052
053    public CamelLogProcessor() {
054        this(new CamelLogger(CamelLogProcessor.class.getName()));
055    }
056    
057    public CamelLogProcessor(CamelLogger log) {
058        this.formatter = new ToStringExchangeFormatter();
059        this.log = log;
060    }
061
062    public CamelLogProcessor(CamelLogger log, ExchangeFormatter formatter, MaskingFormatter maskingFormatter, Set<LogListener> listeners) {
063        this(log);
064        this.formatter = formatter;
065        this.maskingFormatter = maskingFormatter;
066        this.listeners = listeners;
067    }
068
069    @Override
070    public String toString() {
071        return "Logger[" + log + "]";
072    }
073
074    public String getId() {
075        return id;
076    }
077
078    public void setId(String id) {
079        this.id = id;
080    }
081
082    public void process(Exchange exchange) throws Exception {
083        AsyncProcessorHelper.process(this, exchange);
084    }
085
086    public boolean process(Exchange exchange, AsyncCallback callback) {
087        if (log.shouldLog()) {
088            String output = formatter.format(exchange);
089            if (maskingFormatter != null) {
090                output = maskingFormatter.format(output);
091            }
092            output = fireListeners(exchange, output);
093            log.log(output);
094        }
095        callback.done(true);
096        return true;
097    }
098
099    public void process(Exchange exchange, Throwable exception) {
100        if (log.shouldLog()) {
101            String output = formatter.format(exchange);
102            if (maskingFormatter != null) {
103                output = maskingFormatter.format(output);
104            }
105            output = fireListeners(exchange, output);
106            log.log(output, exception);
107        }
108    }
109
110    public void process(Exchange exchange, String message) {
111        if (log.shouldLog()) {
112            String output = formatter.format(exchange) + message;
113            if (maskingFormatter != null) {
114                output = maskingFormatter.format(output);
115            }
116            output = fireListeners(exchange, output);
117            log.log(output);
118        }
119    }
120
121    private String fireListeners(Exchange exchange, String message) {
122        if (listeners == null) {
123            return message;
124        }
125        for (LogListener listener : listeners) {
126            if (listener == null) {
127                continue;
128            }
129            try {
130                String output = listener.onLog(exchange, log, message);
131                message = output != null ? output : message;
132            } catch (Throwable t) {
133                LOG.warn("Ignoring an exception thrown by {}: {}", listener.getClass().getName(), t.getMessage());
134                if (LOG.isDebugEnabled()) {
135                    LOG.debug("", t);
136                }
137            }
138        }
139        return message;
140    }
141
142    public CamelLogger getLogger() {
143        return log;
144    }
145    
146    public void setLogName(String logName) {
147        log.setLogName(logName);
148    }
149    
150    public void setLevel(LoggingLevel level) {
151        log.setLevel(level);
152    }
153
154    public void setMarker(String marker) {
155        log.setMarker(marker);
156    }
157
158    public void setMaskingFormatter(MaskingFormatter maskingFormatter) {
159        this.maskingFormatter = maskingFormatter;
160    }
161
162    /**
163     * {@link ExchangeFormatter} that calls <tt>toString</tt> on the {@link Exchange}.
164     */
165    static class ToStringExchangeFormatter implements ExchangeFormatter {
166        public String format(Exchange exchange) {
167            return exchange.toString();
168        }
169    }
170
171}