Enabling per-API Logs for APIs
The advantage of having per-API log files is that it is very easy to analyze/monitor what went wrong in a particular REST API defined in the ESB profile by looking at the log files. The API log is an additional log file, which will contain a copy of the logs to a particular REST API.
Below are the configuration details to configure the logs of a REST API called TestAPI
using log4j2
properties.
Open <EI_HOME>/conf/log4j2.properties
file using your favorite text editor to configure log4j
to log the API specific logs to a file. You can configure the logger for either INFO level logs or DEBUG level logs as follows:
Open up the
log4j2.properties
file found in theconf
directory of the WSO2 EI installation directory using your favorite text editor and add the following log appender.#Per API appender appender.TestAPI_LOGGER.type = RollingFile appender.TestAPI_LOGGER.name = TestAPI_LOGGER appender.TestAPI_LOGGER.fileName = ${sys:carbon.home}/repository/logs/TestAPI.log appender.TestAPI_LOGGER.filePattern = ${sys:carbon.home}/repository/logs/TestAPI-%d{MM-dd-yyyy}.log appender.TestAPI_LOGGER.layout.type = PatternLayout appender.TestAPI_LOGGER.layout.pattern = TID: [%tenantId] [%d] %5p {%c} - %m%ex%n appender.TestAPI_LOGGER.policies.type = Policies appender.TestAPI_LOGGER.policies.time.type = TimeBasedTriggeringPolicy appender.TestAPI_LOGGER.policies.time.interval = 1 appender.TestAPI_LOGGER.policies.time.modulate = true appender.TestAPI_LOGGER.policies.size.type = SizeBasedTriggeringPolicy appender.TestAPI_LOGGER.policies.size.size=10MB appender.TestAPI_LOGGER.strategy.type = DefaultRolloverStrategy appender.TestAPI_LOGGER.strategy.max = 20 appender.TestAPI_LOGGER.filter.threshold.type = ThresholdFilter appender.TestAPI_LOGGER.filter.threshold.level = INFO
Define a logger referring to the appender . Add the following section to the end of the file to configure the logger for log messages where the Log Category is INFO.
logger.API_LOGGER_TestAPI.name = API_LOGGER.TestAPI
Here TestAPI is the name of the API which you have created.
#Logger for per API logger.API_LOGGER_TestAPI.name = API_LOGGER.TestAPI logger.API_LOGGER_TestAPI.level = INFO logger.API_LOGGER_TestAPI.additivity = false logger.API_LOGGER_TestAPI.appenderRef.TestAPI_LOGGER.ref = TestAPI_LOGGER
The in-sequence of the REST API will need to contain a Log mediator with the Log Category defined as INFO to be able to view logs in the log file.
Add your appender name into the section called "list of all appenders" as following manner.
# list of all appenders #add entry "syslog" to use the syslog appender appenders = CARBON_CONSOLE, CARBON_LOGFILE, AUDIT_LOGFILE, ATOMIKOS_LOGFILE, CARBON_TRACE_LOGFILE, osgi, SERVICE_LOGFILE, ERROR_LOGFILE, TestAPI_LOGGER
Add your logger name into the section called “loggers” as following manner.
loggers = AUDIT_LOG, SERVICE_LOGGER, trace-messages, org-apache-coyote, com-hazelcast, Owasp-CsrfGuard, \ org-apache-axis2-wsdl-codegen-writer-PrettyPrinter, org-apache-axis2-clustering, org-apache-catalina, \ org-apache-tomcat, org-wso2-carbon-apacheds, org-apache-directory-server-ldap, \ org-apache-directory-server-core-event, com-atomikos, org-quartz, org-apache-jackrabbit-webdav, org-apache-juddi, \ org-apache-commons-digester-Digester, org-apache-jasper-compiler-TldLocationsCache, org-apache-jasper-servlet-TldScanner, org-apache-qpid, \ org-apache-qpid-server-Main, qpid-message, qpid-message-broker-listening, ca-uhn-hl7v2, org-apache-tiles, \ org-apache-commons-httpclient, org-apache-solr, me-prettyprint-cassandra-hector-TimingLogger, \ org-apache-axis-enterprise, org-apache-directory-shared-ldap, org-apache-directory-server-ldap-handlers, \ org-apache-directory-shared-ldap-entry-DefaultServerAttribute, org-apache-directory-server-core-DefaultDirectoryService, \ org-apache-directory-shared-ldap-ldif-LdifReader, org-apache-directory-server-ldap-LdapProtocolHandler, \ org-apache-directory-server-core, org-apache-directory-server-ldap-LdapSession, DataNucleus, Datastore, Datastore-Schema, \ JPOX-Datastore, JPOX-Plugin, JPOX-MetaData, JPOX-Query, JPOX-General, JPOX-Enhancer, org-apache-hadoop-hive, hive, ExecMapper, \ ExecReducer, net-sf-ehcache-config-ConfigurationFactory, axis2Deployment, equinox, tomcat2, StAXDialectDetector, \ org-apache-synapse, org-apache-synapse-transport, org-apache-axis2, org-apache-axis2-transport, org-wso2-carbon, synapse-transport-http-access, API_LOGGER_TestAPI
The above configuration creates a log file names
TestAPI.log
in the folder<EI_HOME>/repository/logs
.The in-sequence of the REST API will need to contain a Log mediator with the Log Category defined as DEBUG to be able to view logs in the log file.