Logging In Python

In this notebook we will learn how to do logging in python. Logging is very important once the project grows beyond a POC stage. Having a logging leads to better debugging, proper error identification, good standard to have. So we'll going to see how to apply logging through few examples

Logging has differnt configuration, which are important in implementing logging,

  1. level
  2. formatter
  3. Handlers
  4. Filters
Logging Level
  1. DEBUG - Detailed information, typically of interest only when diagnosing problems.
  2. INFO - Confirmation that things are working as expected.
  3. WARNING - An 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.
  4. ERROR - Due to a more serious problem, the software has not been able to perform some function.
  5. CRITICAL - A serious error, indicating that the program itself may be unable to continue running.
Level Numeric value
=====================
CRITICAL  50
ERROR     40
WARNING   30
INFO      20
DEBUG     10
NOTSET    0
Logging Formatter

logging formatter helps formating the message which we write to disk or console. Formatters specify the layout of log records in the final output,

Example,

  1. asctime - %(asctime)s
  2. created - %(created)f
  3. filename - %(filename)s
Logging Handlers

Handlers send the log records (created by loggers) to the appropriate destination. Filters provide a finer grained facility for determining which log records to output.

  1. queue_handler = QueueHandler(que)
  2. stream_handler = logging.StreamHandler()
  3. file_handler = logging.FileHandler("csr_logfile.log")

References