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