Nearby lessons

86 of 159

Python - Logging

📌 What You Will Learn
  • Understand why storing application flow and exception information is important
  • Know the six logging levels and their severity values
  • Configure logging with basicConfig(filename=..., level=...)
  • Write log messages using debug, info, warning, error, and critical
  • Write exception information into the log file with logging.exception()

Python Logging

It is highly recommended to store the complete application flow and exception information in a file.

This process is called Logging.

Advantages of Logging

  1. We can use log files while performing debugging.
  2. We can provide statistics such as the number of requests per day.

Logging Module

To implement Logging, Python provides one inbuilt module.

🐍Code Cell
1import logging
Output
No output captured.

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 are 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:

  1. Create a file to store log messages.
  2. Specify which level of messages should be stored.

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

🐍Code Cell
1logging.basicConfig(
2 filename='log.txt',
3 level=logging.WARNING
4)
Output
No output captured.

Explanation

  • filename='log.txt' — creates a log file named log.txt.
  • level=logging.WARNING — stores WARNING level messages and all higher level messages.

Methods Used to Write Log Messages

After creating the log file, we can write messages by using the following methods:

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

Program to Create a Log File and Write WARNING and Higher Level Messages

Write a Python program to create a log file and write WARNING and higher level messages.

🐍Code Cell
1import logging
2 
3logging.basicConfig(filename='log.txt', level=logging.WARNING)
4 
5print("Logging Module Demo")
6 
7logging.debug("This is debug message")
8logging.info("This is info message")
9logging.warning("This is warning message")
10logging.error("This is error message")
11logging.critical("This is critical message")
Output
Logging Module Demo

log.txt (WARNING Level)

After executing the above program, the log.txt file contains:

WARNING:root:This is warning message
ERROR:root:This is error message
CRITICAL:root:This is critical message

Explanation

The print() statement writes Logging Module Demo to the console only — it is not written to the log file.

Because the logging level is set to WARNING, only WARNING, ERROR, and CRITICAL messages are stored:

Log Method Stored in log.txt?
logging.debug() ❌ No
logging.info() ❌ No
logging.warning() ✅ Yes
logging.error() ✅ Yes
logging.critical() ✅ Yes

Program Using DEBUG Level

If we set the logging level to DEBUG, then all messages will be written to the log file.

🐍Code Cell
1import logging
2import sys
3 
4logger = logging.getLogger("debug_demo")
5logger.setLevel(logging.DEBUG)
6logger.handlers.clear()
7 
8handler = logging.StreamHandler(sys.stdout)
9handler.setLevel(logging.DEBUG)
10handler.setFormatter(logging.Formatter("%(levelname)s:%(name)s:%(message)s"))
11logger.addHandler(handler)
12 
13logger.debug("This is debug message")
14logger.info("This is info message")
15logger.warning("This is warning message")
16logger.error("This is error message")
17logger.critical("This is critical message")
Output
DEBUG:debug_demo:This is debug message
INFO:debug_demo:This is info message
WARNING:debug_demo:This is warning message
ERROR:debug_demo:This is error message
CRITICAL:debug_demo:This is critical message

log.txt (DEBUG Level)

DEBUG:root:This is debug message
INFO:root:This is info message
WARNING:root:This is warning message
ERROR:root:This is error message
CRITICAL:root:This is critical message

Comparison

Logging Level Messages Written to log.txt
logging.WARNING WARNING, ERROR, CRITICAL
logging.DEBUG DEBUG, INFO, WARNING, ERROR, CRITICAL

How to Write Python Program Exceptions to the Log File

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

🐍Code Cell
1logging.exception(msg)
Output
No output captured.

Python Program to Write Exception Information to the Log File

Write a Python program to write exception information to the log file.

🐍Code Cell
1import logging
2 
3logging.basicConfig(filename='mylog.txt', level=logging.INFO)
4 
5logging.info("A New request Came:")
6 
7try:
8 x = int(input("Enter First Number: "))
9 y = int(input("Enter Second Number: "))
10 print(x / y)
11 
12except ZeroDivisionError as msg:
13 print("cannot divide with zero")
14 logging.exception(msg)
15 
16except ValueError as msg:
17 print("Enter only int values")
18 logging.exception(msg)
19 
20logging.info("Request Processing Completed")
Output
No output captured.

Program Execution

Case 1 - input 10 and 2:

5.0

No exception occurs, so the division is performed successfully.

Case 2 - input 10 and 0:

cannot divide with zero

A ZeroDivisionError is raised. The program prints cannot divide with zero and writes the complete exception information to mylog.txt by using logging.exception(msg).

Case 3 - input 10 and ten:

Enter only int values

A ValueError is raised. The program prints Enter only int values and stores the complete exception information in mylog.txt.

Purpose of logging.info() and logging.exception()

  • logging.info("A New request Came:") writes a message when a new request starts.
  • logging.info("Request Processing Completed") writes a message when the request finishes.
  • logging.exception(msg) writes the full exception information into the log file whenever an exception occurs.

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
📝 Key Takeaways
  • Logging stores application flow and exception details in a file instead of only printing to the console
  • The default logging level is WARNING - only WARNING and higher messages appear
  • basicConfig(filename, level) creates the log file and sets the minimum level to record
  • print() goes to the console only; logging methods write to the log file
  • logging.exception(msg) writes the full exception details into the log file from inside an except block

🧠 Test Your Knowledge

10 Questions
Progress: 0 / 10