How to Change Java Logging Console Output from Std Err to Std Out

relations between log levels and stdErr/stdOut

Very briefly:

Loggers are part of a high level logging framework. Logging is much more advanced than standard output streams.

When a process starts up the operating system gives it a stdout and stderr stream which the program may write to. Stdout should be used for information and stderr for error information.

This is much lower level and you can access these streams with the System class:

System.out.println(); // Write to stdout
System.err.println(); // Write to stderr

Why does java.util.logging.Logger print to stderr?

By default, the logger outputs log records of level INFO and above (i.e., INFO, WARNING and SEVERE) to standard error stream (System.err).

Source: www3.ntu.edu.sg/home/ehchua/programming/java/JavaLogging.html

Logging error to stderr and debug, info to stdout with log4j

Per Jon Skeet's previous post at Post

unfortunately there is no maximum threshold, so wherever you get
debug messages, you also get warning messages. That's a bit of a pain,
IMO.

Java: Program stdout and stderr to some sort of handler

By using System.setOut() and System.setErr() you can set standard output and error stream to any PrintStream, and capture everything.

ADDITION:
I don't know if there exist any elegant way to do this, but one way to do so is:

1. Save default output stream

PrintStream defaultOut = System.out;

(System.getOut() would be fun to have though)

2. Redirect System.out to the printstream you want (e.g. any file)

3. Instead of using System.out.println() use your print function, which does following:

print(Object o){

defaultOut.println(o);//Assuming that defaultOut is visible in this function

System.out.println(o);
}

How to redirect stderr and stdout in distinct files in Jetty?

Jetty has crude logging by default because we can't really make assumptions was to what logging frameworks anyone will use. However we have it set up so that all you need to do is drop in the slf4j-api jar into the classpath and we'll start using that and from there you can redirect your logging however you like.

see this for basic information: http://wiki.eclipse.org/Jetty/Feature/Jetty_Logging

you can look for a nice fine grained example of sifting logs based on webapps here:

http://wiki.eclipse.org/Jetty/Tutorial/Sifting_Logs_with_Logback

So while jetty logging looks rough right out of the box, it has to be that way to not cause problems with any user logging framework since jetty is used in so many different ways, embedded, as as distribution, on devices like android, etc etc. We do have a nice simple way to give you all the control you need via this slf4j setup.

How to not to show stdout from 3rd party jar and only show slf4j log

What I would suggest would be to redirect System.out to slf4j. That way, all logging can be configured in your logging framework in a standardized normal way.

Note that depending on the library you're using to do the redirection, if you have some loggers print to standard output, you may need to carefully configure it so that you don't end up in an infinite loop, where a printed message gets redirected to logging, which gets redirected to printing a message, and so on.

Also note that most build environments & IDEs have support for viewing the contents of a log file while it's being generated, so you don't have to print anything to standard output if you don't want to.



Related Topics



Leave a reply



Submit