Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Thread name from LogRecord's Thread ID?

Posted on 2006-07-05
20
Medium Priority
?
329 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 3
20 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 1000 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

722 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