Link to home
Start Free TrialLog in
Avatar of d1G1t4L
d1G1t4L

asked on

Thread name from LogRecord's Thread ID?


Hi,

I'm trying to figure out how to write a custom LogFormatter that is able to include the name of the thread that generates a LogRecord.  It's very simple to get the thread ID through LogRecord.getThreadID() - but the problem is that the implementation for this API method actually creates a ThreadLocal that maintains an "ID" that has absolutely nothing to do with the VM's thread ID.

I looked into using the java.lang.management APIs - specifically ThreadMXBean and ThreadInfo, which allows one to easily lookup a thread's name based on its ID.  However, since this thread ID is completely unrelated to the thread ID reported in a LogRecord, it is not easy to match up the actual name of the thread that generated the LogRecord with the ID contained within the LogRecord.

BTW, I am using Java 5.0 Update 7, though I don't necessarily have an objection to upgrading to a beta of Mustang, if necessary...

Does anyone have any suggestions on how to accomplish this?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of d1G1t4L
d1G1t4L

ASKER


There are a couple of problems that I see with that:

#1 - I'd be limited to getting thread info only for one level of messages (actually, I don't think there is a debug() method in java.util.Logger, but that's OK, I got what you meant anyway :-)

#2 - I'd have to replace all of my existing logging statements with something like:

log.info(Thread.currentThread() + "...");

or whatever the appropriate logging level was (warning(), fine(), etc.)  I guess both problems are the same issue, really ... at any rate, it is a very expensive change to make.

Additionally, since I already have a custom LogFormatter, I figured it would be easiest for me to just change the current implementation of my format() method to print out the thread's name.
I take your point.

You probably know that log4j is really the de facto standard for logging. java.util.logging amounts to really only a hobbled version of it. You would be better using that, and you'd get thread id support merely by changing your message format
Avatar of d1G1t4L

ASKER


Yes, I'm aware of that ... unfortunately, the codebase I'm working on uses the java.util.logging facilities ... so gutting the logging engine would be equally painful :-/
OK, then i would incorporate the code i posted into the appropriate logging subclasses. The id reported should be the same as the one reported in my 'naive version' but check it anyway
Theres no way to get the thread name that I'm aware of, perhaps try using aspects or similkiar to inject the code if thats an option
There's no problem getting the name of a thread once you have a reference to it

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html#getName()
> There's no problem getting the name of a thread once you have a reference to it

but you don't have a reference to it, thats the point.
If you had that there be no problem, but you don't.
You can get a reference to the thread that called the custom logger using the code i first posted
> You can get a reference to the thread that called the custom logger using the code i first posted

of course you can, bet adding that to *every* existing log call, and all future logs calls it silly and unmaintainable.
something like aspects could look after it for you.

alternitively you could use a wrapper methodf that added the log name to all messages and do all your logging thru that method.
But you certainly wouldn't want to go and insert the thread name in *every* log call.
another approach would be to log the available thread id from LogRecord, and in addition log the name of each thread when you start it.
That way you would have the is->name mapping in your log.

you could even add some trickery in your custom logger to extract the thread name and maintain a map of id->name
>>of course you can, bet adding that to *every* existing log call

You don't add it - you get the reference in the custom logger as i mentioned earlier

http:Q_21909149.html#17045724
you don't seem to uderstand the problem, you cannot do that. Thats why the q is being asked :)
>>you cannot do that

Why not?
Avatar of d1G1t4L

ASKER

> another approach would be to log the available thread id from LogRecord, and in addition log the name of each thread when you start it.

Let me see if I understand your approach - you are suggesting that whenever I start a new thread, I should add a log statement that includes the name of the thread that was just started, and use the message information as well as the thread ID information to generate a mapping from (LogRecord) Thread ID -> Thread Name?

That's fine, I suppose ... but there's another catch - there are some third party libraries that this application uses that start threads, and I am unable to really go in and change the code for those libraries.

I guess maybe the best thing to do is just to bite the bullet and consider re-implementing the logging mechanism using another logging engine ... not something I was looking forward to doing, but perhaps the best long-term solution.
>>consider re-implementing the logging mechanism using another logging engine ... not something I was looking forward to doing,

Better to do that than start your own logging API. As i mentioned, log4j does what you want out of the box. Changes shouldn't be extensive
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial