<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Dec 4, 2017 at 4:02 PM, Osvaldo 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"><div dir="ltr"><div><div><div><div>Hi,<br><br></div>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.<br><br></div>I have Logback configured with slf4j and jul-to-slf4j, also I wrote a System.LoggerFinder</div></div></div></blockquote><div><br></div><div>Could you share it?</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"><div dir="ltr"><div><div>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.<br><br></div>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.<br><br></div>Anyone knows a magic trick here? :-)</div></blockquote><div><br></div><div>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".</div><div><br></div><div>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.</div><div><br></div><div>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.</div></div></div><div class="gmail_extra"><br></div><div class="gmail_extra"><br></div></div>