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')"/>
    </log>

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.category.SERVICE_LOGGER.TestProxy=INFO, PROXY_APPENDER
log4j.additivity.PROXY_APPENDER=false
log4j.appender.PROXY_APPENDER=org.apache.log4j.DailyRollingFileAppender
log4j.appender.PROXY_APPENDER.File=${carbon.home}/repository/logs/${instance.log}/wso2-esb-test-proxy${instance.log}.log
log4j.appender.PROXY_APPENDER.Append=true
log4j.appender.PROXY_APPENDER.layout=org.apache.log4j.PatternLayout
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 {
                       setLogger(proxy);

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

       public void proxyServiceRemoved(ProxyService proxy) {
               try {
                       setLogger(proxy);
               } 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());

               proxyLogger.setLevel(Level.ALL);

               proxyLogger.setAdditivity(false);
               proxyLogger.addAppender(appender);

       }
}

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.
synapse.observers=org.wso2.carbon.customobserver.CustomSynapseObserverForLogging

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.

Thursday, October 6, 2011

More on deploying apps in Stratos Live

September has been an amazing month for WSO2. We had the WSO2Con 2011 and it was a smashing success with amazing keynotes, tech talks and really cool networking events. We received wonderful feedback and folks are already looking forward to WSO2Con 2012 :) ! So if you missed the conference this time make sure to be there next year, because it's just going to be one amazing conference, guaranteed!

I was hoping to write some more about deploying applications in Stratos Live as a follow up to my last blog post. What I did there was just show you how to deploy a really simple web app. There's so much more that you can do with all the services offered in Stratos Live. I came across a slide deck shared by Chintana Wilamuna at WSO2 that explains how to create a cool web application in Stratos Live with the use of the Application Server, Data Services Server and Carbon Studio.

The slide share contains steps to use the Relational Storage Service in the Data Services Server as a Service to create a database that can be hosted in the cloud. You'll be able to follow easy steps on how to create users and groups and set permissions for them. Next up, you'll learn how to create tables and perform database operations via data services. You'll learn how to create data services in the Data Services Server and how to invoke them via a web application that will be hosted in the Application Server in Stratos. He has used Carbon Studio to create the web application.