Advantages of logging vs. print()

Giampaolo Rodolà g.rodola at gmail.com
Sat May 19 13:23:53 EDT 2012


Hi all,
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 rewrite 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 asking: 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
use/provide logging 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 logging substantially slower compared to print()?


Thanks in advance

--- Giampaolo
http://code.google.com/p/pyftpdlib/
http://code.google.com/p/psutil/
http://code.google.com/p/pysendfile/



More information about the Python-list mailing list