Hi,
I would hope your hold on your job is not as tenuous as you describe ;(

Here's an approach: in your login code (presuming you have a login page,
filter, servlet, or something like that) or home page, create a new
FileAppender for the user.  Make sure the append property is true so you
don't overwrite older logs, e.g. from yesterday.

I'll make a few assumptions to show this in code, but they're general
enough so that this is realistic/useful:
- You have an HttpSession for the user
- You have a login page called LoginServlet (this can be a filter or a
JSP easily)

So in the login servlet's doGet/doPost method, you'd do something like
this:

// Authenticate user, get a string with the userID
String userId = ...;

// Get the session, from it get a logger for the user
HttpSession session = request.getSession();
String userLoggerName = session.getAttribute("loggerName");
Logger userLogger;

// If the logger doesn't exist, create it
if(userLoggerName == null) {
  userLoggerName = "logger for " + userId;
  // Create the logger and appender for this user
  String fileName = logDirectory + File.separator + userId + ".txt";
  Layout logLayout = ... // Use layout of your choice
  FileAppender userAppender = new FileAppender(logLayout, fileName,
true);
  userLogger = Logger.getLogger(userLoggerName);
  userLogger.addAppender(userAppender);
  userLogger.setAdditivity(false);
  session.setAttribute("loggerName", userLoggerName);
} else {
  userLogger = Logger.getLogger(userLoggerName);
}

// Use it
userLogger.info("User " + userId + " authenticated.");

Other pages and parts of your application just have to do:
String userLoggerName = session.getAttribute("loggerName");
Logger userLogger = Logger.getLogger(userLoggerName);

If you want to hang these loggers off a singleton instead of sessions,
you can, and it's a good idea of you don't want/can't pass sessions
around.

Note that the logger name and not the Logger itself is stored as a
session attribute because Logger is not Serializable.

Finally, note that I don't think this is a great idea because you're
losing some log4j feature like logger hierarchy and class-specific
logging.  But if it's a business requirement, you can't do much about it
I presume.

Log4j 1.3 domains would be nice here as well.

Yoav Shapira
Millennium Research Informatics


>-----Original Message-----
>From: Piyush Kar [mailto:[EMAIL PROTECTED]
>Sent: Friday, April 09, 2004 11:29 AM
>To: [EMAIL PROTECTED]
>Subject: having a separate log file for every client in a J2EE App.
>
>Hi
>
>we have a need in our J2EE App. to be able to have a
>a separate log file for every client . I know this
>sounds weird but ours is an intranet app and we have a
>max. of 20 clients and the business requires  for a
>separate log file for every client .
>
>I am in a dilemma as how to achieve this . I had tried
>an approach by MDC whereby I could log all messages
>coming from every client identified by hostname into a
>single log file but the business users did not like
>the idea . So can any of teh folks out there help me
>with this . I mean how do I go about this ..
>
>Ant tips/code snippets would be real cool. If I dont
>get his knocked out then my job is sure gone !! so
>pleasee folks do help me !!
>
>Thanks
>
>Kar
>
>__________________________________
>Do you Yahoo!?
>Yahoo! Small Business $15K Web Design Giveaway
>http://promotions.yahoo.com/design_giveaway/
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [EMAIL PROTECTED]
>For additional commands, e-mail: [EMAIL PROTECTED]




This e-mail, including any attachments, is a confidential business communication, and 
may contain information that is confidential, proprietary and/or privileged.  This 
e-mail is intended only for the individual(s) to whom it is addressed, and may not be 
saved, copied, printed, disclosed or used by anyone else.  If you are not the(an) 
intended recipient, please immediately delete this e-mail from your computer system 
and notify the sender.  Thank you.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to