Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantages of logging vs. print() + logging best practices

Tags:

python

logging

I'm currently working on 1.0.0 release of pyftpdlib module. This new release will introduce some backward incompatible changes in that certain APIs will no longer accept bytes but unicode. While I'm at it, as part of this breackage, I was contemplating the possibility to get rid of my logging functions, which currently use the print statement, and use the logging module instead.

As of right now pyftpdlib delegates the logging to 3 functions:

def log(s):
   """Log messages intended for the end user."""
   print s

def logline(s):
   """Log commands and responses passing through the command channel."""
   print s

def logerror(s):
   """Log traceback outputs occurring in case of errors."""
   print >> sys.stderr, s

The user willing to customize logs (e.g. write them to a file) is supposed to just overwrite these 3 functions as in:

>>> from pyftpdlib import ftpserver
>>>
>>> def log2file(s):
...        open('ftpd.log', 'a').write(s)
...
>>> ftpserver.log = ftpserver.logline = ftpserver.logerror = log2file

Now I'm wondering: what benefits would imply to get rid of this approach and use logging module instead? From a module vendor perspective, how exactly am I supposed to expose logging functionalities in my module? Am I supposed to do this:

import logging
logger = logging.getLogger("pyftpdlib")

...and state in my doc that "logger" is the object which is supposed to be used in case the user wants to customize how logs behave? Is it legitimate to deliberately set a pre-defined format output as in:

FORMAT = '[%(asctime)] %(message)s'
logging.basicConfig(format=FORMAT)
logger = logging.getLogger('pyftpdlib')

...?

Can you think of a third-party module I can take cues from where the logging functionality is exposed and consolidated as part of the public API?

Thanks in advance.

like image 466
Giampaolo Rodolà Avatar asked May 20 '12 22:05

Giampaolo Rodolà


People also ask

Is there any reason to use logging instead of print?

Now throw in other cool things that print can't do (sending to socket, setting debug levels, logrotate, adding meta data etc.), you have every reason to prefer logging over plain print statements.

Why are logging best practices important for your business?

In a world with data flowing constantly through our devices, efficient logging practices have become a fundamental tool for modern businesses. By using logging best practices, logging data can give your business valuable insights, and you can use these logs and the information they hold for several purposes.

Should I use print or logging in Python?

For big projects logging is always a "best practice" because you can easily turn it on or off, and get more or less information. print offers neither of these advantages. The python logging documentation says the best use case for print is to display help messages for the user in a command line application.

What are the benefits of logging monitoring and analysis?

Using logging monitoring and analysis tools, you can reduce data volume, normalize logs from different sources, and make the entire process faster and more accurate. Any tool you use should be able to: Aggregate and centralize all your logs in one location Display your log messages in an easy-to-understand UI


1 Answers

libraries (ftp server or client library) should never initialize the logging system. So it's ok to instantiate a logger object and to point at logging.basicConfig in the documentation (or provide a function along the lines of basicConfig with fancier output and let the user choose among his logging configuration strategy, plain basicConfig or library provided configuration)

frameworks (e.g. django) or servers (ftp server daemon) should initialize the logging system to a reasonable default and allow for customization of logging system configuration.

like image 94
paolo_losi Avatar answered Sep 30 '22 09:09

paolo_losi