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 */
017package org.apache.camel.processor;
018
019import org.apache.camel.AsyncCallback;
020import org.apache.camel.AsyncProcessor;
021import org.apache.camel.Exchange;
022import org.apache.camel.LoggingLevel;
023import org.apache.camel.Processor;
024import org.apache.camel.spi.ExchangeFormatter;
025import org.apache.camel.support.ServiceSupport;
026import org.apache.camel.util.AsyncProcessorHelper;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030/**
031 * A {@link Processor} which just logs to a {@link CamelLogger} object which can be used
032 * as an exception handler instead of using a dead letter queue.
033 * <p/>
034 * The name <tt>CamelLogger</tt> has been chosen to avoid any name clash with log kits
035 * which has a <tt>Logger</tt> class.
036 *
037 * @deprecated This class has been split up into org.apache.camel.util.CamelLogger and org.apache.camel.processor.CamelLogProcessor 
038 */
039@Deprecated
040public class CamelLogger extends ServiceSupport implements AsyncProcessor {
041    private Logger log;
042    private LoggingLevel level;
043    private ExchangeFormatter formatter;
044
045    public CamelLogger() {
046        this(LoggerFactory.getLogger(CamelLogger.class));
047    }
048
049    public CamelLogger(Logger log) {
050        this(log, LoggingLevel.INFO);
051    }
052
053    public CamelLogger(Logger log, LoggingLevel level) {
054        this.formatter = new CamelLogProcessor.ToStringExchangeFormatter();
055        this.log = log;
056        this.level = level;
057    }
058
059    public CamelLogger(String logName) {
060        this(LoggerFactory.getLogger(logName));
061    }
062
063    public CamelLogger(String logName, LoggingLevel level) {
064        this(LoggerFactory.getLogger(logName), level);
065    }
066
067    public CamelLogger(Logger log, ExchangeFormatter formatter) {
068        this(log);
069        this.formatter = formatter;
070    }
071
072    @Override
073    public String toString() {
074        return "Logger[" + log + "]";
075    }
076
077    public void process(Exchange exchange) throws Exception {
078        AsyncProcessorHelper.process(this, exchange);
079    }
080
081    public boolean process(Exchange exchange, AsyncCallback callback) {
082        switch (level) {
083        case DEBUG:
084            if (log.isDebugEnabled()) {
085                log.debug(logMessage(exchange));
086            }
087            break;
088        case ERROR:
089            if (log.isErrorEnabled()) {
090                log.error(logMessage(exchange));
091            }
092            break;
093        case INFO:
094            if (log.isInfoEnabled()) {
095                log.info(logMessage(exchange));
096            }
097            break;
098        case TRACE:
099            if (log.isTraceEnabled()) {
100                log.trace(logMessage(exchange));
101            }
102            break;
103        case WARN:
104            if (log.isWarnEnabled()) {
105                log.warn(logMessage(exchange));
106            }
107            break;
108        case OFF:
109            break;
110        default:
111            log.error("Unknown level: " + level + " when trying to log exchange: " + logMessage(exchange));
112        }
113
114        callback.done(true);
115        return true;
116    }
117
118    public void process(Exchange exchange, Throwable exception) {
119        switch (level) {
120        case DEBUG:
121            if (log.isDebugEnabled()) {
122                log.debug(logMessage(exchange), exception);
123            }
124            break;
125        case ERROR:
126            if (log.isErrorEnabled()) {
127                log.error(logMessage(exchange), exception);
128            }
129            break;
130        case INFO:
131            if (log.isInfoEnabled()) {
132                log.info(logMessage(exchange), exception);
133            }
134            break;
135        case TRACE:
136            if (log.isTraceEnabled()) {
137                log.trace(logMessage(exchange), exception);
138            }
139            break;
140        case WARN:
141            if (log.isWarnEnabled()) {
142                log.warn(logMessage(exchange), exception);
143            }
144            break;
145        case OFF:
146            break;
147        default:
148            log.error("Unknown level: " + level + " when trying to log exchange: " + logMessage(exchange));
149        }
150    }
151
152    public void process(Exchange exchange, String message) {
153        switch (level) {
154        case DEBUG:
155            if (log.isDebugEnabled()) {
156                log.debug(logMessage(exchange, message));
157            }
158            break;
159        case ERROR:
160            if (log.isErrorEnabled()) {
161                log.error(logMessage(exchange, message));
162            }
163            break;
164        case INFO:
165            if (log.isInfoEnabled()) {
166                log.info(logMessage(exchange, message));
167            }
168            break;
169        case TRACE:
170            if (log.isTraceEnabled()) {
171                log.trace(logMessage(exchange, message));
172            }
173            break;
174        case WARN:
175            if (log.isWarnEnabled()) {
176                log.warn(logMessage(exchange, message));
177            }
178            break;
179        case OFF:
180            break;
181        default:
182            log.error("Unknown level: " + level + " when trying to log exchange: " + logMessage(exchange, message));
183        }
184    }
185
186    public void log(String message, LoggingLevel loggingLevel) {
187        LoggingLevel oldLogLevel = getLevel();
188        setLevel(loggingLevel);
189        log(message);
190        setLevel(oldLogLevel);
191    }
192    
193    public void log(String message) {
194        switch (level) {
195        case DEBUG:
196            if (log.isDebugEnabled()) {
197                log.debug(message);
198            }
199            break;
200        case ERROR:
201            if (log.isErrorEnabled()) {
202                log.error(message);
203            }
204            break;
205        case INFO:
206            if (log.isInfoEnabled()) {
207                log.info(message);
208            }
209            break;
210        case TRACE:
211            if (log.isTraceEnabled()) {
212                log.trace(message);
213            }
214            break;
215        case WARN:
216            if (log.isWarnEnabled()) {
217                log.warn(message);
218            }
219            break;
220        case OFF:
221            break;
222        default:
223            log.error("Unknown level: " + level + " when trying to log exchange: " + message);
224        }
225    }
226
227    public void log(String message, Throwable exception, LoggingLevel loggingLevel) {
228        LoggingLevel oldLogLevel = getLevel();
229        setLevel(loggingLevel);
230        log(message, exception);
231        setLevel(oldLogLevel);
232    }   
233    
234    public void log(String message, Throwable exception) {
235        switch (level) {
236        case DEBUG:
237            if (log.isDebugEnabled()) {
238                log.debug(message, exception);
239            }
240            break;
241        case ERROR:
242            if (log.isErrorEnabled()) {
243                log.error(message, exception);
244            }
245            break;
246        case INFO:
247            if (log.isInfoEnabled()) {
248                log.info(message, exception);
249            }
250            break;
251        case TRACE:
252            if (log.isTraceEnabled()) {
253                log.trace(message, exception);
254            }
255            break;
256        case WARN:
257            if (log.isWarnEnabled()) {
258                log.warn(message, exception);
259            }
260            break;
261        case OFF:
262            break;
263        default:
264            log.error("Unknown level: " + level + " when trying to log exchange: " + message, exception);
265        }
266    }
267
268    protected String logMessage(Exchange exchange) {
269        return formatter.format(exchange);
270    }
271
272    protected String logMessage(Exchange exchange, String message) {
273        return formatter.format(exchange) + message;
274    }
275
276    public Logger getLog() {
277        return log;
278    }
279
280    public void setLog(Logger log) {
281        this.log = log;
282    }
283
284    public LoggingLevel getLevel() {
285        return level;
286    }
287
288    public void setLevel(LoggingLevel level) {
289        this.level = level;
290    }
291
292    public void setFormatter(ExchangeFormatter formatter) {
293        this.formatter = formatter;
294    }
295
296    public void setLogName(String logName) {
297        this.log = LoggerFactory.getLogger(logName);
298    }
299
300    @Override
301    protected void doStart() throws Exception {
302        // noop
303    }
304
305    @Override
306    protected void doStop() throws Exception {
307        // noop
308    }
309}