SlideShare a Scribd company logo
1 of 4
Logging the Exceptions:
It is highly recommended to store complete application flow and exceptions
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.
logging.basicConfig(filename='log.txt',level=logging.WARNING)
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.
logging.debug(message)
logging.info(message)
logging.warning(message)
logging.error(message)
logging.critical(message)
.Write a Python program to create a log file and write WARNING and higher
level messages?
EX:
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")
Note: In the above program only WARNING and higher level messages will be
written to log file.
If we set level as DEBUG then all messages will be written to log file.
Ex:
import logging
logging.basicConfig(filename='log.txt',
level=logging.DEBUG)
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")
Note: We can format log messages to include date and time, ip address of the
client etc at advanced level.
How to write Python program exceptions to the log file:
By using the following function we can write exceptions information to the
log file.
logging.exception(msg)
Ex:
Python Program to write exception information to the log file
EX:
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 ZeroDivisionError as msg:
print("cannot divide with
zero")
logging.exception(msg)
except ValueError as msg:
print("Enter only int values")
logging.exception(msg)
logging.info("Request Processing
Completed")

More Related Content

Similar to Logging in Python.docx

Merged document
Merged documentMerged document
Merged documentsreeja_16
 
Message, Debugging, File Transfer and Type Group
Message, Debugging, File Transfer and Type GroupMessage, Debugging, File Transfer and Type Group
Message, Debugging, File Transfer and Type Groupsapdocs. info
 
Automated Evolution of Feature Logging Statement Levels Using Git Histories a...
Automated Evolution of Feature Logging Statement Levels Using Git Histories a...Automated Evolution of Feature Logging Statement Levels Using Git Histories a...
Automated Evolution of Feature Logging Statement Levels Using Git Histories a...Raffi Khatchadourian
 
lecture 15.pptx
lecture 15.pptxlecture 15.pptx
lecture 15.pptxITNet
 
python lab programs.pdf
python lab programs.pdfpython lab programs.pdf
python lab programs.pdfCBJWorld
 
Chapter01_Python.ppt
Chapter01_Python.pptChapter01_Python.ppt
Chapter01_Python.pptPigPug1
 
Federico Soich - Upgrading Magento Version
Federico Soich - Upgrading Magento VersionFederico Soich - Upgrading Magento Version
Federico Soich - Upgrading Magento VersionMeet Magento Italy
 
Trouble shoot with linux syslog
Trouble shoot with linux syslogTrouble shoot with linux syslog
Trouble shoot with linux syslogashok191
 
LogMan 8.0 U S E R G U I D E
LogMan 8.0  U S E R  G U I D ELogMan 8.0  U S E R  G U I D E
LogMan 8.0 U S E R G U I D ENgo Hung Long
 
469-Porting the build system of a commercial RCP Application from Europa to G...
469-Porting the build system of a commercial RCP Application from Europa to G...469-Porting the build system of a commercial RCP Application from Europa to G...
469-Porting the build system of a commercial RCP Application from Europa to G...gustavoeliano
 
CSO Laboratory Manual
CSO Laboratory ManualCSO Laboratory Manual
CSO Laboratory ManualDwight Sabio
 
Mca pro1 online
Mca pro1 onlineMca pro1 online
Mca pro1 onlinerameshvvv
 
Log4cpp - Updated - to verify modify updates
Log4cpp - Updated - to verify modify updatesLog4cpp - Updated - to verify modify updates
Log4cpp - Updated - to verify modify updatesyosaggregator2
 

Similar to Logging in Python.docx (20)

Sap application log
Sap application logSap application log
Sap application log
 
Merged document
Merged documentMerged document
Merged document
 
Message, Debugging, File Transfer and Type Group
Message, Debugging, File Transfer and Type GroupMessage, Debugging, File Transfer and Type Group
Message, Debugging, File Transfer and Type Group
 
PYTHON PROGRAMMING NOTES RKREDDY.pdf
PYTHON PROGRAMMING NOTES RKREDDY.pdfPYTHON PROGRAMMING NOTES RKREDDY.pdf
PYTHON PROGRAMMING NOTES RKREDDY.pdf
 
Automated Evolution of Feature Logging Statement Levels Using Git Histories a...
Automated Evolution of Feature Logging Statement Levels Using Git Histories a...Automated Evolution of Feature Logging Statement Levels Using Git Histories a...
Automated Evolution of Feature Logging Statement Levels Using Git Histories a...
 
lecture 15.pptx
lecture 15.pptxlecture 15.pptx
lecture 15.pptx
 
python lab programs.pdf
python lab programs.pdfpython lab programs.pdf
python lab programs.pdf
 
Chapter01_Python.ppt
Chapter01_Python.pptChapter01_Python.ppt
Chapter01_Python.ppt
 
Mp lab manual
Mp lab manualMp lab manual
Mp lab manual
 
Federico Soich - Upgrading Magento Version
Federico Soich - Upgrading Magento VersionFederico Soich - Upgrading Magento Version
Federico Soich - Upgrading Magento Version
 
Trouble shoot with linux syslog
Trouble shoot with linux syslogTrouble shoot with linux syslog
Trouble shoot with linux syslog
 
LogMan 8.0 U S E R G U I D E
LogMan 8.0  U S E R  G U I D ELogMan 8.0  U S E R  G U I D E
LogMan 8.0 U S E R G U I D E
 
469-Porting the build system of a commercial RCP Application from Europa to G...
469-Porting the build system of a commercial RCP Application from Europa to G...469-Porting the build system of a commercial RCP Application from Europa to G...
469-Porting the build system of a commercial RCP Application from Europa to G...
 
CSO Laboratory Manual
CSO Laboratory ManualCSO Laboratory Manual
CSO Laboratory Manual
 
11i Logs
11i Logs11i Logs
11i Logs
 
Log4j in 8 slides
Log4j in 8 slidesLog4j in 8 slides
Log4j in 8 slides
 
Design
DesignDesign
Design
 
Mca pro1 online
Mca pro1 onlineMca pro1 online
Mca pro1 online
 
log4cpp-29/10
log4cpp-29/10log4cpp-29/10
log4cpp-29/10
 
Log4cpp - Updated - to verify modify updates
Log4cpp - Updated - to verify modify updatesLog4cpp - Updated - to verify modify updates
Log4cpp - Updated - to verify modify updates
 

More from manohar25689

Numpy in Python.docx
Numpy in Python.docxNumpy in Python.docx
Numpy in Python.docxmanohar25689
 
Multi Threading.docx
Multi Threading.docxMulti Threading.docx
Multi Threading.docxmanohar25689
 
Modules in Python.docx
Modules in Python.docxModules in Python.docx
Modules in Python.docxmanohar25689
 
controlstatementspy.docx
controlstatementspy.docxcontrolstatementspy.docx
controlstatementspy.docxmanohar25689
 
List Data Structure.docx
List Data Structure.docxList Data Structure.docx
List Data Structure.docxmanohar25689
 
File Handling in python.docx
File Handling in python.docxFile Handling in python.docx
File Handling in python.docxmanohar25689
 

More from manohar25689 (6)

Numpy in Python.docx
Numpy in Python.docxNumpy in Python.docx
Numpy in Python.docx
 
Multi Threading.docx
Multi Threading.docxMulti Threading.docx
Multi Threading.docx
 
Modules in Python.docx
Modules in Python.docxModules in Python.docx
Modules in Python.docx
 
controlstatementspy.docx
controlstatementspy.docxcontrolstatementspy.docx
controlstatementspy.docx
 
List Data Structure.docx
List Data Structure.docxList Data Structure.docx
List Data Structure.docx
 
File Handling in python.docx
File Handling in python.docxFile Handling in python.docx
File Handling in python.docx
 

Recently uploaded

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 

Recently uploaded (20)

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 

Logging in Python.docx

  • 1. Logging the Exceptions: It is highly recommended to store complete application flow and exceptions 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.
  • 2. logging.basicConfig(filename='log.txt',level=logging.WARNING) 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. logging.debug(message) logging.info(message) logging.warning(message) logging.error(message) logging.critical(message) .Write a Python program to create a log file and write WARNING and higher level messages? EX: 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")
  • 3. Note: In the above program only WARNING and higher level messages will be written to log file. If we set level as DEBUG then all messages will be written to log file. Ex: import logging logging.basicConfig(filename='log.txt', level=logging.DEBUG) 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") Note: We can format log messages to include date and time, ip address of the client etc at advanced level. How to write Python program exceptions to the log file: By using the following function we can write exceptions information to the log file. logging.exception(msg) Ex: Python Program to write exception information to the log file EX: import logging logging.basicConfig(filename='mylog.txt ',level=logging.INFO)
  • 4. logging.info("A New request Came:") try: x=int(input("Enter First Number: ")) y=int(input("Enter Second Number: ")) print(x/y) except ZeroDivisionError as msg: print("cannot divide with zero") logging.exception(msg) except ValueError as msg: print("Enter only int values") logging.exception(msg) logging.info("Request Processing Completed")