Logging is an essential aspect of any application, providing insights into the application's behavior, aiding in debugging, and monitoring system health. While Python’s built-in logging module is versatile, there are scenarios where a custom log generator can provide additional flexibility.
In this post, we will create a custom log generator helper class in Python to streamline and enhance your logging process.
Why Custom Logging?
A custom log generator can offer several benefits:
- Simplify configuration and setup.
- Standardize log formats and file naming.
- Automatically handle log rotation based on file size.
- Make it easy to include additional context in logs.
Building the Custom Log Generator Helper Class
Step 1: Import Required Modules
First, import the necessary modules. We’ll use os for handling file operations and datetime for timestamping.
import os
import datetime
Step 2: Define the CustomLogGenerator Class
Next, define the CustomLogGenerator class. This class will handle the logging configuration, log file management, and message logging.
class CustomLogGenerator:
def __init__(self, log_folder, file_prefix, max_file_size=1e6):
self.log_folder = log_folder
self.file_prefix = file_prefix
self.max_file_size = max_file_size # in bytes
# Create log folder if it doesn't exist
if not os.path.exists(self.log_folder):
os.makedirs(self.log_folder)
def _get_filename(self, date_str, file_number=None):
if file_number:
return f"{self.log_folder}/{self.file_prefix}{date_str}_{file_number}.log"
else:
return f"{self.log_folder}/{self.file_prefix}{date_str}.log"
def _get_next_file_number(self, date_str):
i = 1
while True:
log_file = self._get_filename(date_str, i)
if not os.path.exists(log_file):
return i
i += 1
def generate_log(self, message):
date_str = datetime.datetime.now().strftime("%d%m%Y")
log_file = self._get_filename(date_str)
next_file_number = 1
# Check if the log file exists and its size is more than the max_file_size
if os.path.exists(log_file) and os.path.getsize(log_file) > self.max_file_size:
while True:
log_file = self._get_filename(date_str, next_file_number)
if not os.path.exists(log_file) or os.path.getsize(log_file) <= self.max_file_size:
break
next_file_number += 1
# Write the log message to the log file
with open(log_file, 'a') as f:
timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
f.write(f"{timestamp} - {message}\n")
Step 3: Using the Custom Log Generator
Now, let's see how you can use this CustomLogGenerator in your application.
# Initialize the log generator
log_generator = CustomLogGenerator(log_folder='logs', file_prefix='app_log_', max_file_size=1e6)
# Generate some log messages
log_generator.generate_log('This is the first log message.')
log_generator.generate_log('This is the second log message.')
log_generator.generate_log('Another log message with more details.')
Conclusion
By creating a custom log generator helper class, you can streamline your logging process, ensure consistent log formatting, and handle log file rotation automatically. This class provides a flexible foundation for enhancing your application's logging capabilities.
Feel free to modify and extend this class to suit your specific needs. Happy logging!
Please find complete code here
Top comments (0)