View Javadoc

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