users@glassfish.java.net

Re: thread name in log messages?

From: Lachezar Dobrev <l.dobrev_at_gmail.com>
Date: Mon, 10 Sep 2012 15:05:20 +0300

  I hope I am not going to hijack the thread, but is there an option
to have the application's name and/or the application module's name in
the log? Especially required when using one and the same library
(Spring Framework for example) in multiple applications.

  I also noted, that Thread Name is inconsistent, since many threads
with different ID have the same name, which is at least
counter-productive (small log excerpt):

> glassfish_at_glassfish:~/glassfish/domains/domain1/logs$ cut -f 6 -d \| < server.log | cut -f 1,2 -d \; | uniq | sort -n -k 2 -t \= | uniq
>
> _ThreadID=36;_ThreadName=Thread-2
> _ThreadID=70;_ThreadName=Thread-2
> _ThreadID=76;_ThreadName=Thread-2
> _ThreadID=100;_ThreadName=Thread-2
> _ThreadID=101;_ThreadName=Thread-2
> _ThreadID=102;_ThreadName=Thread-2
> _ThreadID=104;_ThreadName=Thread-2
> _ThreadID=105;_ThreadName=Thread-2
> _ThreadID=900;_ThreadName=Thread-2
> _ThreadID=901;_ThreadName=Thread-2
> _ThreadID=902;_ThreadName=Thread-2
> _ThreadID=903;_ThreadName=Thread-2
> _ThreadID=917;_ThreadName=Thread-2
> _ThreadID=921;_ThreadName=Thread-2
> _ThreadID=927;_ThreadName=Thread-2
> _ThreadID=1056;_ThreadName=Thread-2

  Is it possible, that the Thread Name is generated, rather than
obtained from the Thread?
  Or is it possible, that this information is obtained on the first
logging attempt (hence a Thread Name with low number), and then
somehow reused, instead of being obtained again?

2012/9/6 Carla Mott <carla.mott_at_oracle.com>:
> I did work on this code years ago. Naman took over that code and made
> significant changes so he may have an answer.
>
> Carla
>
>
> On 9/6/12 8:56 AM, Jason Lee wrote:
>>
>> Good grief. I totally missed that. Too many irons. :) I can't answer
>> your real question, unfortunately. Carla spent some time working on
>> logging. Perhaps she can chime in if she's paying attention. If not, you
>> might try emailing her directly.
>>
>> On 09/06/2012 06:18 AM, Bobby Bissett wrote:
>>>
>>> Hi Jason,
>>>
>>> How's it going? In my example, I am setting the name of the thread
>>> that's doing the logging, but it's not the name that's published in
>>> the log record. I'm guessing the handler set in GF is using a separate
>>> thread to actually write to the log file, and was wondering why or if
>>> that's what's going on. Or if there's some other setting I can use so
>>> that my thread name shows up. Or something....
>>>
>>> Cheers,
>>> Bobby
>>>
>>> On Wed, Sep 5, 2012 at 2:46 PM, Jason Lee <jason.d.lee_at_oracle.com> wrote:
>>>>
>>>> There is a ctor on Thread that lets you set the name as you create it,
>>>> or
>>>> you can call thread.setName() after the fact. If you don't do that, it
>>>> calculates it. Somehow. Or is your question how it calculates the
>>>> default?
>>>> :)
>>>>
>>>>
>>>> On 09/05/2012 10:27 AM, Bobby Bissett wrote:
>>>>>
>>>>> Hi all,
>>>>>
>>>>> Just curious: where does the thread name come from in messages logged
>>>>> from an EE app? For instance, in GF 3.1.2.2, I have this servlet:
>>>>>
>>>>> --- begin ---
>>>>> public class TestServlet extends HttpServlet {
>>>>>
>>>>> protected void doGet(HttpServletRequest req, HttpServletResponse
>>>>> resp)
>>>>> throws ServletException, IOException {
>>>>>
>>>>> resp.getWriter().println("hi there");
>>>>>
>>>>> TestThread t = new TestThread();
>>>>> t.start();
>>>>> }
>>>>>
>>>>> static class TestThread extends Thread {
>>>>>
>>>>> TestThread() {
>>>>> super("bobby_test_thread");
>>>>> }
>>>>>
>>>>> @Override
>>>>> public void run() {
>>>>> Logger.getLogger("testLogger").info("Hi, I am " +
>>>>> getName());
>>>>> }
>>>>> }
>>>>> }
>>>>> --- end ---
>>>>>
>>>>> ...and when accessed the log contains:
>>>>>
>>>>>
>>>>>
>>>>> [#|2012-09-05T09:45:35.156-0400|INFO|glassfish3.1.2|testLogger|_ThreadID=24;_ThreadName=Thread-4;|Hi,
>>>>> I am bobby_test_thread|#]
>>>>>
>>>>> From where does that "Thread-4" name come (and is there any way to
>>>>> have it list the thread that logged the message)?
>>>>>
>>>>> Thanks,
>>>>> Bobby
>>>>
>>>>
>>>>
>>>> --
>>>> Jason Lee
>>>> Principal Member of Technical Staff
>>>> GlassFish Team
>>>>
>>>> Oracle Corporation
>>>> Phone +1 405-216-3193
>>>> Blog http://blogs.steeplesoft.com
>>>>
>>
>>
>