Monday, July 4, 2016

How to enable per API logging in WSO2 API Manager - Create log file per API


 Though the log4j.properties file it is possible to control logging behavior per API as it create separate log file per API, define log category per proxy etc. What you need to do is add following to log4j.properties file and restart server.
If need you can add this once to file and update log level etc from management console.

log4j.appender.TestAPI_APPENDER=org.apache.log4j.RollingFileAppender
log4j.appender.TestAPI_APPENDER.File=${carbon.home}/repository/logs/${instance.log}/wso2-APILogs-service${instance.log}.log
log4j.appender.TestAPI_APPENDER.MaxFileSize=1000KB
log4j.appender.TestAPI_APPENDER.MaxBackupIndex=10
log4j.appender.TestAPI_APPENDER.layout=org.apache.log4j.PatternLayout
log4j.appender.TestAPI_APPENDER.layout.ConversionPattern=%d{ISO8601} [%X{ip}-%X{host}] [%t] %5p %c{1} %m%n
log4j.category.API_LOGGER.admin--CalculatorAPI= TRACE,  TestAPI_APPENDER


If you enabled it successfully then when you invoke APIs it will create wso2-APILogs-service.log file in repository/logs directory. And you can see API specific invocation details will add to that log file.  Content would be something like below.

2016-07-04 14:26:29,599 [-] [localhost-startStop-1]  INFO admin--CalculatorAPI Initializing API: admin--CalculatorAPI:v1.0
2016-07-04 14:26:57,546 [-] [PassThroughMessageProcessor-8]  WARN admin--CalculatorAPI ERROR_CODE : 0 ERROR_MESSAGE : Access-Control-Allow-Headers:authorization,Access-Control-Allow-Origin,Content-Type,Access-Control-Allow-Methods:GET,Access-Control-Allow-Origin:*, Unexpected error sending message back
2016-07-04 14:26:57,548 [-] [PassThroughMessageProcessor-8]  WARN admin--CalculatorAPI Executing fault sequence mediator : fault
2016-07-04 14:26:57,592 [-] [PassThroughMessageProcessor-8]  INFO admin--CalculatorAPI STATUS = Executing default 'fault' sequence, ERROR_CODE = 0, ERROR_MESSAGE = Access-Control-Allow-Headers:authorization,Access-Control-Allow-Origin,Content-Type,Access-Control-Allow-Methods:GET,Access-Control-Allow-Origin:*, Unexpected error sending message back

1 comment:

  1. Hi Sanjeewa,

    Try to log per API by a tenant but it did not work.
    I created the tenant as test1.com, tenant user as admin, create an API (tenantapi) and subscribed to it.

    in the log4j.properties, i added the following
    log4j.appender.TestAPI_APPENDER=org.wso2.carbon.utils.logging.appenders.CarbonDailyRollingFileAppender
    log4j.appender.TestAPI_APPENDER.File=${carbon.home}/repository/logs/${instance.log}/wso2-APILogs-service${instance.log}.log
    log4j.appender.TestAPI_APPENDER.Append=true
    log4j.appender.TestAPI_APPENDER.layout=org.wso2.carbon.utils.logging.TenantAwarePatternLayout
    log4j.appender.TestAPI_APPENDER.layout.ConversionPattern=%d{ISO8601} [%X{ip}-%X{host}] [%t] %5p %c{1} %m%n
    log4j.appender.TestAPI_APPENDER.layout.TenantPattern=%U%@%D [%T] [%S]
    log4j.category.API_LOGGER.admin@test1.com--tenantapi= TRACE, TestAPI_APPENDER
    log4j.appender.TestAPI1_APPENDER.threshold=TRACE

    and restart the server. In API in-flow, I added an INFO log. This gets logged to the wso2carbon.log but not to the file specified above. I tried the above APPENDER with the values for org.apache.log4j.RollingFileAppender and org.wso2.carbon.utils.logging.appenders.CarbonDailyRollingFileAppender. Have I missed anything?

    ReplyDelete