View Javadoc
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  public class LoggerMessageProcessor implements MessageProcessor, Initialisable, MuleContextAware
29  {
30      protected transient Log logger;
31  
32      protected String message;
33      protected String category;
34      protected String level = "DEBUG";
35  
36      protected MuleContext muleContext;
37      protected ExpressionManager expressionManager;
38  
39      public void initialise() throws InitialisationException
40      {
41          initLogger();
42          expressionManager = muleContext.getExpressionManager();
43      }
44  
45      protected void initLogger()
46      {
47          if (category != null)
48          {
49              logger = LogFactory.getLog(category);
50          }
51          else
52          {
53              logger = LogFactory.getLog(LoggerMessageProcessor.class);
54          }
55      }
56  
57      public MuleEvent process(MuleEvent event) throws MuleException
58      {
59          log(event);
60          return event;
61      }
62  
63      protected void log(MuleEvent event)
64      {
65          if (event == null)
66          {
67              logWithLevel(null);
68          }
69          else
70          {
71              if (StringUtils.isEmpty(message))
72              {
73                  logWithLevel(event.getMessage());
74              }
75              else
76              {
77                  logWithLevel(expressionManager.parse(message, event.getMessage()));
78              }
79          }
80      }
81  
82      protected void logWithLevel(Object object)
83      {
84          if ("ERROR".equals(level))
85          {
86              logger.error(object);
87          }
88          else if ("WARN".equals(level))
89          {
90              logger.warn(object);
91          }
92          else if ("INFO".equals(level))
93          {
94              if (logger.isInfoEnabled())
95              {
96                  logger.info(object);
97              }
98          }
99          else if ("DEBUG".equals(level))
100         {
101             if (logger.isDebugEnabled())
102             {
103                 logger.debug(object);
104             }
105         }
106         else if ("TRACE".equals(level))
107         {
108             if (logger.isTraceEnabled())
109             {
110                 logger.trace(object);
111             }
112         }
113     }
114 
115     public void setMuleContext(MuleContext muleContext)
116     {
117         this.muleContext = muleContext;
118     }
119 
120     public void setMessage(String message)
121     {
122         this.message = message;
123     }
124 
125     public void setCategory(String category)
126     {
127         this.category = category;
128     }
129 
130     public void setLevel(String level)
131     {
132         this.level = level.toUpperCase();
133     }
134 }