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