Package: verboselogs
Theverboselogspackage extends Python’sloggingmodule to add the log levelsNOTICE,SPAM,SUCCESSandVERBOSE:The NOTICE level sits between the predefined WARNING and INFO levels.The SPAM level sits between the predefined DEBUG and NOTSET levels.The SUCCESS level sits between the predefined WARNING and ERROR levels.The VERBOSE level sits between the predefined INFO and DEBUG levels.The code to do this is simple and short, but I still don’t want to copy/paste
it to every project I’m working on, hence this package. It’s currently tested
on Python 2.6, 2.7, 3.4, 3.5, 3.6 and PyPy.InstallationUsagePylint pluginOverview of logging levelsContactLicenseInstallationThe verboselogs package is available onPyPIwhich means installation should
be as simple as:$pipinstallverboselogsThere’s actually a multitude of ways to install Python packages (e.g. theper
user site-packages directory,virtual environmentsor just installing
system wide) and I have no intention of getting into that discussion here, so
if this intimidates you then read up on your options before returning to these
instructions ;-).UsageIt’s very simple to start using the verboselogs package:>>> import logging, verboselogs
>>> logger = verboselogs.VerboseLogger('verbose-demo')
>>> logger.addHandler(logging.StreamHandler())
>>> logger.setLevel(logging.VERBOSE)
>>> logger.verbose("Can we have verbose logging? %s", "Yes we can!")Here’s a skeleton of a very simple Python program with a command line interface
and configurable logging:"""
Usage: demo.py [OPTIONS]

This is the usage message of demo.py. Usually
this text explains how to use the program.

Supported options:
  -v, --verbose  make more noise
  -h, --help     show this message and exit
"""importgetoptimportloggingimportsysimportverboselogslogger=verboselogs.VerboseLogger('demo')logger.addHandler(logging.StreamHandler())logger.setLevel(logging.INFO)# Command line option defaults.verbosity=0# Parse command line options.opts,args=getopt.getopt(sys.argv[1:],'vqh',['verbose','quiet','help'])# Map command line options to variables.foroption,argumentinopts:ifoptionin('-v','--verbose'):verbosity+=1elifoptionin('-q','--quiet'):verbosity-=1elifoptionin('-h','--help'):print__doc__.strip()sys.exit(0)else:assertFalse,"Unhandled option!"# Configure logger for requested verbosity.ifverbosity>=4:logger.setLevel(logging.SPAM)elifverbosity>=3:logger.setLevel(logging.DEBUG)elifverbosity>=2:logger.setLevel(logging.VERBOSE)elifverbosity>=1:logger.setLevel(logging.NOTICE)elifverbosity<0:logger.setLevel(logging.WARNING)# Your code goes here....If you want to setVerboseLoggeras the default logging class for all
subsequent logger instances, you can do so usingverboselogs.install():importloggingimportverboselogsverboselogs.install()logger=logging.getLogger(__name__)# will be a VerboseLogger instancePylint pluginIf using the aboveverboselogs.install()approach,Pylintis not smart
enough to recognize thatloggingis using verboselogs, resulting in errors
like:E:285,24: Module 'logging' has no 'VERBOSE' member (no-member)
E:375,12: Instance of 'RootLogger' has no 'verbose' member (no-member)To fix this, verboselogs provides a Pylint pluginverboselogs.pylintwhich,
when loaded withpylint--load-pluginsverboselogs.pylint, adds the
verboselogs methods and constants to Pylint’s understanding of theloggingmodule.Overview of logging levelsThe table below shows the names,numeric valuesanddescriptionsof the
predefined log levels and the VERBOSE, NOTICE, and SPAM levels defined by this
package, plus some notes that I added.LevelValueDescriptionNotesNOTSET0When a logger is created, the
level is set to NOTSET (note
that the root logger is
created with level WARNING).This level isn’t intended to
be used explicitly, however
when a logger has its level
set to NOTSET its effective
level will be inherited from
the parent logger.SPAM5Way too verbose for regular
debugging, but nice to have
when someone is getting
desperate in a late night
debugging session and decides
that they want as much
instrumentation as possible!
:-)DEBUG10Detailed information,
typically of interest only
when diagnosing problems.Usually at this level the
logging output is so low
level that it’s not useful
to users who are not
familiar with the software’s
internals.VERBOSE15Detailed information that
should be understandable to
experienced users to provide
insight in the software’s
behavior; a sort of high
level debugging information.INFO20Confirmation that things
are working as expected.NOTICE25Auditing information about
things that have multiple
success paths or may need to
be reverted.WARNING30An indication that something
unexpected happened, or
indicative of some problem
in the near future (e.g.
‘disk space low’). The
software is still working
as expected.SUCCESS35A very explicit confirmation
of success.ERROR40Due to a more serious
problem, the software has not
been able to perform some
function.CRITICAL50A serious error, indicating
that the program itself may
be unable to continue
running.ContactThe latest version of verboselogs is available onPyPIandGitHub. The
documentation is hosted onRead the Docs. For bug reports please create an
issue onGitHub. If you have questions, suggestions, etc. feel free to send me
an e-mail atpeter@peterodding.com.LicenseThis software is licensed under theMIT license.© 2017 Peter Odding.