I am trying to use tinylog but can't figure out how to redirect System.out and System.err to the logger. With log4j2, I did it like this (from java code):
System.setErr(IoBuilder.forLogger(LogManager.getRootLogger()).setLevel(Level.ERROR).buildPrintStream());
System.setOut(IoBuilder.forLogger(LogManager.getRootLogger()).setLevel(Level.INFO).buildPrintStream());
I assume I should use same mechanism (System.setOut and setErr), however I'm not sure what is the right way to do it. I've read the online documentation but could not find any answer to my question (which is strange, since this is such a basic functionality that any logger must support in my opinion).
Any ideas?
I solved the problem myself, not sure if my solution is the best, but anyway. I introduced these two methods:
As far as LineReadingOutputStream class goes, I found it here: Java OutputStream reading lines of strings
One final element of this solution is creation of custom console writer, since the default one outputs everything to System.out / System.err, and that would create an infinite loop. The trick is to wrap the two streams and send strings to them. This is the code of my class that I call "IsolatedConsoleWriter" and has to be registered via META-INF (as described in the tinylog docs):
So this writer is exact copy of ConsoleWriter, it add only two fields: errStream and outStream. So, putting all of these elements together, I managed to achieve what I wanted - all System.out.println() and similar calls are rerouted to my logger, which formats all the data according to the defined rules (I actually use several writers - isolated console, as shown here, then the rolling file one, the "normal" file one, and also logcat, when I run my app under android).
If anyone comes up with a better solution, please let me know. However I believe that the functionality that achieves what I want should be part of the tinylog library itself (rather than using custom hacks like this one).