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