Nearby lessons
76 of 112Python - Introduction to Logging
Python Logging
Logging the Exceptions
It is highly recommended to store the complete application flow and exception information in a file.
This process is called Logging.
Advantages of Logging
The main advantages of Logging are:
- We can use log files while performing debugging.
- We can provide statistics such as the number of requests per day.
Logging Module
To implement Logging, Python provides one inbuilt module.
main.py
No output captured.
Explanation
This module is used to create log files and write log messages.
Logging Levels
Depending on the type of information, logging data is divided into the following 6 levels.
| Logging Level | Value | Description |
|---|---|---|
| CRITICAL | 50 | Represents a very serious problem that needs high attention. |
| ERROR | 40 | Represents a serious error. |
| WARNING | 30 | Represents a warning message. Some caution is needed. It alerts the programmer. |
| INFO | 20 | Represents a message with some important information. |
| DEBUG | 10 | Represents a message with debugging information. |
| NOTSET | 0 | Represents that the level is not set. |
Default Logging Level
By default, while executing a Python program, only WARNING level and higher level messages will be displayed.
That means, by default, messages of the following levels are shown:
- WARNING
- ERROR
- CRITICAL
Messages of these levels are not displayed by default:
- DEBUG
- INFO
How to Implement Logging
To perform Logging:
- Create a file to store log messages.
- Specify which level of messages should be stored.
We can do this by using the basicConfig() function of the logging module.
Syntax
main.py
No output captured.
Explanation
logging.basicConfig(filename='log.txt', level=logging.WARNING)
Explanation:
-
filename='log.txt'
- Creates a log file named
log.txt.
- Creates a log file named
-
level=logging.WARNING
- Stores WARNING level messages and all higher level messages.
What Does This Statement Do?
The following statement:
main.py
No output captured.
Methods Used to Write Log Messages
After creating the log file, we can write messages by using the following methods.
main.py
No output captured.
Purpose of Each Method
| Method | Purpose |
|---|---|
logging.debug() |
Writes a DEBUG message |
logging.info() |
Writes an INFO message |
logging.warning() |
Writes a WARNING message |
logging.error() |
Writes an ERROR message |
logging.critical() |
Writes a CRITICAL message |
Flow of Logging
Import logging module
│
▼
Configure Logging
(using basicConfig)
│
▼
Create Log File
│
▼
Write Log Messages
(debug/info/warning/error/critical)
This flow summarizes the Logging process described in the PDF.
Summary
| Topic | Description |
|---|---|
| Logging | Process of storing application flow and exception information in a file |
| Logging Module | logging |
basicConfig() |
Used to configure logging |
| Log File | Stores log messages |
| Default Level | WARNING |
| Logging Levels | CRITICAL, ERROR, WARNING, INFO, DEBUG, NOTSET |
| Methods | debug(), info(), warning(), error(), critical() |
Important Notes
- Logging is used to store application flow and exception information in a file.
- Log files are useful while debugging applications.
- Python provides the built-in
loggingmodule for implementing Logging. - Logging information is divided into six levels.
- By default, only WARNING and higher-level messages are displayed.
basicConfig()is used to configure the log file and logging level.filenamespecifies the name of the log file.levelspecifies which messages should be stored.- After configuring logging, messages can be written using the five logging methods provided in the PDF.
Program to Create a Log File and Write WARNING and Higher Level Messages
Question:
Write a Python program to create a log file and write WARNING and higher level messages.
Program
main.py
No output captured.
log.txt
After executing the above program, the log.txt file contains:
Explanation
Step 1
Import the logging module.
main.py
No output captured.
Step 2
Configure Logging.
main.py
No output captured.
Explanation
This creates the log.txt file and stores WARNING level and higher-level messages.
Step 3
Display a normal message on the console.
main.py
No output captured.
Explanation
This message is printed on the console only.
It is not written to the log file.
Step 4
Write log messages.
main.py
No output captured.
Which Messages Are Stored?
| Log Method | Stored in log.txt? |
|---|---|
logging.debug() |
❌ No |
logging.info() |
❌ No |
logging.warning() |
✅ Yes |
logging.error() |
✅ Yes |
logging.critical() |
✅ Yes |
Reason:
The logging level is set to WARNING.
Important Note
In the above program, only WARNING level and higher-level messages will be written to the log file.
Program Using DEBUG Level
If we set the logging level to DEBUG, then all messages will be written to the log file.
Program
main.py
No output captured.
log.txt
Explanation
Since the logging level is DEBUG, every logging message is written into the file.
That includes:
- DEBUG
- INFO
- WARNING
- ERROR
- CRITICAL
Note
We can format log messages to include:
- Date
- Time
- IP address of the client
- Other information
This is done at the advanced level.
Comparison
| Logging Level | Messages Written to log.txt |
|---|---|
logging.WARNING |
WARNING, ERROR, CRITICAL |
logging.DEBUG |
DEBUG, INFO, WARNING, ERROR, CRITICAL |
Summary
| Topic | Description |
|---|---|
logging.basicConfig() |
Configures the log file and logging level |
| WARNING Level | Stores WARNING, ERROR and CRITICAL messages |
| DEBUG Level | Stores all log messages |
print() |
Displays output on the console only |
| Log File | Stores messages according to the configured logging level |
Important Notes
logging.basicConfig()is used to configure the logging system.- When the logging level is WARNING, only WARNING and higher-level messages are stored.
- DEBUG and INFO messages are ignored when the level is WARNING.
- When the logging level is DEBUG, all logging messages are stored.
- The
print()function displays messages on the console and does not write them to the log file. - Log messages can be formatted to include additional information such as date, time, and client IP address at the advanced level.
How to Write Python Program Exceptions to the Log File
By using the following function, we can write exception information to the log file.
main.py
No output captured.
Python Program to Write Exception Information to the Log File
Question:
Write a Python program to write exception information to the log file.
Program
main.py
No output captured.
Program Execution – Case 1
Input
main.py
No output captured.
Output
Explanation
Execution Flow
Request Started
│
▼
Read First Number
│
▼
Read Second Number
│
▼
Division Successful
│
▼
Request Processing Completed
Since there is no exception, the division is performed successfully.
Program Execution – Case 2
Input
main.py
No output captured.
Output
Explanation
When the second number is 0, Python raises a ZeroDivisionError.
The program:
- Prints:
cannot divide with zero
- Writes complete exception information to
mylog.txtby using:
main.py
No output captured.
Program Execution – Case 3
Input
main.py
No output captured.
Output
Explanation
The input "ten" cannot be converted into an integer.
Python raises a ValueError.
The program:
- Prints:
Enter only int values
- Stores complete exception information in
mylog.txtby using:
main.py
No output captured.
Log File Used
The program stores logging information in the following file:
mylog.txt
The log file contains:
- Information messages.
- Exception details whenever an exception occurs.
Purpose of logging.info()
Writes a message when a new request starts.
main.py
No output captured.
Purpose of logging.exception()
This function writes exception information into the log file whenever an exception occurs.
main.py
No output captured.
Complete Program Flow
Start Program
│
▼
Configure Logging
│
▼
Write "A New request Came:"
│
▼
Read First Number
│
▼
Read Second Number
│
▼
Perform Division
│
├───────────────────────┐
│ │
▼ ▼
Success Exception Occurs
│ │
│ ├──► ZeroDivisionError
│ │ │
│ │ ▼
│ │ logging.exception(msg)
│ │
│ └──► ValueError
│ │
│ ▼
│ logging.exception(msg)
│
▼
Write "Request Processing Completed"
│
▼
End
Summary
| Topic | Description |
|---|---|
logging.exception() |
Writes exception information to the log file |
logging.info() |
Writes informational messages |
| Log File | mylog.txt |
| ZeroDivisionError | Logged using logging.exception() |
| ValueError | Logged using logging.exception() |
Important Notes
- Exception information can be written to a log file by using
logging.exception(msg). - In this program, the log file name is
mylog.txt. logging.info("A New request Came:")writes a message when a new request starts.logging.info("Request Processing Completed")writes a message after request processing is completed.- If a
ZeroDivisionErroroccurs, the exception information is written to the log file. - If a
ValueErroroccurs, the exception information is written to the log file. logging.exception()is used inside theexceptblocks to record exception details.