Merchako
Merchako

Reputation: 899

Does python logging replace print?

Up to now, I've been peppering my code with 'if VERBOSE: print debug message'. Then I learned how to use the logging module. It looks as though it does everything I could possibly want, and then some. So much so that I got carried away and replaced all print statements in my code with logging statements, not just the verbose ones.

Is this a mistake? If I'm already using logging, can I use it to replace print in every way? If not, how do I choose whether to use logging or print?

Upvotes: 10

Views: 10174

Answers (5)

chepner
chepner

Reputation: 531808

print would still be used for output that is essential to the operation of your program. logging is for output that might be useful in determining how your program is working (errors, progress, etc), but could be omitted without affecting the usability of your program.

The Basic Logging Tutorial discusses when to use logging versus other mechanisms.

Upvotes: 9

m1keil
m1keil

Reputation: 4575

logging may or may not completely replace your print() calls. In some cases, like when developing a library or using multiple threads logging is the better choice.

There are no hard rules about it. Personally I tend not to mix logging and print and use either the one or the other.

Upvotes: 0

Fred S
Fred S

Reputation: 995

One other thing to consider is if you have code where speed is a concern. Or if you are printing something where formats may change in rare corner cases, like say if communication is lost, or an error occurs.

Consider these 2 options:

msg = "Parameters are: %.3f, %.3f, %.3f, %.3f, %.3f, %.3f, %.3f, %.3f"%(p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7])
logger.debug(msg)

or

if VERBOSE:
    msg = "Parameters are: %.3f, %.3f, %.3f, %.3f, %.3f, %.3f, %.3f, %.3f"%(p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7])
    print msg

In the first case, the string formatting is still getting done even if the logging level is not DEBUG, which will impact overall performance. In the second, if VERBOSE is False, nothing gets done.

Also, I have ran into troubles where exceptions are generated from the string formatting of debug messages. Take for example in the above code, if there is a rare use case where "p" only has 6 values, you will get an index error. Or if one of the p values is "None", you will get an exception. But in the second case, if VERBOSE is False, you will never see a crash caused by the debug code.

So logging is very handy, but I tend to use it only in cases where I want the messaging as a permanent code feature. If I need to put in a temporary message to help debug a one time problem, I'll usually just use print statements.

And also as general rule, stuff the end user is meant to see should be "print".

Upvotes: 0

logc
logc

Reputation: 3923

Some people say that logging is for operators of your software. That means, people who need to correct a recurring error, or tell what is the state of a running software while it runs.

One could then say that printing is for users of your software. That means, those people who are interested in the results of running your software. If there is an error, they are not able to correct it, so they can only ignore it, or stop using your software altogether.

That is why, traditionally, logging goes to stderr, while printing goes to stdout.

So, logging should not completely replace printing, because each is intended for a different audience. You can misuse it to completely replace printing (e.g. by using different handlers that attach to stderr and stdout with different levels), but this is misusing the logging system in the same way that before you were misusing the printing statements to do logging.

Incidentally, misusing print statements for debugging is sometimes called Caveman logging. Welcome outside the cave! :)

Upvotes: 3

Klaus D.
Klaus D.

Reputation: 14379

Python logging can completely replace your print statements. You just have more options. The default configuration of logging writes to the console as print does, but you can use logging in other ways, like writing to a logging server, reformatting the output, adding information like the module the line came from, or the time.

Upvotes: 1

Related Questions