How to create a rolling file appender plugin in log4j2

前端 未结 1 1023
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-03 12:34

I want to create a custom log4j2 rolling file appender. I need to create this custom appender because I want to wrap the log4j log event with some information unique to my a

相关标签:
1条回答
  • 2021-01-03 13:05

    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.

    0 讨论(0)
提交回复
热议问题