001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.processor;
019    
020    import org.apache.camel.Exchange;
021    import org.apache.camel.Processor;
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    
025    /**
026     * A {@link Processor} which just logs to a {@link Log} object which can be used as an exception
027     * handler instead of using a dead letter queue.
028     *
029     * @version $Revision: 534145 $
030     */
031    public class Logger implements Processor {
032        private Log log;
033        private LoggingLevel level;
034    
035        public Logger() {
036            this(LogFactory.getLog(Logger.class));
037        }
038    
039        public Logger(Log log) {
040            this(log, LoggingLevel.INFO);
041        }
042    
043        public Logger(Log log, LoggingLevel level) {
044            this.log = log;
045            this.level = level;
046        }
047    
048        @Override
049        public String toString() {
050            return "Logger[" + log + "]";
051        }
052    
053        public void process(Exchange exchange) {
054            switch (level) {
055                case DEBUG:
056                    if (log.isDebugEnabled()) {
057                        log.debug(logMessage(exchange));
058                    }
059                    break;
060                case ERROR:
061                    if (log.isErrorEnabled()) {
062                        log.error(logMessage(exchange));
063                    }
064                    break;
065                case FATAL:
066                    if (log.isFatalEnabled()) {
067                        log.fatal(logMessage(exchange));
068                    }
069                    break;
070                case INFO:
071                    if (log.isInfoEnabled()) {
072                        log.debug(logMessage(exchange));
073                    }
074                    break;
075                case TRACE:
076                    if (log.isTraceEnabled()) {
077                        log.trace(logMessage(exchange));
078                    }
079                    break;
080                case WARN:
081                    if (log.isWarnEnabled()) {
082                        log.warn(logMessage(exchange));
083                    }
084                    break;
085                default:
086                    log.error("Unknown level: " + level + " when trying to log exchange: " + logMessage(exchange));
087            }
088        }
089    
090        public void log(String message) {
091            switch (level) {
092                case DEBUG:
093                    if (log.isDebugEnabled()) {
094                        log.debug(message);
095                    }
096                    break;
097                case ERROR:
098                    if (log.isErrorEnabled()) {
099                        log.error(message);
100                    }
101                    break;
102                case FATAL:
103                    if (log.isFatalEnabled()) {
104                        log.fatal(message);
105                    }
106                    break;
107                case INFO:
108                    if (log.isInfoEnabled()) {
109                        log.debug(message);
110                    }
111                    break;
112                case TRACE:
113                    if (log.isTraceEnabled()) {
114                        log.trace(message);
115                    }
116                    break;
117                case WARN:
118                    if (log.isWarnEnabled()) {
119                        log.warn(message);
120                    }
121                    break;
122                default:
123                    log.error("Unknown level: " + level + " when trying to log exchange: " + message);
124            }
125        }
126    
127        public void log(String message, Throwable exception) {
128            switch (level) {
129                case DEBUG:
130                    if (log.isDebugEnabled()) {
131                        log.debug(message, exception);
132                    }
133                    break;
134                case ERROR:
135                    if (log.isErrorEnabled()) {
136                        log.error(message, exception);
137                    }
138                    break;
139                case FATAL:
140                    if (log.isFatalEnabled()) {
141                        log.fatal(message, exception);
142                    }
143                    break;
144                case INFO:
145                    if (log.isInfoEnabled()) {
146                        log.debug(message, exception);
147                    }
148                    break;
149                case TRACE:
150                    if (log.isTraceEnabled()) {
151                        log.trace(message, exception);
152                    }
153                    break;
154                case WARN:
155                    if (log.isWarnEnabled()) {
156                        log.warn(message, exception);
157                    }
158                    break;
159                default:
160                    log.error("Unknown level: " + level + " when trying to log exchange: " + message, exception);
161            }
162        }
163    
164        protected Object logMessage(Exchange exchange) {
165            return exchange;
166        }
167    
168        public Log getLog() {
169            return log;
170        }
171    
172        public void setLog(Log log) {
173            this.log = log;
174        }
175    
176        public LoggingLevel getLevel() {
177            return level;
178        }
179    
180        public void setLevel(LoggingLevel level) {
181            this.level = level;
182        }
183    }