I would work backwards. Unique logs files are created by unique appenders.
As far as I know a file appender can only write to a single file; therefore,
you require unique appenders for each unique, username based, log file you
wish to write to. I believe that such an architecture is resource
contstrained. If instead you wrote any logging event out to the same file,
but with a field that identified the current username, you could scale much
better (you only ever have one file open, not 'n' different file handles).
 
I sympathize with your statement that a DB may be too much overhead, how
about a simple shell script that finds all entries in a log file with a
username:
 
  grep username log.txt
 
So now the remaining piece would be to only log for a given set of userids.
This can be accomplished using the log4j filter api. You can accept, deny,
or be neutral to any incoming logging event. In your case it sounds like you
would need to write a filter that loads a configuration/properties file that
lists the usernames. Then if the NDC.peek() value of the current logging
event is contained in your configuration file, go ahead and accept it. It
will then get written out to the single log file. Use the PatternLayout to
write out the current NDC.
 
So the only piece of code you need to write is:
 
1. A servlet filter that sets up the NDC with the current username
2. A log4j filter that accepts the current logging event only if the
NDC.peek() value is contained in your configuration file.
3. A log4j configuration file that logs all records with the current NDC
value (see PatternLayout).
 
 
 

Naresh Sikha 

-----Original Message-----
From: Srinivas Kotamraju [mailto:[EMAIL PROTECTED] 
Sent: Monday, April 26, 2004 1:08 PM
To: Sikha, Naresh
Subject: RE: logging per user


Hi naresh,
Thanks for the info.I only need the information for each user in a file not
in DB..I kind of follow your idea..but do I have to still create as many
appenders as the number of users I have.? Ideally I would like to just keep
the userids in a text file and by adding or removing a userid in the text
file, I would like to enable logging for that user.
Thanks

"Sikha, Naresh" <[EMAIL PROTECTED]> wrote:

An alternative means would be to use NDC.

So your logging methods would look the same as before:

private static final Logger logger =
Logger.getLogger(TheCurrentClass.class);

if (logger.isInfoEnabed()) {
..
}

But before any logs are created for the current request, identify the
current user of your app, ideally in a Servlet filter or other top level
location.

public void doFilter() {
String username = // get user name from incoming servlet request
NDC.push(username);
try {
chain.doFilter(request, response);
} finally {
// Always clean up user information attached to the current thread
NDC.clear();
}
}

Finally, to make all this setup provide the data you want you will need a
custom Log4J filter that accepts the current LoggingEvent if the value of
NDC.peek() is equal to the specified use! rname that the current log file
represents.

There may be additional work configuring many file appenders, one for each
user - have you thought about just writing the username in every logging
event and then correlating them offline (in a database for example)?

Cheers.

Naresh Sikha 



-----Original Message-----
From: James Stauffer [mailto:[EMAIL PROTECTED] 
Sent: Monday, April 26, 2004 10:46 AMM
To: 'Log4J Users List'
Subject: RE: logging per user


You could do:
Logger log = Logger.getLogger(username);
Log.info("User: " + username);

James Stauffer



-----Original Message-----
From: Srinivas Kotamraju [mailto:[EMAIL PROTECTED] 
Sent: Monday, April 26, 2004 12:45 PM
To: [EMAIL PROTECTED]
Subject: logging per user


Hi guys,
I am a newbie to log4j. I quickly looked through log4j and it really looks
great and I amazed by its flexibility. I am ha! ving a web application and I
have a bunch of users 20 -25..using it. I would like to be able to have one
log file per user..with an option to configure the users in a properties
file. The logfile will be generated only for the configured users. I have
been looking at NDC..is that the one I should be using? 
NDC.clear();
NDC.push(userName);
But how would a separate file created for each user?

Can someone please provide me some examples..
Thanks


---------------------------------
Do you Yahoo!?
Yahoo! Photos: High-quality 4x6 digital prints for 25¢

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



  _____  

Do you Yahoo!?
Yahoo! Photos: High-quality
<http://pa.yahoo.com/*http://us.rd.yahoo.com/evt=23765/*http://photos.yahoo.
com/ph/print_splash> 4x6 digital prints for 25¢

Reply via email to