Logging Thread Name or Thread ID

617 views
Skip to first unread message

zhenya leonov

unread,
Jun 12, 2020, 3:08:11 PM6/12/20
to Flogger Discuss
Hi again, I have another question about best practices with flogger.

My team is working on a project which uses a lot of concurrency.
Currently a lot of our log statements look like this: logger.atInfo().log("[Thread:%s]..."), Thread.currentThread().getName(), ...);

As you can imagine, sometimes we forget to add the thread name to the log message, and having every message start with "[Thread%s]" does seem cumbersome.

A little better way would be (although this doesn't solve anything, just stylistically):
logger.atInfo().withThread(Thread.currentThread().getName()).log(...);
logger.atInfo().withThread(Thread.currentThread().getId()).log(...);

An even better way would be (still, just syntactic sugar):
logger.atInfo().withThreadName().log(...);
logger.atInfo().withThreadId().log(...);

And finally, maybe a logger implementation that always prints the thread name/id:
private static final ThreadAwareFluentLogger logger = ThreadAwareFluentLogger.forEnclosingClass(USE_THREAD_NAME); // or something similar
...
logger.atInfo().log("...");

My first question is, am I asking something stupid :)?
Before using flogger we were using JUL which did not offer native support for logging the thread name/id either.
But I believe log4j, or slf4j, or one of them does.

If I am not asking something stupid, then, what would be the best way to go about implementing this? And do you think this should be a feature of flogger?

If I understand correctly, my options are:
1) See if MetadataKey would work for this use case.
2) Create a customer logger, with a custom concrete api implementation, and a custom back-end.


Thoughts?

Thanks a lot.
Message has been deleted

zhenya

unread,
Jun 12, 2020, 10:26:33 PM6/12/20
to David Beaumont, Flogger Discuss
David, thanks for replying.

First things first. Your reply indicates that you think I work in google. I don't.
I do believe the discussion group is open to the public?

Right....?

:)

On Fri, Jun 12, 2020 at 3:48 PM David Beaumont <dbea...@google.com> wrote:
I had thought that the GoogleFormatter class added thread information to all log statements (even non Flogger ones), but it doesn't.

When I checked it ends up being conditional:
and relying on:
Which suggests using TraceLoggingContext (which is the other idea).

So either use LogContext (even though it's deprecated) because that adds the thread info at the front of the logs and is easy to use.
Or use TraceLoggingContext (but that does not result in the thread name/ID being at the front of the log line).

Neither is great, I'll admit (I thought GoogleFormatting just always did this to be honest, but I guess the logs team would not be happy with the additional output that would cause).
Ideally you'd just want a flag that says "add current thread info everywhere", since I suspect you also want it on log statements you don't own.
However that's problematic if you are generating a lot of logs and might even have other implications.

So please raise a feature request (since it's a perfectly reasonable thing to want) and I'll have a think.
What Flogger needs is better control of formatting tags in the output (a feature I've wanted for about 5 years but never had the time to work on).

And finally, please do not try and solve perceived deficiencies with Flogger by inventing another logging API (I just spent 7 years of 20% time removing other logging APIs people had invented to "solve" little problems like this :). Flogger should be able to handle anything like this one way or another and adding a new logging API of any kind is a very big slice of technical debt.

If you want to meet (VC) to chat about potential solutions, please ping me.

David


--
You received this message because you are subscribed to the Google Groups "Flogger Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flogger-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flogger-discuss/2ffdcf0a-8103-4432-9731-b4851e596ab3o%40googlegroups.com.


--
David Beaumont :: Îñţérñåţîöñåļîžåţîờñ Libraries :: Google
Google Switzerland GmbH., Brandschenkestrasse 110, CH-8002, Zürich - Switzerland

David Beaumont

unread,
Jun 13, 2020, 5:46:33 AM6/13/20
to Flogger Discuss
Yeah, my bad, I thought it was an internal mail (none of the link would would for you, so it's not a big deal).
I've deleted that, and you'll be glad to know that in that case, you problem has a much easier solution :)

If you're using the JUL backend (i.e. the default "system" backend) then you can just install a formatter for logging via the normal logging.properties mechanism.

Then you can add things like Thread ID to every log message regardless of where it comes from (no change of API needed).
This works because the log formatter is always going to be called in the same thread as the log statement, so Thread.currentThread() works fine there and you don't need to pass anything through.

If you did want only certain log messages to have it, you can do that too using a MetadataKey, and then having the formatter recognize the existence of the key in order to change what's formatted (you can check if a LogRecord comes from Flogger by checking its type via instanceof and then casting it to AbstractLogContext, from where you can get the LogData).

Hope that helps,
    David
Reply all
Reply to author
Forward
0 new messages