Solved

Thread name from LogRecord's Thread ID?

Posted on 2006-07-05
20
297 Views
Last Modified: 2008-02-26

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?
0
Comment
Question by:d1G1t4L
  • 8
  • 6
  • 3
20 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 17045468
Can't you use:

log.debug(Thread.currentThread());

?
0
 
LVL 1

Author Comment

by:d1G1t4L
ID: 17045556

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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17045648
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
0
 
LVL 1

Author Comment

by:d1G1t4L
ID: 17045685

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 :-/
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17045724
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
0
 
LVL 92

Expert Comment

by:objects
ID: 17047102
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17048570
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()
0
 
LVL 92

Expert Comment

by:objects
ID: 17048589
> 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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:CEHJ
ID: 17048602
You can get a reference to the thread that called the custom logger using the code i first posted
0
 
LVL 92

Expert Comment

by:objects
ID: 17048624
> 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.
0
 
LVL 92

Expert Comment

by:objects
ID: 17048633
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17048652
>>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
0
 
LVL 92

Expert Comment

by:objects
ID: 17048675
you don't seem to uderstand the problem, you cannot do that. Thats why the q is being asked :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17048680
>>you cannot do that

Why not?
0
 
LVL 1

Author Comment

by:d1G1t4L
ID: 17083325
> 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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17083665
>>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
0
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 17087002
> 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.

Sorry, only way i could think of to get the trhread name

> 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

using log4j you can get the thread name using %t with the standard PatternLayout

http://logging.apache.org/log4j/docs/api/org/apache/log4j/PatternLayout.html
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now