Search code examples
log4j2appendervaadin8

Log4j2 one file per user session


In my vaadin application, I'd like to create one log file per user session. I started to create a customer file appender but then decided to investigate on ThreadContexts a bit more. I now store the user's uid inside the ThreadContext, which works fine within my PatternLayout (%X{uid}), but unfortunately not inside the FileAppender (maybe I was a bit naive).

Which way would you go for something like this? Simply put, I'd like to get this result for the time being:

  • User not logged in -> filename=debug.log
  • User logged in -> filename=[username]_debug.log

Any ideas or pointers highly appreciated!


Solution

  • Problem solved and lessons learned:

    • To write to different log files, I needed Routers, as described in the FAQ. No clue why I didn't come across this while googling.
    • There's no need for a custom appender to solve this problem.
    • Log4j2 is very, very picky about directories not being writable. If this occurs, scroll up in the stacktrace: ERROR Unable to invoke factory method in class org.apache.logging.log4j.core.appender.RollingFileAppender. You'll find the culprit, google won't.
    • As well, Log4j2 cares in which order the appenders appear. It's stated multiple times in the FAQ and in the appender docs, still I overread it multiple times.