<div dir="ltr">Hi Oswaldo,<br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Dec 5, 2017 at 2:40 AM, Osvaldo Pinali Doederlein <span dir="ltr"><<a href="mailto:opinali@gmail.com" target="_blank">opinali@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi Martin,
Someone shared this code already: (Some TODOs but I didn't finish cleaning it up myself, but it;'s just some missing switch statements and reading resource bundles)
<a href="http://www.logicbig.com/tutorials/core-java-tutorial/java-9-changes/platform-logging/" target="_blank">http://www.logicbig.com/<wbr>tutorials/core-java-tutorial/<wbr>java-9-changes/platform-<wbr>logging/</a>
Not much code but it would be nice to have this built into logging libraries this code can be compiled with -source/-target compatible with older JVM, will be automatically ignored by older JVMs that do not recognize the new service declaration in the manifest.</blockquote><div><br></div><div>thank you. The TODOs are no problem.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
A standard prefix to work around recursion with stdout is one possible fix, but modern logging APIs make a lot of effort to reduce per-call overhead so copying/splitting all message strings can be significant...</blockquote><div><br></div><div>Only messages coming from stdout need to be processed and it can be pretty fast. No splitting, just appending.</div><div><br></div><div>You redirect stdout to MyLoggingOutputStream, which implements a simple state machine recognizing <font face="monospace, monospace">\nmyprefix</font> and possibly dropping the whole line.</div><div>Otherwise it does something like <font face="monospace, monospace"><a href="http://logger.info">logger.info</a>("myprefix {}", new String(collectedBytes))</font>.</div><div>I guess, that adding the prefix is way faster than other necessary processing, especially the String<->byte[] conversions.</div><div><br></div><div>I'd write more about it, but I've got a better idea: Using logback Markers. A string coming from stdout gets logged with a marker and the stdout appender ignores marked messages.</div><div><br></div><div>Regards, Martin.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">maybe I could make my app check the logging config at startup, and only enable the stdout->logger redirection if the logging API's stdout appender is not enabled. I guess Logback's LoggerContextListener allows me to be notified if the configuration changes to I can enable/disable that as needed.
<blockquote style="border-left:2px solid rgb(204,204,204);padding:0px 1em" class="gmail-m_-2033639827863038497quote gmail-m_-2033639827863038497dark-border-color"><div class="gmail-m_-2033639827863038497quote gmail-m_-2033639827863038497light-border-color">
<div class="gmail-m_-2033639827863038497quote-author" style="font-weight:bold">Martin Grajcar wrote</div>
<div class="gmail-m_-2033639827863038497quote-message">
On Mon, Dec 4, 2017 at 4:02 PM, Osvaldo Doederlein <<a href="http:///user/SendEmail.jtp?type=email&email=opinali%40" rel="nofollow" target="_blank">[hidden email]</a>>
wrote:
> Hi,
>
> I want to redirect the JVM's internal logs to Logback, is this possible?
> More specifically, I have all my app logs going to a single appender and I
> want JVM logs such as GC to be writing to the same appender, interleaved
> with app logs.
>
> I have Logback configured with slf4j and jul-to-slf4j, also I wrote a
> System.LoggerFinder
>
Could you share it?
> service to redirect System.Logger to SLF4J->Logback, which I can see
> working for core lib logs such as java.base / sun.net.www.protocol.http.<wbr>HttpURLConnection.
> (See <a href="http://openjdk.java.net/jeps/158" target="_blank">http://openjdk.java.net/jeps/<wbr>158</a>) However, the GC logs are still
> going to stdout. The documentation for JEP 158 seems to imply that the JVM
> logs are something separate from java.util.logging, can only be sent to
> stderr, stdout or a log file, but not to a JUL logger.
>
> I know that I can hook the System.out/err streams and redirect writes to a
> logger, but this is not ideal, for one thing I cannot configure Logback to
> send logs back to stdout (which I often do in test/interactive runs)
> without risking a stack overflow. I could write code that walks the stack
> to decide if the call to System.err/out is coming from a logger, but this
> is expensive.
>
> Anyone knows a magic trick here? :-)
>
A magic prefix maybe? Something like prefixing every message by "I'm coming
from stdout. If you send me there again, your stack will overflow".
Prefixing every line forwarded from stdout to the logger by "STDOUT "
should do, though you'll miss all lines already starting by the prefix.
Which can be made arbitrarily improbable.
Assuming all lines are unique, you could use a (Concurrent?)HashSet storing
all lines send to stdout by the logger and remove them when you see them
coming back. This is probably much faster than stack walking. Some periodic
cleanup may be necessary in case something gets lost.
______________________________<wbr>_________________
logback-user mailing list
<a href="http:///user/SendEmail.jtp?type=email&email=logback-user%40" rel="nofollow" target="_blank">[hidden email]</a>
<a href="http://mailman.qos.ch/mailman/listinfo/logback-user" target="_blank">http://mailman.qos.ch/mailman/<wbr>listinfo/logback-user</a>
</div>
</div></blockquote>
<br><hr align="left" width="300">
Sent from the <a href="http://logback.10977.n7.nabble.com/Users-f3.html" target="_blank">Users mailing list archive</a> at Nabble.com.<br><br>______________________________<wbr>_________________<br>
logback-user mailing list<br>
<a href="mailto:logback-user@qos.ch">logback-user@qos.ch</a><br>
<a href="http://mailman.qos.ch/mailman/listinfo/logback-user" rel="noreferrer" target="_blank">http://mailman.qos.ch/mailman/<wbr>listinfo/logback-user</a><br></blockquote></div><br></div></div>