简体   繁体   中英

Dynamic fileName File Appender log4j2

I'd like to set dynamically the fileName where the FileAppender will write. I'll provide an example of my configuration.

log4j2.yml

...

  Appenders:

    Console:

      name: Console_Appender

      target: SYSTEM_OUT

      PatternLayout:

        pattern: "[%-5level] %d{dd-MM-yyyy HH:mm:ss.SSS} [%t] %c{1} - %msg%n"

    File:

      name: File_Appender

      fileName: ${log-path}/fileLog.log


      PatternLayout:

        pattern: "[%-5level] %d{dd-MM-yyyy HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
...

I'd like fileLog${javaVariable}.log where javaVariable is dynamic. Is this possible?

Thank you :)

EDIT: To be clear I need that if the method that I intercept with @Around annotation is called by the user0 I have to write the (single) log in "user0.log"; if is called by user1 -> "user1.log". I can create users at any time so I need a dynamic filename.

The solution is MDC. I've used this guide where you can use Routing to select the file according to a variable in the Thread context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM