How to log messages in Pyhon

Published on Aug. 22, 2023, 12:15 p.m.

In Python, you can use the built-in logging module to log messages. Here’s a basic example:

import logging

logging.basicConfig(level=logging.INFO, format='%(asctime)s [%(levelname)s] %(message)s')

logging.info('This is an info message.')
logging.warning('This is a warning message.')
logging.error('This is an error message.')

In this example, the basicConfig function is used to configure the logging module, with the level parameter setting the minimum level of messages to log to INFO, and format parameter setting the format of the log messages. Different logging methods, with different levels of severity, are used to log different messages. The log messages output in accordance with the specified formatting.

You can also log to a file. More on this can be found in the logging module documentation.

example

Here’s an example of how to use the logging module in Python:

import logging

# Configuring the logger
logging.basicConfig(filename='example.log', level=logging.DEBUG)

# Logging messages of different levels
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')

In this example, we first configure the logging module, setting the filename and the minimum logging level. Then, we log messages at different levels using methods such as debug(), info(), warning(), error(), and critical().

The log messages will be written to a file called example.log in the current directory. You can also specify other types of handlers to log messages to different destinations, such as the console or a remote server.

To clean up old log files generated by the logging module in Python

To clean up old log files generated by the logging module in Python, you can use a combination of file rotation and deletion.

One common way to rotate log files is to use the RotatingFileHandler class from the logging.handlers module. This handler can be used to rotate log files based on their size or the number of files that should be kept. Here’s an example:

import logging
from logging.handlers import RotatingFileHandler

logger = logging.getLogger(__name__)
handler = RotatingFileHandler('example.log', maxBytes=1000000, backupCount=10)
logger.addHandler(handler)

logger.info('This is an info message')

In this example, we create a RotatingFileHandler with a maximum file size of 1 MB and 10 backups. When the log file reaches the maximum size, the current log file is renamed with a .1 extension, and the .1 file is renamed with a .2 extension, and so on, until the oldest file with the .10 extension is overwritten.

Once the log files have been rotated, you can delete the old log files using a script that runs periodically, or using a cron job on Linux or macOS. Here’s an example of a script that deletes log files older than 7 days:

import os
import glob
import time

log_path = '/logs/'
now = time.time()

for file in glob.glob(log_path + '*.log'):
    if os.stat(file).st_mtime < now - 7 * 86400:
        os.remove(file)

In this example, we use the glob module to find all log files in the /logs/ directory, and we check their modification time. If the file was last modified more than 7 days ago (specified in seconds), we remove the file using the os.remove() function.

Tags: