logginglog4jwso2pattern-layoutwso2-esb

How to pick host name in Per service logs in WSO2 ESB


I am making Per service Logs in WSO2 ESB according to their official link. The layout conversion pattern is working fine but it is not picking up the "HostName". I don't know whats the problem , WSO2 using this pattern itself in "log4j.properties" file. But it is not working in per service logs.

Note: I am using WSO2 ESB 4.8.1

Configurations in "log4j.properties" file:

# Seperate LOG File for MyService Service
log4j.category.SERVICE_LOGGER.MyService=INFO, MyService_PROXY_APPENDER
log4j.additivity.SERVICE_LOGGER.MyService=false
log4j.appender.MyService_PROXY_APPENDER=org.apache.log4j.DailyRollingFileAppender
log4j.appender.MyService_PROXY_APPENDER.File=logs/MyService-service.log
log4j.appender.MyService_PROXY_APPENDER.datePattern='.'yyyy-MM-dd-HH-mm
log4j.appender.MyService_PROXY_APPENDER.layout=org.apache.log4j.PatternLayout
log4j.appender.MyService_PROXY_APPENDER.layout.ConversionPattern=[%d{ISO8601}] - %5p - [%X{host}] - %c{1} - %n

This is creating the separate log file and logging the following output.

Output:

[2015-01-14 12:37:34,063] -  INFO - [] - MyService - 

Problem:

The rest of Conversion specifiers are working fine but [%X{host}] is not working.


Solution

  • I noticed that host and ip MDC entries used by default in log4j conversion patterns (for ERROR_LOGFILE, SERVICE_APPENDER, ... appenders) are not initilized in 4.8.1

    A workaround could be to initialize host's value by yourself, with this script at the start of your mediation for example :

        <script language="js"><![CDATA[
            importPackage(Packages.java.net);
            importPackage(Packages.org.apache.log4j);
            try {
                var addr = InetAddress.getLocalHost();
                if (addr != null) {
                    var hostname = addr.getHostName();
                    MDC.put("host", hostname);
                }
            } catch (e) {
            }
        ]]></script>
    

    But this is just a workaound and I'm interested in a better solution