Search code examples
wso2-api-managerwso2

How to add custom log file for every day entries in wso2 apim?


I am trying to configure gateway access log of wso2 apim (4.0.0) to be written to a separate log file every day so that it should contain api username and api name in a log file. To form the structure of the log file I followed the answer of following question.

The log file structure I have as below:

datetime | remoteIp | username | invoked_api_name | api_url | request | response

Now all entries is being written in wso2carbon.log

I would like it to be written in a file with following pattern:

custom_access_log_gwyyyy-mm-dd.log

Any help is welcome!


Solution

  • You can introduce an extra Log Appender to log the specific Handler logs in it. Find sample instructions below

    • Open the <apim>/repository/conf/log4j2.properties and add the following to create a Log Appender

      appender.APIHANDLER_LOG.type = RollingFile
      appender.APIHANDLER_LOG.name = APIHANDLER_LOG
      appender.APIHANDLER_LOG.fileName = ${sys:carbon.home}/repository/logs/api-log.log
      appender.APIHANDLER_LOG.filePattern = ${sys:carbon.home}/repository/logs/api-log-%d{MM-dd-yyyy}.log
      appender.APIHANDLER_LOG.layout.type = PatternLayout
      appender.APIHANDLER_LOG.layout.pattern = TID: [%tenantId] [%appName] [%d] %5p {%c} - %m%ex%n
      appender.APIHANDLER_LOG.policies.type = Policies
      appender.APIHANDLER_LOG.policies.time.type = TimeBasedTriggeringPolicy
      appender.APIHANDLER_LOG.policies.time.interval = 1
      appender.APIHANDLER_LOG.policies.time.modulate = true
      appender.APIHANDLER_LOG.policies.size.type = SizeBasedTriggeringPolicy
      appender.APIHANDLER_LOG.policies.size.size=10MB
      appender.APIHANDLER_LOG.strategy.type = DefaultRolloverStrategy
      appender.APIHANDLER_LOG.strategy.max = 20
      appender.APIHANDLER_LOG.filter.threshold.type = ThresholdFilter
      appender.APIHANDLER_LOG.filter.threshold.level = DEBUG
      
    • Add the created Appender to the appenders property at the top of the log4j2.properties

      appenders=APIHANDLER_LOG, CARBON_CONSOLE, ..
      
    • Configure your package to log into the new Appender

      logger.api-log-handler.name = com.sample.handlers.APILogHandler
      logger.api-log-handler.level = DEBUG
      logger.api-log-handler.appenderRef.APIHANDLER_LOG.ref = APIHANDLER_LOG
      logger.api-log-handler.additivity = false
      
      loggers = api-log-handler, AUDIT_LOG, ...
      

    Save the configurations and invoke the API. Now the logs will be printed to a file called api-log.log.