Untitled Document
Logging Basics - Logging to Files,Setting Levels and Formatting
1. Introduction Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ ***** 
# Introduction

# We have Five Different levels of Logging 

# 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') . The software is still working as expected 

# 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 .

# IMP The Default level of logging is Warning i.e it will log everything
# above WARNING i.e ERROR and CRITICAL 

       
2. Program - Simple Example for understading logging Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - Simple Example for understading logging 

import logging

num1 = 10
num2 = 20

add_result = num1 + num2
mul_result = num1 * num2
logging.warning(f"Sum of Two numbers {num1} + {num2} = add_result")
logging.info(f"Sum of Two numbers {num1} * {num2} = mul_result")
       
3. Program - Changing Logging Level using basicConfig() Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - Changing Logging Level using basicConfig()

# Since the default logging level is set to DEBUG infor statement was not printed 

import logging

logging.basicConfig(level=logging.DEBUG)

num1 = 10
num2 = 20

add_result = num1 + num2
mul_result = num1 * num2
logging.info(f"Sum of Two numbers {num1} * {num2} = mul_result")
logging.warning(f"Sum of Two numbers {num1} + {num2} = add_result")
       
4. Program - Storing Log information in log file Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - Storing Log information in log file  

import logging

logging.basicConfig(filename='test.log',level=logging.DEBUG)

num1 = 10
num2 = 20

add_result = num1 + num2
mul_result = num1 * num2
logging.info(f"Sum of Two numbers {num1} * {num2} = mul_result")
logging.warning(f"Sum of Two numbers {num1} + {num2} = add_result")
logging.debug(f"This is Simple Debug Statement")
       
5. Program - Adding Some formatting to our logging . Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - Adding Some formatting to our logging .
# In below example we will add time, log level and messages  

import logging

logging.basicConfig(filename='test.log',level=logging.DEBUG,
	                format='%(asctime)s:%(levelname)s:%(message)s')

num1 = 10
num2 = 20

add_result = num1 + num2
mul_result = num1 * num2
logging.info(f"Sum of Two numbers {num1} * {num2} = mul_result")
logging.warning(f"Sum of Two numbers {num1} + {num2} = add_result")
logging.debug(f"This is Simple Debug Statement")
       
Untitled Document