2

Generally print statements are frowned upon in favor to logging. But are there any situations where I should prefer using print statements?

In a interactive command line application, if I ask for user input and he gives me wrong input, should I print the error message or should I log it? If I should print it, to where, stdout or stderr?

1
  • 2
    I'd think of it a little differently. First, these are not mutually exclusive, you can do both simultaneously. Second, think of the purpose. Logging lets you reconstruct what was going on with the application (why was some decision taken, what was the state of the application when something was written to the DB or when it crashed). On the other hand, a user entering the wrong input is not really an error in the application, it's an expected scenario that your application should be able to gracefully handle, and respond to with a message that's helpful to the user. Commented Nov 23, 2023 at 6:01

2 Answers 2

8

Printing and logging serve different purposes.

The purpose of logging tools is that they provide you with information after the fact about what a piece of software was doing. These tools are designed such that the software can run unattended, even in a production environment. The better tools even make it possible to tune the amount of logging you get to see while the software is running.

The primary purpose of print statements is to communicate with the user of the software, while the user is interacting with it.

As logging and printing are not mutually exclusive and target different audiences, you can easily provide both logging and printing in a interactive console application. The print statement would tell the user that something is wrong and the logging should help the developer to reconstruct what happened when the user asks for support.

5

Each has it's own capabilities.

Print lets you write:

  1. to a hard coded file path
  2. to stdout
  3. to stderr
  4. only what you say to write

Logging lets you write

  1. to a user configured file path
  2. to a user configured logging level
  3. to stdout
  4. to stderr
  5. many prefix fields (timestamps, etc.) that you may not want

These days I only use print when writing debugging scaffolding code. That is, code that never makes it into production. I write it only for me with a little //TODO remove comment next to it that helps me keep it out of production.

Sure, printing to stdout and stderr can be redirected. But most applications update more than two files. Sure, print doesn't have to work from a hard coded file path. But now you have to write code to make the file path configurable.

Logging is built from Print. So with enough effort you can make Print do all the same things Logging does. But if you need those things use what already has them.

In a interactive command line application, if I ask for user input and he gives me wrong input, should I print the error message or should I log it? If I should print it, to where, stdout or stderr?

You can log it to stderr in most logging systems. Doing it this way gives users the ability to reconfigure where it goes without making them redirect stderr every time they run it.

If that's not an issue, printing to stderr still works as well as it ever did.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.