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 */ 017 package org.apache.camel.processor; 018 019 import org.apache.camel.AsyncCallback; 020 import org.apache.camel.AsyncProcessor; 021 import org.apache.camel.Exchange; 022 import org.apache.camel.Expression; 023 import org.apache.camel.Traceable; 024 import org.apache.camel.support.ServiceSupport; 025 import org.apache.camel.util.AsyncProcessorHelper; 026 import org.apache.camel.util.CamelLogger; 027 028 /** 029 * A processor which evaluates an {@link Expression} and logs it. 030 * 031 * @version 032 */ 033 public class LogProcessor extends ServiceSupport implements AsyncProcessor, Traceable { 034 035 private final Expression expression; 036 private final CamelLogger logger; 037 038 public LogProcessor(Expression expression, CamelLogger logger) { 039 this.expression = expression; 040 this.logger = logger; 041 } 042 043 public void process(Exchange exchange) throws Exception { 044 AsyncProcessorHelper.process(this, exchange); 045 } 046 047 @Override 048 public boolean process(Exchange exchange, AsyncCallback callback) { 049 try { 050 if (logger.shouldLog()) { 051 String msg = expression.evaluate(exchange, String.class); 052 logger.doLog(msg); 053 } 054 } catch (Exception e) { 055 exchange.setException(e); 056 } finally { 057 // callback must be invoked 058 callback.done(true); 059 } 060 return true; 061 } 062 063 @Override 064 public String toString() { 065 return "Log(" + logger.getLog().getName() + ")[" + expression + "]"; 066 } 067 068 public String getTraceLabel() { 069 return "log[" + expression + "]"; 070 } 071 072 @Override 073 protected void doStart() throws Exception { 074 // noop 075 } 076 077 @Override 078 protected void doStop() throws Exception { 079 // noop 080 } 081 }