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.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With