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.Exchange;
020 import org.apache.camel.LoggingLevel;
021 import org.apache.camel.Processor;
022 import org.apache.camel.spi.ExchangeFormatter;
023 import org.apache.camel.util.CamelLogger;
024
025 /**
026 * A {@link Processor} which just logs to a {@link CamelLogger} object which can be used
027 * as an exception handler instead of using a dead letter queue.
028 * <p/>
029 * The name <tt>CamelLogger</tt> has been chosen to avoid any name clash with log kits
030 * which has a <tt>Logger</tt> class.
031 *
032 * @version
033 */
034 public class CamelLogProcessor implements Processor {
035 private CamelLogger log;
036 private ExchangeFormatter formatter;
037
038 public CamelLogProcessor() {
039 this(new CamelLogger(CamelLogProcessor.class.getName()));
040 }
041
042 public CamelLogProcessor(CamelLogger log) {
043 this.formatter = new DefaultExchangeFormatter();
044 this.log = log;
045 }
046
047 public CamelLogProcessor(CamelLogger log, ExchangeFormatter formatter) {
048 this(log);
049 this.formatter = formatter;
050 }
051
052 @Override
053 public String toString() {
054 return "Logger[" + log + "]";
055 }
056
057 public void process(Exchange exchange) {
058 if (log.shouldLog()) {
059 log.log(formatter.format(exchange));
060 }
061 }
062
063 public void process(Exchange exchange, Throwable exception) {
064 if (log.shouldLog()) {
065 log.log(formatter.format(exchange), exception);
066 }
067 }
068
069 public void process(Exchange exchange, String message) {
070 if (log.shouldLog()) {
071 log.log(formatter.format(exchange) + message);
072 }
073 }
074
075 public CamelLogger getLogger() {
076 return log;
077 }
078
079 public void setLogName(String logName) {
080 log.setLogName(logName);
081 }
082
083 public void setLevel(LoggingLevel level) {
084 log.setLevel(level);
085 }
086
087 public void setMarker(String marker) {
088 log.setMarker(marker);
089 }
090
091 static class DefaultExchangeFormatter implements ExchangeFormatter {
092 public String format(Exchange exchange) {
093 return exchange.toString();
094 }
095 }
096 }