March 13, 2021
WSO2 API Manager now supports (from v3.0.0 onwards) Log4j2 to manage logs and appenders. In this cheat, we will be looking at how to configure a custom appender for a simple requirement.
The requirement is to log the Synapse Wire logs in a seperate log file. So, first, lets enable the Synapse Wire DEBUG logs in the API Manager server.
The following configurations and steps are verified in API Manager
v3.0.0,v3.1.0, andv3.2.0environments
To enable the Synapse Wire logs in API Manager server, navigate to <apim>/repository/conf directory and open the log4j2.properties. Then, add the mentioned properties (if not already exist) and set the DEBUG level.
Then, add the synapse-wire keyword to the loggers as mentioned. Refer to Enable Gateway Wire Logs for more info.
Now, let’s introduce a Log4j2 appender to log the Synapse Wire to a separate log file.
Given is a sample Log4j2 Appender configuration with RollingFile. The presented configuration contains the basic set of patterns and layouts to print logs. You can further enhance the configurations according to your need.
Once the appender is configured, the Appender name needs to be added to the appenders property in the log4j2.properties.
At last, configure the synapse-wire logger configuration with the created WIRE_LOG appender as given.
Once the configurations are saved, we will see a new log file created with the name wire.log inside the <apim>/repository/logs directory containing the Wire logs.
We have now successfully configured a Log4j2 Appender to create and log the Synapse Wire DEBUG logs in a separate file named wire.
The complete set of log4j2 configurations that we introduced and used in this cheat is given below.
# appender declarationappenders = WIRE_LOG, ...# custom appenderappender.WIRE_LOG.type = RollingFileappender.WIRE_LOG.name = WIRE_LOGappender.WIRE_LOG.fileName = ${sys:carbon.home}/repository/logs/wire.logappender.WIRE_LOG.filePattern = ${sys:carbon.home}/repository/logs/wire-%d{MM-dd-yyyy}.logappender.WIRE_LOG.layout.type = PatternLayoutappender.WIRE_LOG.layout.pattern = TID: [%tenantId] [%appName] [%d] %5p {%c} - %m%ex%nappender.WIRE_LOG.policies.type = Policiesappender.WIRE_LOG.policies.time.type = TimeBasedTriggeringPolicyappender.WIRE_LOG.policies.time.interval = 1appender.WIRE_LOG.policies.time.modulate = trueappender.WIRE_LOG.policies.size.type = SizeBasedTriggeringPolicyappender.WIRE_LOG.policies.size.size = 10MB# engaging the synapse-wire loggerloggers = synapse-wire, ...# enabling Synapse Wire DEBUGlogger.synapse-wire.name = org.apache.synapse.transport.http.wirelogger.synapse-wire.level = DEBUG# configuring the logger with the appenderlogger.synapse-wire.appenderRef.WIRE_LOG.ref = WIRE_LOGlogger.synapse-wire.additivity = false