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