Logging ======== => It is highly recommended to store the complete application flow and exception information to a file. This process is called as "logging". Ex: --- Telecom application Device ===================> Network User ===================> Call User1 ==================> Network =================> User2 => Advantages with logging are: 1) We can use log files while performing debugging. 2) We can provide statistics like: number of requests in a website number of logins number of registrations number of purchases etc. => To perform the logging in python, there is a built-in module named as "logging". Logging levels: ---------------- => based on type of information, logging data is divided into 6-levels: 1) CRITICAL ==> 50 ==> Very serious problem that need high attention. 2) ERROR ==> 40 ==> a serious error 3) WARNING ==> 30 ==> a warning message used as alert to the programmer, some caution needed. 4) INFO ==> 20 ==> a message with some important information. 5) DEBUG ==> 10 ==> a message with debugging information. 6) NOTSET ==> 0 ==> that the level is not set. Note: ----- By default while executing python application only WARNING and higher messages will be displayed. How to implement the logging ----------------------------- To perform logging: 1) We required to create a file to store messages and we have to specify which level messages we have to store. 2) We can do this by using basicConfig() function of logging module. 3) Syntax: logging.basicConfig(fileName = 'log.txt', level = logging.WARNING) By using the above syntax, we have created a log file which is named as "log.txt" which can store WARNING messages or higher level messages. 4) After the creating the log file: we need to use some methods to write messages into that log file: 1) logging.debug(message) 2) logging.info(message) 3) logging.WARNING(message) 4) logging.error(message) 5) logging.critical(message) Q: Write a python program to create a log file and write WARNING and higher level messages. import logging logging.basicConfig(filename = 'log.txt', level = logging.WARNING) print("Logging Module Demo...") logging.debug("This is debug message") logging.info("This is info message") logging.WARNING("This is warning message") logging.error("This is error message") logging.critical("This is critical message") How to write python program exception to the log file? ------------------------------------------------------- Syntax: logging.exception(msg) Ex: Python program to write exception information to the log file import logging logging.basicConfig(filename = 'myLog.txt', level = logging.INFO) logging.info("A new request came..") try: x = int(input("Enter first number:")) y = int(input("Enter second number:")) print(x/y) except Exception: print("cannot divide a number with zero") logging.exception(msg) logging.info("Request Processing Completed..") ======================================================================= Debugging ========== => In python, debugging we can perform by using "assertions". => there is a keyword named as "assert" to create "assertions". => Very common way of debugging is to use "print() statement". But the problem with the print() is after fixing the bug, compulsory we have to delete the extra added print() statements, otherwise these will be executed at run time which created performance issues and disturbs the console output. To overcome this we should go for "assert statement". => The main advantage of assert statement over the print() is after the fixing bug we are not required to delete assert statements. Based on our requirement we can enable or disable assert statements. => The main purpose of assertions is to perform debugging. Usually we can perform debugging either in development or in test environments but not in production environments. So, assertions are applicable only for dev and test environments but not for production environment. Types of assert statements: ---------------------------- => there are two types of assert statements: 1) Simple version 2) Augmented version Simple Version: assert conditional_expression Augmented version: assert conditional_expression, message Here: the conditional_expression is evaluated first: if it is "True": program will be continued if it is "False" program will be terminated by raising "AssertionError". By seeing the AssertionError, the programmer can analyze the code and can fix the problem. Ex: --- def squarelt(x): return x**x assert squarelt(2)==4,"The square of 2 should be 4" assert squarelt(3)==9,"The square of 3 should be 9" assert squarelt(4)==16, "The square of 4 should be 16" print(squarelt(2)) print(squarelt(3)) print(squarelt(4))