Wouldn't it be possible to use Logger.log(Priority,Object) instead of e.g.
Logger.debug(Object)? In this case you could set Priority depending on user and
process/program/class and pass it along with the message object to log4j.
You would than have only one logger per application (thus saving memory) and the log4j
properties stay the same.
String sUser = "XYZ" ;
String sApplication = "apps.app2") ;
Logger applog = Logger.getLog(sApplication );
applog.log(MyPriorityFactory.getPriority(sUser, sApplication, prioDefault) , "a
debug msg");
class MyPriorityFactor {
import java.util.Hashtable ;
import org.apache.log4j.Priority ;
public static Hashtable htPriorities = new Hashtable() ;
static {
init() ;
}
public static void init() {
// init your priorities here, from file or database with a loop over
the entries.
// for loop
String sUser = ... ;
String sApplication = ... ;
int iLogLevel = ... ;
htPriorities.put(createKey(sUser,sApplication),Priority.toPriority(iLogLevel)) ;
// end loop
}
public static String createKey (String sPart1, String sPart2) {
return sPart1 + ";" + sPart2 ;
}
public static Priority getPriority(String sUser, String sApp, Priority
prioDefault) {
Priority prioReturn = htPriorities.get(createKey(sUser,sApp)) ;
if (prioReturn == null) {
prioReturn = prioDefault ;
}
return prioReturn ;
}
}
I didn't test it, but something like that should work.
Regards
Gyoergy
-----Urspr�ngliche Nachricht-----
Von: Simon Kitching [mailto:[EMAIL PROTECTED]
Gesendet: Donnerstag, 25. M�rz 2004 06:56
An: Log4J Users List
Betreff: Re: Need to log based on criteria other than simply levels
On Thu, 2004-03-25 at 17:25, Mak Carson wrote:
> Hi,
>
> I'm comtemplating using log4j in an application that takes input from users(u1 -
> u99) with in 10 different processes -programs/classes- (p1 - p10).
>
> I understand how to use log4j to turn logging on and off based on a trace level.
> However, I also need to be able to selectively log based on any combination of
> u1-u99 and p1-p10. For example, I want all INFO debugs from messages generated by U3
> in process p3 and p5.
>
> Log4j seems to be my best bet. Any idea on how I can most efficiently achieve this
> slightly complex logging requirement? Another thing to keep in mind is that I need
> to 'log' to a custom application that provides its own API for sending data to it.
>
> Any insights will be mucho appreciated,
I'm looking at some similar issues at the moment.
Sending data to your external logging server is just a matter of
implementing a custom Appender (subclass AppenderSkeleton). If you
download the log4j source from CVS, there are plenty of Appenders in
there to borrow code from.
You can model your different "processes" by using log4j categories, eg
// app #1
Logger applog = Logger.getLog("apps.app1");
applog.debug("a debug msg");
applog.error("an error msg");
Logger applog2 = Logger.getLog("apps.app1.subcat1");
log2.debug("a msg from app1 about topic subcat1");
// app #2
Logger applog = Logger.getLog("apps.app2");
applog.debug("a debug msg");
etc.
You can then have a single log4j configuration file which all the apps
read from yet still configure filtering separately for each app, or even
for different categories within an app.
By using different categories, you can get control on a per-user basis,
provided you don't have *too* many users:
// app #1
String username = "sam";
Logger userlog = Logger.getLog("users." + username);
userlog.debug("a debug msg about sam");
This allows you to control logging on a per-user basis, but each
"Logger" uses memory, and they are cached once used. So if you have
thousands of users, this will chew up a lot of memory. If you definitely
only have a max of 100 users, you might get away with this.
If you use a separate configuration file per application, then the above
will allow you to control on both app and user axes, by enabling logging
for user "sam" only in the config file for app "app1".
I don't know of any way in log4j to be able to use a single
configuration file to configure based on multiple variables, ie
configure filtering level where (user="..." and app="...").
There is this thing called "Nested Diagnostic Contexts" that would at
least ensure that all messages for a user were labelled with the
username or userid of the user they relate to. But there isn't any
facility for filtering based on NDC values.
Regards,
Simon
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]