首页 > 解决方案 > How to enable application logs in wso2?

问题描述

How can I enable my application logs (API published in WSO2) in APIM_HOME>/repository/conf/log4j2.properties file? I went through the 3.0.0 documentation (https://apim.docs.wso2.com/en/latest/Administer/ProductAdministration/Monitoring/Logging/setting-up-logging/#setting-up-logging-in-api-manager) and tried to configure logging for my app component, but I'm unable to see the application specific logs in wso2carbon.log.

By using custom mediators I am able to log the complete request/response but I am looking to log all the "log.xxx()" lines I have in my code, based on the log level I set in the log4j2.properties file in WSO2. I use slf4j logger in code so I tried enabling logging for "org.slf4j" package and also tried enabling for my application's specific package, but neither worked (the "applogger" below was added to list of comma separated loggers in the same file:

# application logs
logger.applogger.name = org.slf4j
logger.applogger.level = DEBUG

logger.applogger.name = my.application.package
logger.applogger.level = DEBUG

I also checked these (1, 2) logging related questions on SO but couldn't find a definitive answer that solved my problem. When I tried following this link to enable message tracing from carbon console I get the below error:

error
Cannot set eventing configuration. Backend server may be unavailable.; nested exception is:
org.apache.axis2.AxisFault: unknown - An error has occurred. Please refer the logs for more details.

in logs:

TID: [-1234] [] [2020-02-11 04:56:48,913] ERROR {org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver} - Exception occurred while trying to invoke service method configureEventing java.lang.NullPointerException
        at org.wso2.carbon.analytics.message.tracer.handler.conf.RegistryPersistenceManager.updateConfigurationProperty(RegistryPersistenceManager.java:84)
.............................
TID: [-1234] [] [2020-02-11 04:56:48,922] ERROR {org.wso2.carbon.analytics.message.tracer.handler.ui.MessageTracerHandlerAdminClient} - Cannot set eventing configuration. Backend server may be unavailable. org.apache.axis2.AxisFault: unknown
        at org.apache.axis2.util.Utils.getInboundFaultFromMessageContext(Utils.java:531)

标签: loggingwso2log4j2slf4jwso2carbon

解决方案


通过使用自定义中介,我可以记录完整的请求/响应,但我希望根据我在 log4j2.properties 文件中设置的日志级别记录我的代码中的所有“log.xxx()”行WSO2。我在代码中使用 slf4j 记录器,因此我尝试为“org.slf4j”包启用日志记录,并尝试为我的应用程序的特定包启用日志记录,但均未成功(下面的“applogger”已添加到同一文件中的逗号分隔记录器列表中:

logger.applogger.name = org.slf4j

logger.applogger.level = 调试

logger.applogger.name = my.application.package

logger.applogger.level = 调试

好像您重复添加了相同的名称。添加以下您的应用程序记录器,如下所示:

logger.applogger.name = my.application.package 
logger.applogger.level = DEBUG

并将其设置为记录器配置:

loggers = AUDIT_LOG, trace-messages, org-apache-coyote, com-hazelcast, Owasp-CsrfGuard, org-apache-axis2-wsdl-codegen-writer-PrettyPrinter.... org-wso2-carbon-apimgt-gateway-mediators-BotDetectionMediator,correlation, JAGGERY_LOG, applogger

您不需要将记录器名称设置为org.slf4j. 您应该只指定组件的包名称。检查后端实现是否使用 log4J2 进行日志记录。如果不是,则可能由于依赖版本不匹配而出现问题。


推荐阅读