[slf4j-dev] [JIRA] Updates for SLF4J-526: Idea for fluent API improvement

QOS.CH (JIRA) noreply-jira at qos.ch
Tue Mar 22 01:07:00 CET 2022


SLF4J / SLF4J-526 [Open]
Idea for fluent API improvement

==============================

Here's what changed in this issue in the last few minutes.

There is 1 comment.

View or comment on issue using this link
https://jira.qos.ch/browse/SLF4J-526

==============================
 1 comment
------------------------------

Adrian Shum on 22/Mar/22 12:53 AM
@ceki+jira Great to know! :)

some other ideas for the initial proposal, which I dunno if make sense:
 * By default, return a thread-local instance of builder which can
 ** reduce cost of allocating new one every time
 ** could provide detection of missing log(), by checking if builder.log() has been called at atXxx()
 ** Although above should serve 99.9% of use case, if someone really need a separate instance of builder (e.g. they are building up multiple log messages at the same time), we could provide an extra atXxx() method which will return new instance
 * It may make sense to allow providing message/messageProvider at the atXxx() method directly.

So to demonstrate the idea:
{code:java}
//////////////////////////////////////////////////////////////////////
// reduced builder allocation
//////////////////////////////////////////////////////////////////////
logger.atDebug().message("first log {}").arg(a1).log();     // returning a thread local instance of builder
logger.atDebug().message("second log {}").arg(a2).log();    // the builder instance we use here will be the same as above

logger.atDebug().message("forgot to call log()");
logger.atDebug().message("fourth log{}").log();             // when we call atDebug(), it check if the builder is "clear". If not, give a warning or automatically call the log()?

//////////////////////////////////////////////////////////////////////
// if people really wants to have multiple instances of builder
//////////////////////////////////////////////////////////////////////
outputBuilder = logger.atDebug(AllocationType::NEW);    // default one is SINGLETON maybe, which means single one per-thread
errorBuilder = logger.atDebug(AllocationType::NEW);
// bunch of logic to construct log by outputBuilder and errorBuilder
outputBuilder.log();
errorBuilder.log();

(of course, user will not enjoy the auto-detection of missing log())


//////////////////////////////////////////////////////////////////////
// we could provide message directly
//////////////////////////////////////////////////////////////////////
logger.atDebug("temperature is {}").arg(temperature).log();

{code}


==============================
 This message was sent by Atlassian Jira (v8.8.0#808000-sha1:e2c7e59)



More information about the slf4j-dev mailing list