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.Expression;
025import org.apache.camel.Traceable;
026import org.apache.camel.spi.IdAware;
027import org.apache.camel.spi.LogListener;
028import org.apache.camel.spi.MaskingFormatter;
029import org.apache.camel.support.ServiceSupport;
030import org.apache.camel.util.AsyncProcessorHelper;
031import org.apache.camel.util.CamelLogger;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034
035/**
036 * A processor which evaluates an {@link Expression} and logs it.
037 *
038 * @version 
039 */
040public class LogProcessor extends ServiceSupport implements AsyncProcessor, Traceable, IdAware {
041
042    private static final Logger LOG = LoggerFactory.getLogger(LogProcessor.class);
043    private String id;
044    private final Expression expression;
045    private final CamelLogger logger;
046    private final MaskingFormatter formatter;
047    private final Set<LogListener> listeners;
048
049    public LogProcessor(Expression expression, CamelLogger logger, MaskingFormatter formatter, Set<LogListener> listeners) {
050        this.expression = expression;
051        this.logger = logger;
052        this.formatter = formatter;
053        this.listeners = listeners;
054    }
055
056    public void process(Exchange exchange) throws Exception {
057        AsyncProcessorHelper.process(this, exchange);
058    }
059
060    @Override
061    public boolean process(Exchange exchange, AsyncCallback callback) {
062        try {
063            if (logger.shouldLog()) {
064                String msg = expression.evaluate(exchange, String.class);
065                if (formatter != null) {
066                    msg = formatter.format(msg);
067                }
068                msg = fireListeners(exchange, msg);
069                logger.doLog(msg);
070            }
071        } catch (Exception e) {
072            exchange.setException(e);
073        } finally {
074            // callback must be invoked
075            callback.done(true);
076        }
077        return true;
078    }
079
080    private String fireListeners(Exchange exchange, String message) {
081        if (listeners == null) {
082            return message;
083        }
084        for (LogListener listener : listeners) {
085            if (listener == null) {
086                continue;
087            }
088            try {
089                String output = listener.onLog(exchange, logger, message);
090                message = output != null ? output : message;
091            } catch (Throwable t) {
092                LOG.warn("Ignoring an exception thrown by {}: {}", listener.getClass().getName(), t.getMessage());
093                if (LOG.isDebugEnabled()) {
094                    LOG.debug("", t);
095                }
096            }
097        }
098        return message;
099    }
100
101    @Override
102    public String toString() {
103        return "Log(" + logger.getLog().getName() + ")[" + expression + "]";
104    }
105
106    public String getTraceLabel() {
107        return "log[" + expression + "]";
108    }
109
110    public String getId() {
111        return id;
112    }
113
114    public void setId(String id) {
115        this.id = id;
116    }
117
118    public Expression getExpression() {
119        return expression;
120    }
121
122    public CamelLogger getLogger() {
123        return logger;
124    }
125
126    public MaskingFormatter getLogFormatter() {
127        return formatter;
128    }
129
130    @Override
131    protected void doStart() throws Exception {
132        // noop
133    }
134
135    @Override
136    protected void doStop() throws Exception {
137        // noop
138    }
139}