• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 389
  • Last Modified:

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?
0
d1G1t4L
Asked:
d1G1t4L
  • 8
  • 6
  • 3
2 Solutions
 
CEHJCommented:
Can't you use:

log.debug(Thread.currentThread());

?
0
 
d1G1t4LAuthor Commented:

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
 
CEHJCommented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
d1G1t4LAuthor Commented:

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
 
CEHJCommented:
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
 
objectsCommented:
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
 
CEHJCommented:
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
 
objectsCommented:
> 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
 
CEHJCommented:
You can get a reference to the thread that called the custom logger using the code i first posted
0
 
objectsCommented:
> 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
 
objectsCommented:
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
 
CEHJCommented:
>>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
 
objectsCommented:
you don't seem to uderstand the problem, you cannot do that. Thats why the q is being asked :)
0
 
CEHJCommented:
>>you cannot do that

Why not?
0
 
d1G1t4LAuthor Commented:
> 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
 
CEHJCommented:
>>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
 
objectsCommented:
> 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now