Python Logging

Python Logging

Logging refers to the process of recording events, activities and errors that occur in your application. Logging is essential in debugging, monitoring and enhancing security.

Python provides a logging module. This module makes work easier for developers as it enables developers to:

  • Identify the date and time of occurrence of a specific activity.

  • Show the severity level.

  • Allows users to save logs on files for tracking.

  • Find the source of the problem in code which could be extremely time-consuming.

Log levels

Logs have different levels of severity. This level ensures that what the developer wants to show matches the severity. Below is a list of levels and their values:

NOTSET: 0

This is the lowest logging, it captures all log messages.

DEBUG: 10

This is used for capturing detailed information during debugging and development. It gives insights into the internal workings of your application.

INFO: 20

It is used to capture general information about the applications progress and status, mostly used to convey different milestones.

WARNING: 30

This indicates potential issues that need attention. This is used for situations that are not necessarily errors.

ERROR: 40

This captures errors that can impact app functionality. This alerts you about conditions that require further investigation.

CRITICAL: 50

This is the highest level of logging. It is used for exceptional situations that require immediate attention. It indicates failures that could lead to application crashes.

Getting Started with Python logging

To install the Python logging module, add this command to your terminal

pip install logging

Import logging and initialize the logger module to your Python file

This allows you to use features of logging on your project.

import logging

logger = logging.getLogger(__name__)

To access the logging levels described above use the below methods:

logger.logLevel("This is the message")

or

logger.log(logging.logLevel,"This is the message")

#debug
logger.debug("This is a debug message")
logger.log(logging.DEBUG,"This is a debug message")
#info
logger.info("This is an info message")
logger.log(logging.INFO,"This is an info message")
#warning
logger.warning("This is a warning message")
logger.log(logging.WARNING,"This is a warningmessage")
#error
logger.error("This is an error message")
logger.log(logging.ERROR,"This is a error message")
#critical
logger.critical("This is a critical message")
logger.log(logging.CRITICAL,"This is a critical message")

Output Logs

It is essential to properly output logs. This could be on the user interface, on the console or a log file. This enables the developer to understand the activities in their application or on their scripts.

  • On Console

This displays the logs on your console or terminal.

To set this up:

import logging
# sys module provides acccess to the standard I/O streams
import sys

#creates a streamhandler that directs log messages to the console
stream_handler = logging.StreamHandler(sys.stdout)
#create logger instance
logger = logging.getLogger()

#adds the stramhandler to the logger
logger.addHandler(stream_handler)
  • On log file

This displays the logs on a specified file created. This prevents the logs from disappearing as compared to logs on the console(logs are lost as soon as the console is closed).

To set this up:

import logging
# sys module provides acccess to the standard I/O streams
import sys

#creates a streamhandler that directs log messages to a specified file
file_handler = logging.FileHandler("the_logfile.log")
#create logger instance
logger = logging.getLogger()

#adds the file handler to the logger
logger.addHandler(file_handler)

Important to note, that logs can be made on both the file and the console by instantiating both handlers.

Log Formatting

This allows you to control the appearance of the log messages. This includes important messages, timestamps and other details that make it more informative.

# Create a formatter for the log messages
log_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
# Apply the formatter to the StreamHandler
stream_handler.setFormatter(log_formatter)
# Apply the formatter to the FileHandler
file_handler.setFormatter(log_formatter)
#setting the logging level: logging_level depends on severity
logger.setLevel(logging.logging_level)

In the code snippet:

log_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s': creates a formatted using a specific format string. It defines how the messages should be structured. %(asctime)s hold the time stamp, %(levelname)s holds the log level and %(message)s is replaced by the actual log message.

.setFormatter(log_formatter): This adds the formatter to the handlers.

logger.setLevel(logging.logging_level): This sets the logging level, which means that log messages with a specified severity level and above will be displayed, e.g. for INFO, severity level INFO and above will be displayed.

Using 'BasicConfig' for Quick Logging Setup

basicConfig function provides a convenient way to set up logging with minimal configuration. This is mostly used for scripts that don't need advanced logging features.

# Set up logging using basicConfig
logging.basicConfig(filename='my_log.log', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

# Now you can use logging functions like logger.debug(), logger.info(), etc.

It is very important to note:

  • It can only be called once in your application, at the beginning. It works as a default configuration of the root logger.

  • The basicConfig function is convenient but does not cover all needs, especially in large applications.

  • The common file parameters include:

    • filename: This specifies where to save log messages.

    • level: This sets the log level for the root logger

    • format: Specifies the log message format.

In summary, the Python 'logging' module, is a poerful tool for managing and recording information about your applications behavior. It is not only a replacement for print statements but also plays an important role in understanding whats happening on your code.

Make logging an intergral part of your coding! Happy coding😊