1) Using SynapseObserver to log proxy service logs to separate file
Create a
CustomSynapseObserver.java class by extending the
AbstractSynapseObserver.java.
package com.custom.synapseobserver; import java.io.IOException; 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; import org.apache.synapse.Mediator; import org.apache.synapse.config.AbstractSynapseObserver; import org.apache.synapse.core.axis2.ProxyService; import org.apache.synapse.mediators.base.SequenceMediator; public class CustomSynapseObserver extends AbstractSynapseObserver { private static final Log log = LogFactory .getLog(CustomSynapseObserver.class); public void proxyServiceAdded(ProxyService proxy) { try { log.info("-------------------- Custom Observer: Added Proxy Invoked---------------------------"); addToLog(proxy.getName()); } catch (IOException e) { log.error( "CustomProxyObserver could not set service level logger for the proxy : " + proxy.getName(), e); } } private void addToLog(String name) throws IOException { String filename = "logs/" + name + ".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." + name); proxyLogger.setLevel(Level.ALL); proxyLogger.setAdditivity(false); proxyLogger.addAppender(appender); } }
Use the below
pom.xml and create a maven project and build the jar.
4.0.0 com.custom.synapseobserver CustomSynapseObserver 0.0.1 CustomSynapseObserver CustomSynapseObserver org.apache.synapse synapse-core 2.1.3-wso2v11 commons-logging commons-logging 1.1.1 true daily ignore wso2-nexus http://maven.wso2.org/nexus/content/groups/wso2-public/ true daily ignore wso2-nexus http://maven.wso2.org/nexus/content/groups/wso2-public/
Copy the jar into
ESB_HOME/repository/compoenents/lib.
To define the
CustomSynapseObserver, open the
ESB_HOME/repository/conf/synapse.properties file and add the
following line:
synapse.observers=com.custom.synapseobserver.CustomSynapseObserver
Now restart the
server and the custom synapse observer is ready to use.
2) Using log4j.properties to log API service logs to separate file
To log the API's we need to go with log4j.properties. Here the API_LOGGER implementation helps us to seperate each API logs into a file. Below is the sample configuration we can set in log4j.properties. But in this case, there is a concern that the API names to be identified early and need to configure it in the lg4j.properties.
log4j.logger.API_LOGGER = DEBUG, API log4j.logger.API_LOGGER.test1API=DEBUG, TestAPI1 log4j.logger.API_LOGGER.test2API=DEBUG, TestAPI2 log4j.appender.API=org.apache.log4j.DailyRollingFileAppender log4j.appender.API.Append = true log4j.appender.API.File=${carbon.home}/repository/logs/api.log log4j.appender.API.layout=org.apache.log4j.PatternLayout log4j.appender.API.layout.ConversionPattern=TID: [%d] %5p {%c} - %x %m {%c}%n log4j.appender.TestAPI1=org.apache.log4j.DailyRollingFileAppender log4j.appender.TestAPI1.Append = true log4j.appender.TestAPI1.File=${carbon.home}/repository/logs/test1API.log log4j.appender.TestAPI1.layout=org.apache.log4j.PatternLayout log4j.appender.TestAPI1.layout.ConversionPattern=TID: [%d] %5p {%c} - %x %m {%c}%n log4j.appender.TestAPI2=org.apache.log4j.DailyRollingFileAppender log4j.appender.TestAPI2.Append = true log4j.appender.TestAPI2.File=${carbon.home}/repository/logs/test2API.log log4j.appender.TestAPI2.layout=org.apache.log4j.PatternLayout log4j.appender.TestAPI2.layout.ConversionPattern=TID: [%d] %5p {%c} - %x %m {%c}%n
References
[1] https://docs.wso2.com/display/ESB490/Sample+651%3A+Using+Synapse+Observers
[2] http://synapse.apache.org/Synapse_Extending.html
[3] http://dakshithar.blogspot.com/2012/03/logging-with-wso2-esb.html