http://www.skillbrew.com /Skillbrew Talent brewed by the industry itself Logging Pavan Verma @YinYangPavan 1 Founder, P3 InfoTech Solutions Pvt. Ltd. Python Programming Essentials
© SkillBrew http://skillbrew.com What is logging  Logging means tracking of events that happen when some software runs  An event is described by a descriptive message which can optionally contain variable data (i.e. data that is potentially different for each occurrence of the event) 2
© SkillBrew http://skillbrew.com Logging module  Python provides a logging module which provides a set of convenience functions for logging usage 3
© SkillBrew http://skillbrew.com Logging levels Level When it is used DEBUG Detailed information, typically of interest only when diagnosing problems INFO Confirmation that things are working as expected WARNING An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’) ERROR Due to a more serious problem, the software has not been able to perform some function CRITICAL A serious error, indicating that the program itself may be unable to continue running 4
© SkillBrew http://skillbrew.com Default logging level The INFO message doesn’t appear because the default level is WARNING >>> import logging >>> logging.warning('warning message') WARNING:root:warning message >>> logging.info('this is info') 5
© SkillBrew http://skillbrew.com Logging to a file basicConfig() does basic configuration for the logging system import logging logging.basicConfig(filename='example.log',level=logging .DEBUG) logging.debug('This message should go to the log file') logging.info('So should this') logging.warning('And this, too') 6
© SkillBrew http://skillbrew.com Setting up the Logging level While setting up the configuration you can set the logging level import logging logging.basicConfig(filename='example.log',level=logging .DEBUG) logging.debug('This message should go to the log file') logging.info('So should this') logging.warning('And this, too') 7
© SkillBrew http://skillbrew.com Logging variable data import logging logging.basicConfig(filename='example.log') logging.warning(‘{0} before you {1}‘.format('look', 'leap')) WARNING:root:look before you leap Contents of example.log level user message 8
© SkillBrew http://skillbrew.com Displaying the date/time in messages import logging logging.basicConfig(format='%(asctime)s %(message)s') logging.warning('is when this event was logged.') Output: 2013-10-28 03:26:13,832 is when this event was logged. 9
© SkillBrew http://skillbrew.com Changing the format of displayed messages import logging logging.basicConfig(format='%(levelname)s:%(message)s') logging.warning('And this, too') Output: WARNING:And this, too 10
© SkillBrew http://skillbrew.com Summary  What is logging  Logging module introduction  Logging levels  Default logging level  Logging to a file  Setting up the logging level  Logging variable data  Display date/time in messages  Changing format of messages 11
© SkillBrew http://skillbrew.com References  http://docs.python.org/2/howto/logging.html #logging-basic-tutorial 12

Python Programming Essentials - M27 - Logging module

  • 1.
    http://www.skillbrew.com /Skillbrew Talent brewed bythe industry itself Logging Pavan Verma @YinYangPavan 1 Founder, P3 InfoTech Solutions Pvt. Ltd. Python Programming Essentials
  • 2.
    © SkillBrew http://skillbrew.com Whatis logging  Logging means tracking of events that happen when some software runs  An event is described by a descriptive message which can optionally contain variable data (i.e. data that is potentially different for each occurrence of the event) 2
  • 3.
    © SkillBrew http://skillbrew.com Loggingmodule  Python provides a logging module which provides a set of convenience functions for logging usage 3
  • 4.
    © SkillBrew http://skillbrew.com Logginglevels Level When it is used DEBUG Detailed information, typically of interest only when diagnosing problems INFO Confirmation that things are working as expected WARNING An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’) ERROR Due to a more serious problem, the software has not been able to perform some function CRITICAL A serious error, indicating that the program itself may be unable to continue running 4
  • 5.
    © SkillBrew http://skillbrew.com Defaultlogging level The INFO message doesn’t appear because the default level is WARNING >>> import logging >>> logging.warning('warning message') WARNING:root:warning message >>> logging.info('this is info') 5
  • 6.
    © SkillBrew http://skillbrew.com Loggingto a file basicConfig() does basic configuration for the logging system import logging logging.basicConfig(filename='example.log',level=logging .DEBUG) logging.debug('This message should go to the log file') logging.info('So should this') logging.warning('And this, too') 6
  • 7.
    © SkillBrew http://skillbrew.com Settingup the Logging level While setting up the configuration you can set the logging level import logging logging.basicConfig(filename='example.log',level=logging .DEBUG) logging.debug('This message should go to the log file') logging.info('So should this') logging.warning('And this, too') 7
  • 8.
    © SkillBrew http://skillbrew.com Loggingvariable data import logging logging.basicConfig(filename='example.log') logging.warning(‘{0} before you {1}‘.format('look', 'leap')) WARNING:root:look before you leap Contents of example.log level user message 8
  • 9.
    © SkillBrew http://skillbrew.com Displayingthe date/time in messages import logging logging.basicConfig(format='%(asctime)s %(message)s') logging.warning('is when this event was logged.') Output: 2013-10-28 03:26:13,832 is when this event was logged. 9
  • 10.
    © SkillBrew http://skillbrew.com Changingthe format of displayed messages import logging logging.basicConfig(format='%(levelname)s:%(message)s') logging.warning('And this, too') Output: WARNING:And this, too 10
  • 11.
    © SkillBrew http://skillbrew.com Summary What is logging  Logging module introduction  Logging levels  Default logging level  Logging to a file  Setting up the logging level  Logging variable data  Display date/time in messages  Changing format of messages 11
  • 12.
    © SkillBrew http://skillbrew.com References http://docs.python.org/2/howto/logging.html #logging-basic-tutorial 12