Advantages of logging vs. print()

Jean-Michel Pichavant jeanmichel at sequans.com
Tue May 22 07:24:52 EDT 2012


Giampaolo Rodolà wrote:
> 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/
>   
Well, getting all the features of the standard logging modules without 
writing a line is one huge benefit.
It has among many other things :
  - Handlers: ability to handle logs in a different way, logging on 
stdout, on a file, on the network ...
  - Filters: ability to filter logs, depending on their severity, 
content or whatsoever
  - Thread safe
  - fully integrated with any other application using the logging module
  - configuration file
  - it's standard

If you plan to release your module to the python community, using the 
logging module is something you should consider.
Only by stating that your module is using the standard logging module 
will allow any user to configure it as they se fit.

btw

The code is usually the following:

import logging
logger = logging.getLogger(__name__)


JM




More information about the Python-list mailing list