95

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 :(

  • If you use a language like java why not use one of the famous logging frameworks, like log4j? – flash Feb 07 '11 at 08: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 07 '11 at 08:01
  • what language do you use? Is it c or c++? – Vladimir Ivanov Feb 07 '11 at 08:02
  • 42
    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. – David Grayson Feb 07 '11 at 08: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 05 '23 at 14:27

3 Answers3

78

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.

Jakob
  • 24,154
  • 8
  • 46
  • 57
  • 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... –  Feb 07 '11 at 08: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.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Andrey Taptunov
  • 9,367
  • 5
  • 31
  • 44
5

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.

Paul Draper
  • 78,542
  • 46
  • 206
  • 285
  • 1
    +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 – Ted Brownlow May 05 '23 at 03:38