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.camel.processor.interceptor.TraceInterceptor;
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: 676850 $
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) {
144 switch (level) {
145 case DEBUG:
146 if (log.isDebugEnabled()) {
147 log.debug(message);
148 }
149 break;
150 case ERROR:
151 if (log.isErrorEnabled()) {
152 log.error(message);
153 }
154 break;
155 case FATAL:
156 if (log.isFatalEnabled()) {
157 log.fatal(message);
158 }
159 break;
160 case INFO:
161 if (log.isInfoEnabled()) {
162 log.debug(message);
163 }
164 break;
165 case TRACE:
166 if (log.isTraceEnabled()) {
167 log.trace(message);
168 }
169 break;
170 case WARN:
171 if (log.isWarnEnabled()) {
172 log.warn(message);
173 }
174 break;
175 default:
176 log.error("Unknown level: " + level + " when trying to log exchange: " + message);
177 }
178 }
179
180 public void log(String message, Throwable exception) {
181 switch (level) {
182 case DEBUG:
183 if (log.isDebugEnabled()) {
184 log.debug(message, exception);
185 }
186 break;
187 case ERROR:
188 if (log.isErrorEnabled()) {
189 log.error(message, exception);
190 }
191 break;
192 case FATAL:
193 if (log.isFatalEnabled()) {
194 log.fatal(message, exception);
195 }
196 break;
197 case INFO:
198 if (log.isInfoEnabled()) {
199 log.debug(message, exception);
200 }
201 break;
202 case TRACE:
203 if (log.isTraceEnabled()) {
204 log.trace(message, exception);
205 }
206 break;
207 case WARN:
208 if (log.isWarnEnabled()) {
209 log.warn(message, exception);
210 }
211 break;
212 default:
213 log.error("Unknown level: " + level + " when trying to log exchange: " + message, exception);
214 }
215 }
216
217 protected Object logMessage(Exchange exchange) {
218 return formatter.format(exchange);
219 }
220
221 public Log getLog() {
222 return log;
223 }
224
225 public void setLog(Log log) {
226 this.log = log;
227 }
228
229 public LoggingLevel getLevel() {
230 return level;
231 }
232
233 public void setLevel(LoggingLevel level) {
234 this.level = level;
235 }
236
237 public void setFormatter(ExchangeFormatter formatter) {
238 this.formatter = formatter;
239 }
240 }