Coverage Report - org.mule.api.processor.LoggerMessageProcessor
 
Classes in this File Line Coverage Branch Coverage Complexity
LoggerMessageProcessor
0%
0/39
0%
0/22
2.222
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.api.processor;
 8  
 
 9  
 import org.mule.api.MuleContext;
 10  
 import org.mule.api.MuleEvent;
 11  
 import org.mule.api.MuleException;
 12  
 import org.mule.api.context.MuleContextAware;
 13  
 import org.mule.api.expression.ExpressionManager;
 14  
 import org.mule.api.lifecycle.Initialisable;
 15  
 import org.mule.api.lifecycle.InitialisationException;
 16  
 import org.mule.util.StringUtils;
 17  
 
 18  
 import org.apache.commons.logging.Log;
 19  
 import org.apache.commons.logging.LogFactory;
 20  
 import org.apache.log4j.Level;
 21  
 
 22  
 /**
 23  
  * MessageProcessor implementation that logs the current element of a value evaluated from it using
 24  
  * an expression evaluator. By default the current messages is logged using the {@link Level#DEBUG}
 25  
  * level to the 'org.mule.api.processor.LoggerMessageProcessor' category. The level and
 26  
  * category can both be configured to suit your needs.
 27  
  */
 28  0
 public class LoggerMessageProcessor implements MessageProcessor, Initialisable, MuleContextAware
 29  
 {
 30  
     protected transient Log logger;
 31  
 
 32  
     protected String message;
 33  
     protected String category;
 34  0
     protected String level = "DEBUG";
 35  
 
 36  
     protected MuleContext muleContext;
 37  
     protected ExpressionManager expressionManager;
 38  
 
 39  
     public void initialise() throws InitialisationException
 40  
     {
 41  0
         initLogger();
 42  0
         expressionManager = muleContext.getExpressionManager();
 43  0
     }
 44  
 
 45  
     protected void initLogger()
 46  
     {
 47  0
         if (category != null)
 48  
         {
 49  0
             logger = LogFactory.getLog(category);
 50  
         }
 51  
         else
 52  
         {
 53  0
             logger = LogFactory.getLog(LoggerMessageProcessor.class);
 54  
         }
 55  0
     }
 56  
 
 57  
     public MuleEvent process(MuleEvent event) throws MuleException
 58  
     {
 59  0
         log(event);
 60  0
         return event;
 61  
     }
 62  
 
 63  
     protected void log(MuleEvent event)
 64  
     {
 65  0
         if (event == null)
 66  
         {
 67  0
             logWithLevel(null);
 68  
         }
 69  
         else
 70  
         {
 71  0
             if (StringUtils.isEmpty(message))
 72  
             {
 73  0
                 logWithLevel(event.getMessage());
 74  
             }
 75  
             else
 76  
             {
 77  0
                 logWithLevel(expressionManager.parse(message, event.getMessage()));
 78  
             }
 79  
         }
 80  0
     }
 81  
 
 82  
     protected void logWithLevel(Object object)
 83  
     {
 84  0
         if ("ERROR".equals(level))
 85  
         {
 86  0
             logger.error(object);
 87  
         }
 88  0
         else if ("WARN".equals(level))
 89  
         {
 90  0
             logger.warn(object);
 91  
         }
 92  0
         else if ("INFO".equals(level))
 93  
         {
 94  0
             if (logger.isInfoEnabled())
 95  
             {
 96  0
                 logger.info(object);
 97  
             }
 98  
         }
 99  0
         else if ("DEBUG".equals(level))
 100  
         {
 101  0
             if (logger.isDebugEnabled())
 102  
             {
 103  0
                 logger.debug(object);
 104  
             }
 105  
         }
 106  0
         else if ("TRACE".equals(level))
 107  
         {
 108  0
             if (logger.isTraceEnabled())
 109  
             {
 110  0
                 logger.trace(object);
 111  
             }
 112  
         }
 113  0
     }
 114  
 
 115  
     public void setMuleContext(MuleContext muleContext)
 116  
     {
 117  0
         this.muleContext = muleContext;
 118  0
     }
 119  
 
 120  
     public void setMessage(String message)
 121  
     {
 122  0
         this.message = message;
 123  0
     }
 124  
 
 125  
     public void setCategory(String category)
 126  
     {
 127  0
         this.category = category;
 128  0
     }
 129  
 
 130  
     public void setLevel(String level)
 131  
     {
 132  0
         this.level = level.toUpperCase();
 133  0
     }
 134  
 }