Logging the Exceptions:

It is highly recommended to store complete application flow and exception information to a file. This process is called logging.

The main advantages of logging are:

  1.  We can use log files while performing debugging
  2. We can provide statistics like number of requests per day etc

To implement logging, Python provides one inbuilt module logging.

logging levels:

Depending on type of information, logging data is divided according to the following 6 

levels in Python.


  1. CRITICAL==>50==>Represents a very serious problem that needs high attention
  2. ERROR===>40===>Represents a serious error
  3. WARNING==>30==>Represents a warning message, some caution needed. It is alert to the programmer
  4. INFO===>20===>Represents a message with some important information
  5. DEBUG===>10==>Represents a message with debugging information
  6. NOTSET==>0==>Rrepresents that the level is not set.

By default while executing Python program only WARNING and higher level messages will be displayed.

How to implement logging:

To perform logging, first we required to create a file to store messages and we have to specify which level messages we have to store.

We can do this by using basicConfig() function of logging module.


The above line will create a file log.txt and we can store either WARNING level or higher level messages to that file.

After creating log file, we can write messages to that file by using the following methods.


How to write Python program exceptions to the log file:

By using the following function we can write exceptions information to the log file.


Q. Python Program to write exception information to the log file

1) import logging 
2) logging.basicConfig(filename='mylog.txt',level=logging.INFO) 
3) logging.info("A New request Came:") 
4) try: 
5) x=int(input("Enter First Number: ")) 
6) y=int(input("Enter Second Number: ")) 
7) print(x/y) 
8) except ZeroDivisionError as msg: 
9) print("cannot divide with zero") 
10) logging.exception(msg) 
11) except ValueError as msg: 
12) print("Enter only int values") 
13) logging.exception(msg) 
14) logging.info("Request Processing Completed") 
17) D:\Python_classes>py test.py 
18) Enter First Number: 10 
19) Enter Second Number: 2 
20) 5.0 
22) D:\Python_classes>py test.py 
23) Enter First Number: 10 
24) Enter Second Number: 0 
25) cannot divide with zero 
27) D:\Python_classes>py test.py 
28) Enter First Number: 10 
29) Enter Second Number: ten 
30) Enter only int values 


1) INFO:root:A New request Came: 
2) INFO:root:Request Processing Completed 
3) INFO:root:A New request Came: 
4) ERROR:root:division by zero 
5) Traceback (most recent call last): 
6) File "test.py", line 7, in <module> 
7) print(x/y) 
8) ZeroDivisionError: division by zero 
9) INFO:root:Request Processing Completed 
10) INFO:root:A New request Came: 
11) ERROR:root:invalid literal for int() with base 10: 'ten' 
12) Traceback (most recent call last): 
13) File "test.py", line 6, in <module> 
14) y=int(input("Enter Second Number: ")) 
15) ValueError: invalid literal for int() with base 10: 'ten' 
16) INFO:root:Request Processing Completed 



Debugging Python Program by using assert keyword:

The process of identifying and fixing the bug is called debugging.

Very common way of debugging is to use print() statement. But the problem with the print() statement is after fixing the bug,compulsory we have to delete the extra added print() statments,otherwise these will be executed at runtime which creates performance problems and disturbs console output.

To overcome this problem we should go for assert statement. The main advantage of assert statement over print() statement is after fixing bug we are not required to delete assert statements. Based on our requirement we can enable or disable assert statements.

Hence the main purpose of assertions is to perform debugging. Usully we can perform debugging either in development or in test environments but not in production environment. Hence assertions concept is applicable only for dev and test environments but not for production environment.

Types of assert statements:

There are 2 types of assert statements

  1. Simple Version
  2. Augmented Version

1. Simple Version:

assert conditional_expression

2. Augmented Version:

assert conditional_expression,message

conditional_expression will be evaluated and if it is true then the program will be continued.

If it is false then the program will be terminated by raising AssertionError. 

By seeing AssertionError, programmer can analyze the code and can fix the problem.


1) def squareIt(x): 
2) return x**x
3) assert squareIt(2)==4,"The square of 2 should be 4" 
4) assert squareIt(3)==9,"The square of 3 should be 9" 
5) assert squareIt(4)==16,"The square of 4 should be 16" 
6) print(squareIt(2)) 
7) print(squareIt(3)) 
8) print(squareIt(4)) 
10) D:\Python_classes>py test.py 
11) Traceback (most recent call last): 
12) File "test.py", line 4, in <module> 
13) assert squareIt(3)==9,"The square of 3 should be 9" 
14) AssertionError: The square of 3 should be 9 
16) def squareIt(x): 
17) return x*x 
18) assert squareIt(2)==4,"The square of 2 should be 4" 
19) assert squareIt(3)==9,"The square of 3 should be 9" 
20) assert squareIt(4)==16,"The square of 4 should be 16" 
21) print(squareIt(2)) 
22) print(squareIt(3)) 
23) print(squareIt(4)) 
25) Output
26) 4 
27) 9 
28) 16

Exception Handling vs Assertions:

The assertions concept can be used to alert programmers to resolve development time errors.

  • Exception Handling can be used to handle runtime errors.

"Python Logging"

"Python Logging Example"

"Python Logging To File"

"Python Logging Set Level"

"Python Logging Exception"

"Python Logging Config"

"Python Logging To Console"

"Python Logging Filehandler"

"Python Logging Timestamp"

"Python Multiprocessing Logging"