Logging in Python
Hello all,
This is the ninth article in the series Python for Data Science. If you are new to this series, we would recommend you to read our previous articles
- Python for Data Science Series - Part 1
- Python for Data Science Series - Part 2
- Using Numpy in Python
- Using Pandas in Python
- Data Visualization using Matplotlib Python
- Parsing XML file in python
- Parsing Json file in python
- Interactive Data Visualization using Plotly in Python
Please refer the videos below for detailed explanation on how to use logging in Python
Please refer the following code to understand on how to implement logging in python
In [1]:
import logging
import os
In [2]:
main_folder_path = r"E:\openknowledgeshare.blogspot.com\Python\Outputs"
log_file_path = os.path.join(main_folder_path,'mylogfile.log')
logging.basicConfig(filename=log_file_path, filemode='w', format='%(name)s - %(levelname)s - %(message)s',level=logging.INFO)
In [3]:
logging.debug('This is a debug message')
logging.info('This is an info message')
logging.warning('This is a warning message')
logging.error('This is an error message')
logging.critical('This is a critical message')
a = 5
b = 0
try:
c = a / b
except Exception as e:
logging.error("Exception occurred", exc_info=True)
Viewing Log File¶
In [4]:
f = open(log_file_path,'r')
log_data = f.readlines()
f.close()
for each_line in log_data:
print(each_line)
In [ ]:
Comments
Post a Comment