且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在log4j2中创建滚动文件appender插件

更新时间:2023-02-01 23:30:18

您要升级的应用程序使用Apache Commons记录,Commons Logging API中没有ThreadContext映射。

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

但是,您只需在应用程序中使用log4j2 ThreadContext映射即可完成对象。这样您就不需要任何自定义日志事件或appender子类。

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.

应用程序中应该只有少数位置可以设置或修改userID。在这些地方,添加以下代码行:

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

如果将Commons Logging委托给log4j2,那么你可以配置一个模式布局,如:% - 5p [%t]%c:%X {userID} /%{appplicationName}%m%n来制作你的价值观出现在日志中。

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.