What would be a good way to create different log files for different users with log4j version 1.x?
Asked
Active
Viewed 139 times
0
-
What you mean different users? by http session? ldap session? sso? – HRgiger Nov 25 '16 at 16:37
-
I manage approximately 10 TCP connections and use 5 Threads for each connection (50 Threads in total). I would like to have a separate log file for each connection. – David Nov 25 '16 at 16:55
1 Answers
0
You can do something like that:
String user = ... // Should be a valid file name, so eventually
// handle special characters
FileAppender fa = new FileAppender();
fa.setName("FileLogger");
fa.setFile(user + ".log");
fa.setLayout(new PatternLayout("%d %-5p [%c{1}] %m%n"));
fa.setThreshold(Level.DEBUG);
fa.setAppend(true);
fa.activateOptions();
Logger userLogger = LoggerFactory.getLogger("LoggerName");
userLogger.addAppender(fa);

Davide Lorenzo MARINO
- 26,420
- 4
- 39
- 56
-
Suppose I have 10 TCP connections, 5 threads per connection, i.e., 50 threads in total, and perhaps 20 different classes that are executed in those 50 threads. I cannot really see how your suggested answer helps. – David Nov 25 '16 at 16:59
-
You asked how to write logs on different files for each user. This code write logs on different files for each user. Don't know if this can help you, but this is what you asked, or at least what I understood from *create different log files for different users* – Davide Lorenzo MARINO Nov 25 '16 at 17:04
-
I guess my question is a little bit imprecise. I'm sorry about that. What I mean is the following: I have many classes (with one logger per class), many threads, and many users executing the code (similar to a web server) and I need to make sure that all code that is executed by one user is logged to the same file. I saw another related questions for log4j version 2.x, but I am not sure how to realize this with log4j version 1.x: http://stackoverflow.com/questions/25114526/log4j2-how-to-write-logs-to-separate-files-for-each-user – David Nov 25 '16 at 18:07