Search code examples
loggingwso2wso2-api-managerwso2-enterprise-integratorwso2-micro-integrator

Is there any way to log XML or JSON in one line in WSO2 Microintegrator


This is the log output in the console in Integration Studio

2022-11-17 16:32:05 DEBUG Service: - To: /services/Service, WSAction:XYS Direction: request, Request = ======== Log Request =======, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ><soapenv:Body>
      <Read>
         <filter>
            <Field>XYS</Field>
         </filter>
      </Read>
   </soapenv:Body></soapenv:Envelope>

I didn't find any out-of-the-box solution.


Solution

  • Here are two ways you can achieve this without writing any code.

    Option 01: Global Solution

    Update your CARBON_CONSOLE and CARBON_LOGFILE appenders in <MI_HOME>/conf/log4j2.properties to something like the ones below.

    # CARBON_CONSOLE is set to be a ConsoleAppender using a PatternLayout.
    appender.CARBON_CONSOLE.type = Console
    appender.CARBON_CONSOLE.name = CARBON_CONSOLE
    appender.CARBON_CONSOLE.layout.type = PatternLayout
    appender.CARBON_CONSOLE.layout.pattern = [%d] %5p {%c{1}} - %replace{%msg}{[\r\n]+}{}%ex%n
    appender.CARBON_CONSOLE.filter.threshold.type = ThresholdFilter
    appender.CARBON_CONSOLE.filter.threshold.level = DEBUG
    
    # CARBON_LOGFILE is set to be a DailyRollingFileAppender using a PatternLayout.
    appender.CARBON_LOGFILE.type = RollingFile
    appender.CARBON_LOGFILE.name = CARBON_LOGFILE
    appender.CARBON_LOGFILE.fileName = ${sys:carbon.home}/repository/logs/wso2carbon.log
    appender.CARBON_LOGFILE.filePattern = ${sys:carbon.home}/repository/logs/wso2carbon-%d{MM-dd-yyyy}.log
    appender.CARBON_LOGFILE.layout.type = PatternLayout
    appender.CARBON_LOGFILE.layout.pattern = [%d] %5p {%c} - %replace{%msg}{[\r\n]+}{}%ex%n
    appender.CARBON_LOGFILE.policies.type = Policies
    appender.CARBON_LOGFILE.policies.time.type = TimeBasedTriggeringPolicy
    appender.CARBON_LOGFILE.policies.time.interval = 1
    appender.CARBON_LOGFILE.policies.time.modulate = true
    appender.CARBON_LOGFILE.policies.size.type = SizeBasedTriggeringPolicy
    appender.CARBON_LOGFILE.policies.size.size=10MB
    appender.CARBON_LOGFILE.strategy.type = DefaultRolloverStrategy
    appender.CARBON_LOGFILE.strategy.max = 20
    appender.CARBON_LOGFILE.filter.threshold.type = ThresholdFilter
    appender.CARBON_LOGFILE.filter.threshold.level = DEBUG
    

    If you just want to update the console log just update the CONSOLE appender. Remember this will remove all the new lines from all log messages, not just the messages from the Log Mediator, also this will not affect any stack-trace logs, etc. So I don't really see any issue using this approach.

    Option 02: Log Mediator-Specific Solution

    When you are logging do not rely on the Log level full to log the Payload, instead simply Log the message body as seperate property. While logging replace the newline characters. In the below example, I'm using XPATH 2.0 replace function for this. So if you have not enabled XPATH 2.0 in Micro Integrator add the following to the deployment.toml.

    [mediation]
    synapse.enable_xpath_dom_failover="true"
    

    Then in your Log Mediator use something like the below.

     <log level="custom">
         <property name="MESSAGE_BODY" expression="fn:replace($ctx:body, '\n', '')" scope="default" type="STRING" xmlns:fn="http://www.w3.org/2005/xpath-functions" />
     </log>