Wednesday, June 22, 2016

How to add custom log appender to WSO2 products to directs logs to separate file(take mediator logs to different file)

Implement custom log appender class as follows.

package org.wso2.test.logging;
import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.spi.LoggingEvent;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.utils.logging.LoggingUtils;
import org.wso2.carbon.utils.logging.TenantAwareLoggingEvent;
import java.io.File;
import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedAction;
public class CustomAppender extends DailyRollingFileAppender {
    private static final String LOG_FILE_PATH = org.wso2.carbon.utils.CarbonUtils.getCarbonHome() + File.separator + "repository" +
                                                File.separator + "logs" + File.separator + "messages" + File.separator;
   
    @Override
    protected void subAppend(LoggingEvent loggingEvent) {
        int tenantId = AccessController.doPrivileged(new PrivilegedAction() {
            public Integer run() {
                return CarbonContext.getThreadLocalCarbonContext().getTenantId();
            }
        });
       
        String logFileName = "test_file";
            try {
                this.setFile(LOG_FILE_PATH + logFileName, this.fileAppend, this.bufferedIO, this.bufferSize);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            String serviceName = CarbonContext.getThreadLocalCarbonContext().getApplicationName();
            final TenantAwareLoggingEvent tenantAwareLoggingEvent = LoggingUtils
                    .getTenantAwareLogEvent(loggingEvent, tenantId, serviceName);
            AccessController.doPrivileged(new PrivilegedAction() {
                public Void run() {
                    CustomAppender.super.subAppend(tenantAwareLoggingEvent);
                    return null; // nothing to return
                }
            });
        }
    }
}


Then add following to log4j.properties file.

log4j.logger.org.wso2.test.logging.LoggingClassMediator=INFO, NEW_CARBON_LOGFILE

log4j.appender.NEW_CARBON_LOGFILE=org.wso2.test.logging.CustomAppender
log4j.appender.NEW_CARBON_LOGFILE.File=${carbon.home}/repository/logs/${instance.log}/wso2carbon${instance.log}.log
#log4j.appender.NEW_CARBON_LOGFILE.Append=true
log4j.appender.NEW_CARBON_LOGFILE.layout=org.wso2.carbon.utils.logging.TenantAwarePatternLayout
log4j.appender.NEW_CARBON_LOGFILE.layout.ConversionPattern=TID: [%T] [%S] [%d] %P%5p {%c} - %x %m {%c}%n
log4j.appender.NEW_CARBON_LOGFILE.layout.TenantPattern=%U%@%D [%T] [%S]
log4j.appender.NEW_CARBON_LOGFILE.threshold=INFO
log4j.appender.NEW_CARBON_LOGFILE.MaxFileSize=5kb


Then add org.wso2.test.logging.LoggingClassMediator class to your mediation flow. Please see sample mediator code below.

package org.wso2.test.logging;
import org.apache.log4j.Logger;
import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;
public class LoggingClassMediator extends AbstractMediator {
private static final Logger log = Logger.getLogger(AVSLoggingClassMediator.class);
public LoggingClassMediator() {
}
public boolean mediate(MessageContext mc) {
String apiName = mc.getProperty("SYNAPSE_REST_API").toString();
String name = "APIName::" + apiName+"::";
try{
log.info(name+"LOGGING MESSAGE " + mc.getProperty("RESPONSE_TIME"));
log.info(name+"LOGGING MESSAGE " + mc.getProperty("SYNAPSE_REST_API"));
}catch(Exception e){
log.error(name+"ERROR :",e);
}
return true;
}

No comments:

Post a Comment