DEV Community

Srinivasulu Paranduru
Srinivasulu Paranduru

Posted on β€’ Edited on

Python : Part 4 - Logging

Logging facility for Python

##### Logging in Python - Table Of Contents
- Format 1
- Format 2

  • This module defines functions and classes which implement a flexible event logging system for applications and libraries.
  • To work with logging, need to logging module and basicConfig to be added

Format 1 - with out log file

import logging
logging.basicConfig(level=logging.DEBUG,format='%(asctime)s - %(levelname)s - %(message)s')
Enter fullscreen mode Exit fullscreen mode

Format 2-with log file

import logging
logging.basicConfig(filename='mylog.txt',level=logging.DEBUG,format='%(asctime)s - %(levelname)s - %(message)s')
Enter fullscreen mode Exit fullscreen mode

%s in a string format in Python
Basically, the % symbol is used with a large variety of data having many data types and configurations in Python.

Talking about %s, it is specifically used to perform concatenation of two or more strings together in Python. The %s allow us to format or place a string or numerical value within a given string. In simple language, the %s in Python is used to incorporate a given string within another string.

Example:

# Define a string value   
str = "JavaTpoint!"  
# using %s to appending string  
print("Hello Python developers! Welcome to, %s!" % str)  
Enter fullscreen mode Exit fullscreen mode

Example : Buggy Factorial Program

Scenario :1
#fact.py
def factorial (n):
    total = 1
    for i in range(n+1):
         total * = i
    return total

print(factorial(5))
Enter fullscreen mode Exit fullscreen mode

o/p : 0

Scenario :2
import logging
logging.basicConfig(level=logging.DEBUG,format='%(asctime)s - %(levelname)s - %(message)s')
#logging.basicConfig(filename='mylog.txt' level=logging.DEBUG,format='%(asctime)s - %(levelname)s - %(message)s')

#logging.disable(logging.CRITICAL) # it will disable all messages
# if specific messages to be disabled then try below
#logging.disable(logging.WARNING) 

logging.debug('Start of program')
def factorial (n):
    total = 1
    for i in range(n+1):
    #for i in range(1,n+1):
         total *= i
     logging.debug('i is %s, total is %s' % (i,total))
    logging.debug('Return value is %s' % (total))
    return total

print(factorial(5))
logging.debug('End of program')
Enter fullscreen mode Exit fullscreen mode

o/p : 120

Log Levels

  • debug(lowest)
  • info
  • warning
  • error
  • critical(highest)

Recap :

  • The logging module lets you display logging messages.
  • Log messages create a "breadcrumb trail" of what your program is doing.
  • After calling logging.basicConfig() to set up logging, call logging.debug(β€˜This is the message') to create a log message.
  • When done, you can disable the log messages with logging.disable(logging.CRITICAL)
  • Don't use print() for log messages: It's hard to remove the mall when you're done debugging.
  • The five log levels are: DEBUG, INFO, WARNING, ERROR, and CRITICAL.
  • You can also log to a file instead of the screen with the filename keyword argument in the logging.basicConfig() function.

*References *: https://www.javatpoint.com/python-s-string-formatting#:~:text=Talking%20about%20%25s%2C%20it%20is,given%20string%20within%20another%20string.

Conclusion : Discussed how to use logging in python and used IDLE shell command for running the python code

πŸ’¬ If you enjoyed reading this blog post and found it informative, please take a moment to share your thoughts by leaving a review and liking it πŸ˜€ and share this blog with ur friends and follow me in linkedin

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more