-1

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!

AndréB
  • 65
  • 7
  • I believe that [this answer](https://stackoverflow.com/a/43404124/3284624) will illustrate how you can achieve what you want. You will have to tweak it to use variables for the file name instead of the directory but that's a minor change. You can also read the [FAQ page associated with log4j2](https://logging.apache.org/log4j/2.x/faq.html#separate_log_files) – D.B. Nov 12 '17 at 03:12
  • Thanks for this, D.B. I can't comment on the other thread (lack of reputation, it just went down again) - could it be that there's a typo in the XML? appender-ref instead of AppenderRef? – AndréB Nov 13 '17 at 14:55
  • Hmm I see what you mean, it could be a typo. I'll try running it again both ways and see what happens. – D.B. Nov 13 '17 at 15:24
  • Maybe it's just another way of setting attributes. I've seen something similar in the FAQ (`...` instead of ``) – AndréB Nov 13 '17 at 15:26
  • It appears to to be valid, I am able to run that code successfully using that config file. If I change to AppenderRef it still works fine so must just be an alternate syntax. – D.B. Nov 13 '17 at 22:47

1 Answers1

1

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.
AndréB
  • 65
  • 7