96

I have a program I'm writing that I want to write a custom logging facility for (e.g. diagnostic, notice, warning, error).

Should I be using the stdout or the stderr stream to do this? It is an interpreter of sorts and the user can ask it to print output.

Edit: Please stop recommending me logging frameworks :(

5
  • If you use a language like java why not use one of the famous logging frameworks, like log4j?
    – flash
    Feb 7, 2011 at 8:00
  • I'd really recommend that you look at some logging libraries before spending the time implementing logging. If you let us know which language you are writing in, I'm sure there will be lots of suggestions.
    – a'r
    Feb 7, 2011 at 8:01
  • what language do you use? Is it c or c++? Feb 7, 2011 at 8:02
  • 46
    The first four commenters seem to think that rfw is asking for help on how to write logging code. He is not. He has already made his decision and he is just wondering what the best practices are for the high-level behavior of a logging system. The language he is using is irrelevant. flash, your comment could have been more useful if you just told us whether log4j uses stderr or stdout. Feb 7, 2011 at 8:05
  • log4j outputs to stdout by default. This is an early example of the utter cluelessness now rampant in the Java world. Never mind that stderr was invented in the 1970s for this purpose. Too bad it wasn't named stdlog instead. (In C++, std::clog is usually aliased to std::cerr)
    – arayq2
    Feb 5, 2023 at 14:27

3 Answers 3

79

Regular output (the actual result of running the program) should go on stdout, things like you mentioned (e.g. diagnostic, notice, warning, error) on stderr.

If there is no "regular output", I would say that it doesn't really matter which one you choose. You could argue that the logging is the only output, so that should go to stdout. Or you could argue that it is still "exceptional information" which should go to stderr.

1
  • Since it's an interpreter, I'd expect the majority of users to output something to stdout. It may have support for printing to stderr, but I guess that's their own funeral...
    – user193476
    Feb 7, 2011 at 8:43
16

Do you write something else to stdout? If the answer is Yes then it would be quite hard for someone to distinguish between normal flow and errors in the middle of the file that was used to redirect from stdout.

What if stdout will be used as input for another program? Will it be able to parse this debug information? In this case it would be good to write only critical errors to stderr. If you need to write something else you may consider to write to additional log file.

8

Stdout is the "output" or "result" of a program. (This is why shell piping applies to stdout.)

Stderr is the "logging" or "diagnostics" of a program. (This is why stderr is usually unbuffered.)

It sounds like these messages would be the latter.

1
  • 2
    +1 for the buffering consideration. Having errors be saved ASAP is important, especially if they occur rarely and thus might not be flushed for a long time otherwise May 5, 2023 at 3:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.