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