View Javadoc

1   /*
2    * $Id: LoggerMessageProcessor.java 22584 2011-08-04 00:52:00Z julien.eluard $
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 java.util.Collections;
14  import java.util.Map;
15  import java.util.concurrent.ConcurrentHashMap;
16  
17  import javax.xml.namespace.QName;
18  
19  import org.mule.api.AnnotatedObject;
20  import org.mule.api.MuleContext;
21  import org.mule.api.MuleEvent;
22  import org.mule.api.MuleException;
23  import org.mule.api.context.MuleContextAware;
24  import org.mule.api.expression.ExpressionManager;
25  import org.mule.api.lifecycle.Initialisable;
26  import org.mule.api.lifecycle.InitialisationException;
27  import org.mule.util.StringUtils;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.log4j.Level;
32  
33  /**
34   * MessageProcessor implementation that logs the current element of a value evaluated from it using
35   * an expression evaluator. By default the current messages is logged using the {@link Level#DEBUG}
36   * level to the 'org.mule.api.processor.LoggerMessageProcessor' category. The level and
37   * category can both be configured to suit your needs.
38   */
39  public class LoggerMessageProcessor implements MessageProcessor, Initialisable, MuleContextAware, AnnotatedObject
40  {
41      protected transient Log logger;
42  
43      protected String message;
44      protected String category;
45      protected String level = "DEBUG";
46  
47      protected MuleContext muleContext;
48      protected ExpressionManager expressionManager;
49      private final Map<QName, Object> annotations = new ConcurrentHashMap<QName, Object>();
50  
51      public void initialise() throws InitialisationException
52      {
53          initLogger();
54          expressionManager = muleContext.getExpressionManager();
55      }
56  
57      protected void initLogger()
58      {
59          if (category != null)
60          {
61              logger = LogFactory.getLog(category);
62          }
63          else
64          {
65              logger = LogFactory.getLog(LoggerMessageProcessor.class);
66          }
67      }
68  
69      public MuleEvent process(MuleEvent event) throws MuleException
70      {
71          log(event);
72          return event;
73      }
74  
75      protected void log(MuleEvent event)
76      {
77          if (event == null)
78          {
79              logWithLevel(null);
80          }
81          else
82          {
83              if (StringUtils.isEmpty(message))
84              {
85                  logWithLevel(event.getMessage());
86              }
87              else
88              {
89                  logWithLevel(expressionManager.parse(message, event.getMessage()));
90              }
91          }
92      }
93  
94      protected void logWithLevel(Object object)
95      {
96          if ("ERROR".equals(level))
97          {
98              logger.error(object);
99          }
100         else if ("WARN".equals(level))
101         {
102             logger.warn(object);
103         }
104         else if ("INFO".equals(level))
105         {
106             if (logger.isInfoEnabled())
107             {
108                 logger.info(object);
109             }
110         }
111         else if ("DEBUG".equals(level))
112         {
113             if (logger.isDebugEnabled())
114             {
115                 logger.debug(object);
116             }
117         }
118         else if ("TRACE".equals(level))
119         {
120             if (logger.isTraceEnabled())
121             {
122                 logger.trace(object);
123             }
124         }
125     }
126 
127     public void setMuleContext(MuleContext muleContext)
128     {
129         this.muleContext = muleContext;
130     }
131 
132     public void setMessage(String message)
133     {
134         this.message = message;
135     }
136 
137     public void setCategory(String category)
138     {
139         this.category = category;
140     }
141 
142     public void setLevel(String level)
143     {
144         this.level = level.toUpperCase();
145     }
146 
147     public final Object getAnnotation(QName name)
148     {
149         return annotations.get(name);
150     }
151 
152     public final Map<QName, Object> getAnnotations()
153     {
154         return Collections.unmodifiableMap(annotations);
155     }
156 
157     public synchronized final void setAnnotations(Map<QName, Object> newAnnotations)
158     {
159         annotations.clear();
160         annotations.putAll(newAnnotations);
161     }
162 }