Coverage Report - org.apache.camel.processor.Logger
 
Classes in this File Line Coverage Branch Coverage Complexity
Logger
19% 
11% 
0
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.processor;
 19  
 
 20  
 import org.apache.camel.Exchange;
 21  
 import org.apache.camel.Processor;
 22  
 import org.apache.commons.logging.Log;
 23  
 import org.apache.commons.logging.LogFactory;
 24  
 
 25  
 /**
 26  
  * A {@link Processor} which just logs to a {@link Log} object which can be used as an exception
 27  
  * handler instead of using a dead letter queue.
 28  
  *
 29  
  * @version $Revision: 534145 $
 30  
  */
 31  
 public class Logger implements Processor {
 32  
     private Log log;
 33  
     private LoggingLevel level;
 34  
 
 35  
     public Logger() {
 36  0
         this(LogFactory.getLog(Logger.class));
 37  0
     }
 38  
 
 39  
     public Logger(Log log) {
 40  1
         this(log, LoggingLevel.INFO);
 41  1
     }
 42  
 
 43  85
     public Logger(Log log, LoggingLevel level) {
 44  85
         this.log = log;
 45  85
         this.level = level;
 46  85
     }
 47  
 
 48  
     @Override
 49  
     public String toString() {
 50  0
         return "Logger[" + log + "]";
 51  
     }
 52  
 
 53  
     public void process(Exchange exchange) {
 54  0
         switch (level) {
 55  
             case DEBUG:
 56  0
                 if (log.isDebugEnabled()) {
 57  0
                     log.debug(logMessage(exchange));
 58  0
                 }
 59  
                 break;
 60  
             case ERROR:
 61  0
                 if (log.isErrorEnabled()) {
 62  0
                     log.error(logMessage(exchange));
 63  0
                 }
 64  
                 break;
 65  
             case FATAL:
 66  0
                 if (log.isFatalEnabled()) {
 67  0
                     log.fatal(logMessage(exchange));
 68  0
                 }
 69  
                 break;
 70  
             case INFO:
 71  0
                 if (log.isInfoEnabled()) {
 72  0
                     log.debug(logMessage(exchange));
 73  0
                 }
 74  
                 break;
 75  
             case TRACE:
 76  0
                 if (log.isTraceEnabled()) {
 77  0
                     log.trace(logMessage(exchange));
 78  0
                 }
 79  
                 break;
 80  
             case WARN:
 81  0
                 if (log.isWarnEnabled()) {
 82  0
                     log.warn(logMessage(exchange));
 83  0
                 }
 84  
                 break;
 85  
             default:
 86  0
                 log.error("Unknown level: " + level + " when trying to log exchange: " + logMessage(exchange));
 87  
         }
 88  0
     }
 89  
 
 90  
     public void log(String message) {
 91  0
         switch (level) {
 92  
             case DEBUG:
 93  0
                 if (log.isDebugEnabled()) {
 94  0
                     log.debug(message);
 95  0
                 }
 96  
                 break;
 97  
             case ERROR:
 98  0
                 if (log.isErrorEnabled()) {
 99  0
                     log.error(message);
 100  0
                 }
 101  
                 break;
 102  
             case FATAL:
 103  0
                 if (log.isFatalEnabled()) {
 104  0
                     log.fatal(message);
 105  0
                 }
 106  
                 break;
 107  
             case INFO:
 108  0
                 if (log.isInfoEnabled()) {
 109  0
                     log.debug(message);
 110  0
                 }
 111  
                 break;
 112  
             case TRACE:
 113  0
                 if (log.isTraceEnabled()) {
 114  0
                     log.trace(message);
 115  0
                 }
 116  
                 break;
 117  
             case WARN:
 118  0
                 if (log.isWarnEnabled()) {
 119  0
                     log.warn(message);
 120  0
                 }
 121  
                 break;
 122  
             default:
 123  0
                 log.error("Unknown level: " + level + " when trying to log exchange: " + message);
 124  
         }
 125  0
     }
 126  
 
 127  
     public void log(String message, Throwable exception) {
 128  4
         switch (level) {
 129  
             case DEBUG:
 130  3
                 if (log.isDebugEnabled()) {
 131  0
                     log.debug(message, exception);
 132  0
                 }
 133  
                 break;
 134  
             case ERROR:
 135  1
                 if (log.isErrorEnabled()) {
 136  1
                     log.error(message, exception);
 137  1
                 }
 138  
                 break;
 139  
             case FATAL:
 140  0
                 if (log.isFatalEnabled()) {
 141  0
                     log.fatal(message, exception);
 142  0
                 }
 143  
                 break;
 144  
             case INFO:
 145  0
                 if (log.isInfoEnabled()) {
 146  0
                     log.debug(message, exception);
 147  0
                 }
 148  
                 break;
 149  
             case TRACE:
 150  0
                 if (log.isTraceEnabled()) {
 151  0
                     log.trace(message, exception);
 152  0
                 }
 153  
                 break;
 154  
             case WARN:
 155  0
                 if (log.isWarnEnabled()) {
 156  0
                     log.warn(message, exception);
 157  0
                 }
 158  
                 break;
 159  
             default:
 160  0
                 log.error("Unknown level: " + level + " when trying to log exchange: " + message, exception);
 161  
         }
 162  4
     }
 163  
 
 164  
     protected Object logMessage(Exchange exchange) {
 165  0
         return exchange;
 166  
     }
 167  
 
 168  
     public Log getLog() {
 169  0
         return log;
 170  
     }
 171  
 
 172  
     public void setLog(Log log) {
 173  0
         this.log = log;
 174  0
     }
 175  
 
 176  
     public LoggingLevel getLevel() {
 177  0
         return level;
 178  
     }
 179  
 
 180  
     public void setLevel(LoggingLevel level) {
 181  2
         this.level = level;
 182  2
     }
 183  
 }