Tuesday, March 27, 2012

Logging with WSO2 ESB

Logging to the Server Log File

Add log mediators to your sequences in order to create log messages.

If you go through the sample index for WSO2 ESB and follow the steps on how to set up the samples  and then go to sample number 2 you will see how the log mediator is used.

e.g. -
<log level="custom">
        <property name="symbol" expression="get-property('symbol')"/>
        <property name="epr" expression="get-property('To')"/>

For more information on the log mediator in ESB please check here.

Logging to Other Log Files

Log mediators log into the server logs, and if you dont want the application logs to write into server logs but to write to its own specific logs you can edit log4j.properties file and easily get your proxy services to log to separate log files.

Editing log4j.properties 

Simply add the following entries into your log4j.properties file of the ESB (this example assumes a proxy service named TestProxy):

log4j.appender.PROXY_APPENDER.layout.ConversionPattern=%d{HH:mm:ss,SSS} [%X{ip}-%X{host}] [%t] %5p %c{1} %m%

With a configuration similar to the above you will see that TestProxy is now logging to a separate log file named wso2-esb-test-proxy.log as follows:

16:43:25,224 [-] [Framework Event Dispatcher] INFO TestProxy Building Axis service for Proxy service : TestProxy
16:43:25,225 [-] [Framework Event Dispatcher] INFO TestProxy Adding service TestProxy to the Axis2 configuration
16:43:25,669 [-] [Framework Event Dispatcher] INFO TestProxy Successfully created the Axis2 service for Proxy service : TestProxy
16:43:35,172 [-] [HttpServerWorker-1] INFO TestProxy To: http://localhost:8280/services/TestProxy, WSAction: urn:getQuote, SOAPAction: urn:getQuote, ReplyTo: http://www.w3.org/2005/08/addressing/anonymous, MessageID: urn:uuid:a3d87df8-6d24-4fda-b6ed-458906ff5e64, Direction: request, Envelope: http://localhost:8280/services/TestProxy</wsa:To><wsa:MessageID>urn:uuid:a3d87df8-6d24-4fda-b6ed-458906ff5e64</wsa:MessageID><wsa:Action>urn:getQuote</wsa:Action></soapenv:Header><soapenv:Body><m0:getQuote xmlns:m0="http://services.samples">IBM

Using Synapse Observers

Another alternative is to use synapse observers, which will be notified whenever a proxy service is called. We can write a custom observer that can grab proxy service level logs. For more information on observer classes go to http://synapse.apache.org/Synapse_Extending.html.

package org.wso2.carbon.customobserver.CustomSynapseObserverForLogging;

import java.io.IOException;
import org.apache.synapse.config.AbstractSynapseObserver;
import org.apache.synapse.core.axis2.ProxyService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

public class CustomSynapseObserverForLogging extends AbstractSynapseObserver {
      private static final Log log = LogFactory.getLog(CustomSynapseObserverForLogging.class);

       public void proxyServiceAdded(ProxyService proxy) {
               try {

               } catch (IOException e) {
                       log.error("CustomProxyObserver could not set service level logger for the proxy : " +
                                 proxy.getName(), e);

       public void proxyServiceRemoved(ProxyService proxy) {
               try {
               } catch (IOException e) {
                       log.error("CustomProxyObserver could not set service level logger for the proxy : " +
                                 proxy.getName(), e);

       private void setLogger(ProxyService proxy) throws IOException {

               String filename = "logs/" + proxy.getName() + ".log";
               String datePattern = "yyyy-MM-dd";
               String SYSTEM_LOG_PATTERN = "[%d] %5p - %x %m {%c}%n";

               PatternLayout layout = new PatternLayout(SYSTEM_LOG_PATTERN);

               DailyRollingFileAppender appender = null;

               appender = new DailyRollingFileAppender(layout, filename, datePattern);

               Logger proxyLogger = Logger.getLogger("SERVICE_LOGGER." + proxy.getName());




1. In the log4j.properties file (located at ESB_HOME/lib) change the log level to DEBUG by setting log4j.category.org.apache.synapse=DEBUG (or any other level as preferred).

2. Edit synapse.properties (located at ESB_HOME/lib/core/WEB-INF/classes) by adding the following line.

3. Complie the observer class and create a jar file that contains it and drop it to the ESB_HOME/repository/components/lib folder.
4. Run your application which calls a proxy service (or simply run sample number 150)
5. The log files will be generated at ESB_HOME/logs/*

All your mediator level logs will also be logged into these files.