How to create a rolling file appender plugin in log4j2

这一生的挚爱 提交于 2019-12-03 21:30:54

The application you are upgrading uses Apache Commons Logging, and there is no ThreadContext map in the Commons Logging API.

However, you can accomplish your object by simply using the log4j2 ThreadContext map in your application. That way you don't need any custom log events or appender subclasses.

There should only be a few places in your application where the userID is set or modified. In those places, add these lines of code:

int userId = //get user Id
String appplicationName = //get application name
ThreadContext.put("userID", String.valueOf(userId));
ThreadContext.put("appplicationName", appplicationName);
// ... your business logic

If Commons Logging is delegated to log4j2, you can configure a pattern layout like: "%-5p [%t] %c: %X{userID}/%{appplicationName} %m%n" to make your values appear in the log.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!