Basics: Logging in Python

T K Sourab

17th March, 2018

Why logging?

  1. What happened?
  2. When it happened?
  3. Where it happened?
  4. How important is it?

Why logging?

YOU ARE NOT THE ONLY ONE USING YOUR APPLICATION.

(Unless it's a college project or written in perl)

Python logging module

  • Released in 2002
  • since Python 2.3
  • Was built keeping in mind the Apache Log4j project

Features

  • EXTREMELY configurable
  • Supports different destination with different formats
  • Triggers or alerts on specific condition
  • Performance metric

TERMS

  • Logging levels
  • Logger
  • LogRecord
  • Handlers
    • Formatters

Logging Levels

  • DEBUG Detailed information, of no interest when everything is working well but invaluable when diagnosing problems.

  • INFO Affirmations that things are working as expected, e.g. "service has started" or "indexing run complete". Often ignored.

  • WARNING There may be a problem in the near future, and this gives advance warning of it. But the application is able to proceed normally.

  • ERROR The application has been unable to proceed as expected, due to the problem being logged

  • CRITICAL This is a serious error, and some kind of application meltdown might be imminent.

     

Logger

  • Controls handlers and namespaces

LogRecord

  • Contains attributes of the message

Formatter

  • Formats the LogRecord to string format.

Handlers

  • Specifies where to send the logRecord.

How logging works?

Ways to configure Logging

  1. Manually in code
  2. FileConfig
  3. DictConfig

Resources

https://docs.python.org/3.5/library/logging.html

Shukriya

@sourabhtk37

sourabhtk37@gmail.com

github.com/sourabhtk37

Basics: Logging in Python

By tk sourabh

Basics: Logging in Python

Talk on logging in python

  • 2,287