Solved

Thread name from LogRecord's Thread ID?

Posted on 2006-07-05
20
294 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bigHeights  challenge 13 56
array11 challenge 16 52
json example 39 115
@SBGen Method 3 25
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

747 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

12 Experts available now in Live!

Get 1:1 Help Now