Peter's Blog

Redefining the Impossible

Python logging module:


Python logging module: a good reference is http://www.red-dove.com/python_logging.html.

It looks to me to be horribly over engineered.

It is kinda cool:

   1  #
   2  import logging
   3  import logging.handlers
   4  
   5  #
   6  # Set up basic handling.
   7  #
   8  logging.basicConfig()
   9  log = logging.getLogger( "Test")
  10  
  11  #
  12  # Log info messages and higher.
  13  #
  14  log.setLevel( logging.INFO)
  15  
  16  #
  17  # An info message
  18  #
  19  log.info( "Blah")
  20  
  21  #
  22  # Add a handler that will send email
  23  #
  24  log.addHandler( logging.handlers.SMTPHandler( "emailserver", "fromname", "toname", "subject"))
  25  
  26  try:
  27          h = wang
  28  except:
  29          log.exception( "oopsy daisy")

The output looks like this:

INFO:Test:Blah
ERROR:Test:oopsy daisy
Traceback (most recent call last):
  File "\Tmp\try.py", line 13, in ?
    h = wang
NameError: name 'wang' is not defined

I like putting prints in my scripts and I never get round to tidying them up. Maybe I could get into the habit of using this.


Filed under: python

Comments are Closed