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.Processor; 021 import org.apache.camel.impl.DefaultExchangeFormatter; 022 import org.apache.camel.processor.interceptor.ExchangeFormatter; 023 import org.apache.commons.logging.Log; 024 import org.apache.commons.logging.LogFactory; 025 026 /** 027 * A {@link Processor} which just logs to a {@link Log} object which can be used 028 * as an exception handler instead of using a dead letter queue. 029 * 030 * @version $Revision: 705129 $ 031 */ 032 public class Logger implements Processor { 033 private Log log; 034 private LoggingLevel level; 035 private ExchangeFormatter formatter = DefaultExchangeFormatter.getInstance(); 036 037 public Logger() { 038 this(LogFactory.getLog(Logger.class)); 039 } 040 041 public Logger(Log log) { 042 this(log, LoggingLevel.INFO); 043 } 044 045 public Logger(Log log, LoggingLevel level) { 046 this.log = log; 047 this.level = level; 048 } 049 050 public Logger(String logName) { 051 this(LogFactory.getLog(logName)); 052 } 053 054 public Logger(String logName, LoggingLevel level) { 055 this(LogFactory.getLog(logName), level); 056 } 057 058 public Logger(Log log, ExchangeFormatter formatter) { 059 this(log); 060 this.formatter = formatter; 061 } 062 063 @Override 064 public String toString() { 065 return "Logger[" + log + "]"; 066 } 067 068 public void process(Exchange exchange) { 069 switch (level) { 070 case DEBUG: 071 if (log.isDebugEnabled()) { 072 log.debug(logMessage(exchange)); 073 } 074 break; 075 case ERROR: 076 if (log.isErrorEnabled()) { 077 log.error(logMessage(exchange)); 078 } 079 break; 080 case FATAL: 081 if (log.isFatalEnabled()) { 082 log.fatal(logMessage(exchange)); 083 } 084 break; 085 case INFO: 086 if (log.isInfoEnabled()) { 087 log.info(logMessage(exchange)); 088 } 089 break; 090 case TRACE: 091 if (log.isTraceEnabled()) { 092 log.trace(logMessage(exchange)); 093 } 094 break; 095 case WARN: 096 if (log.isWarnEnabled()) { 097 log.warn(logMessage(exchange)); 098 } 099 break; 100 default: 101 log.error("Unknown level: " + level + " when trying to log exchange: " + logMessage(exchange)); 102 } 103 } 104 105 public void process(Exchange exchange, Throwable exception) { 106 switch (level) { 107 case DEBUG: 108 if (log.isDebugEnabled()) { 109 log.debug(logMessage(exchange), exception); 110 } 111 break; 112 case ERROR: 113 if (log.isErrorEnabled()) { 114 log.error(logMessage(exchange), exception); 115 } 116 break; 117 case FATAL: 118 if (log.isFatalEnabled()) { 119 log.fatal(logMessage(exchange), exception); 120 } 121 break; 122 case INFO: 123 if (log.isInfoEnabled()) { 124 log.info(logMessage(exchange), exception); 125 } 126 break; 127 case TRACE: 128 if (log.isTraceEnabled()) { 129 log.trace(logMessage(exchange), exception); 130 } 131 break; 132 case WARN: 133 if (log.isWarnEnabled()) { 134 log.warn(logMessage(exchange), exception); 135 } 136 break; 137 default: 138 log.error("Unknown level: " + level + " when trying to log exchange: " + logMessage(exchange)); 139 } 140 } 141 142 public void log(String message) { 143 switch (level) { 144 case DEBUG: 145 if (log.isDebugEnabled()) { 146 log.debug(message); 147 } 148 break; 149 case ERROR: 150 if (log.isErrorEnabled()) { 151 log.error(message); 152 } 153 break; 154 case FATAL: 155 if (log.isFatalEnabled()) { 156 log.fatal(message); 157 } 158 break; 159 case INFO: 160 if (log.isInfoEnabled()) { 161 log.debug(message); 162 } 163 break; 164 case TRACE: 165 if (log.isTraceEnabled()) { 166 log.trace(message); 167 } 168 break; 169 case WARN: 170 if (log.isWarnEnabled()) { 171 log.warn(message); 172 } 173 break; 174 default: 175 log.error("Unknown level: " + level + " when trying to log exchange: " + message); 176 } 177 } 178 179 public void log(String message, Throwable exception) { 180 switch (level) { 181 case DEBUG: 182 if (log.isDebugEnabled()) { 183 log.debug(message, exception); 184 } 185 break; 186 case ERROR: 187 if (log.isErrorEnabled()) { 188 log.error(message, exception); 189 } 190 break; 191 case FATAL: 192 if (log.isFatalEnabled()) { 193 log.fatal(message, exception); 194 } 195 break; 196 case INFO: 197 if (log.isInfoEnabled()) { 198 log.debug(message, exception); 199 } 200 break; 201 case TRACE: 202 if (log.isTraceEnabled()) { 203 log.trace(message, exception); 204 } 205 break; 206 case WARN: 207 if (log.isWarnEnabled()) { 208 log.warn(message, exception); 209 } 210 break; 211 case OFF: 212 break; 213 default: 214 log.error("Unknown level: " + level + " when trying to log exchange: " + message, exception); 215 } 216 } 217 218 protected Object logMessage(Exchange exchange) { 219 return formatter.format(exchange); 220 } 221 222 public Log getLog() { 223 return log; 224 } 225 226 public void setLog(Log log) { 227 this.log = log; 228 } 229 230 public LoggingLevel getLevel() { 231 return level; 232 } 233 234 public void setLevel(LoggingLevel level) { 235 this.level = level; 236 } 237 238 public void setFormatter(ExchangeFormatter formatter) { 239 this.formatter = formatter; 240 } 241 }