[slf4j-user] Avoiding new line after log messages
Ceki Gulcu
listid at qos.ch
Sun Jan 6 20:32:26 CET 2008
Hello Guy,
As implied by both Simon and Jake's answers, the SLF4J is not responsible for
formatting the logs. SLF4J merely delegates to the underlying API, be it log4j,
JUL or logback.
It is entirely possible for the underlying logging API to *conditinally* omit
outputting a line separator depending on the pattern converter (see
PatternLayout). This is theoretically possible with log4j and rather easy to
accomplish if the underlying logging API supports marker objects.
For instance, you could write:
Logger logger = LoggerFactory.getLogger("just.testing");
Marker noNewLineMarker = MarkerFactory.getMarker("NO_NEW_LINE");
logger.debug(noNewLineMarker, "Hello"); <-- the trick is in the marker
logger.debug(" world");
The default new line converter is just that, the default. Nothing prevents you
from registering your own new line converter with some intelligence for
supporting conditional new lines.
I'll be glad to develop if there is interest.
HTH,
simon wrote:
> On Fri, 2008-01-04 at 12:16 -0600, Jacob Kjome wrote:
>
> Well, either
> (a) SLF4J buffers/merges messages and only passes them through to the
> underlying system once a message is complete, or
> (b) SLF4J passes them through immediately, but the underlying logging
> lib merges them, or
> (c) SLF4J passes them through immediately and an appender of the
> underlying logging lib merges them.
>
> Option (a) is theoretically possible, but I can definitely say that no
> such feature currently exists. And I think it would be a bad idea. If
> code logs several "parts" then fails to do the "flush" then the whole
> message gets lost. Plus it adds complexity to the "logging adapter"
> layer that almost nobody will use.
>
> Option (b) and (c) are not feasable because how an app logs is
> determined by the app programmer but what logging lib/adapters are
> configured are determined by the app deployer. There is no cross-library
> convention for merging messages, so the application doing the logging
> will never know whether the lib/adapters available at runtime are
> capable of merging messages or not.
>
> And furthermore, I can say that I am not aware of any such "message
> merging" functionality in log4j or the java.util.logging implementation.
>
> So I think "no" was a pretty fair answer.
>
> If someone wants to merge several strings into a message, then they
> should use a StringBuffer, and then log its contents once everything
> necessary has been collected. As one atomic log call.
>
> Regards,
>
> Simon
--
Ceki Gülcü
More information about the slf4j-user
mailing list