22
Lesson 22
Log file management
Objective
By the end of this lesson, students will understand how to build a log system using file handling in Python. They will learn how to create, write to, and manage log files to record application events and errors effectively.
1. Introduction to log files:
Log files are essential for tracking events, errors, and system performance in applications. They provide a way to diagnose issues, monitor activity, and maintain an audit trail.
Proper log management helps in debugging, monitoring application performance, and ensuring security.
It’s crucial to follow best practices for log file creation, maintenance, and rotation.
2. Creating a simple Log file:
Use the open() function in write mode to create a log file.
By the end of this lesson, students will understand how to build a log system using file handling in Python. They will learn how to create, write to, and manage log files to record application events and errors effectively.
1. Introduction to log files:
Log files are essential for tracking events, errors, and system performance in applications. They provide a way to diagnose issues, monitor activity, and maintain an audit trail.
Proper log management helps in debugging, monitoring application performance, and ensuring security.
It’s crucial to follow best practices for log file creation, maintenance, and rotation.
2. Creating a simple Log file:
Use the open() function in write mode to create a log file.
log_file = open('application.log', 'w') log_file.write('Log file created.\n') log_file.close()
3. Writing to a Log file:
Implement a function to write log messages to the file, including timestamps for better tracking.
import datetime def log_message(message): with open('application.log', 'a') as log_file: # Open in append mode timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') log_file.write(f'[{timestamp}] {message}\n') log_message('Application started.') log_message('An event occurred.')
4. Log levels:
Introduce different log levels (e.g., INFO, WARNING, ERROR) to categorize log messages.
def log_message(level, message): with open('application.log', 'a') as log_file: timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') log_file.write(f'[{timestamp}] [{level}] {message}\n') log_message('INFO', 'Application started.') log_message('ERROR', 'An error occurred.')
5. Reading log files:
Show how to read from log files to review logged events.
def read_log_file(): with open('application.log', 'r') as log_file: content = log_file.read() print(content) read_log_file()
6. Rotating log files:
Discuss the importance of log rotation to prevent log files from becoming too large.
Introduce a method for rotating logs based on size or date.
import os def rotate_log_file(): if os.path.exists('application.log') and os.path.getsize('application.log') > 1024 * 1024: # 1MB os.rename('application.log', f'application_{datetime.datetime.now().strftime("%Y%m%d_%H%M%S")}.log') rotate_log_file() log_message('Log rotation executed.')
7. Best Practices for log management:
- Use clear and descriptive messages for easier troubleshooting.
- Regularly archive and rotate log files to manage disk space.
- Implement appropriate log levels and formats for clarity.
8. Exercises:
Exercise 1: Implement a basic log system
1. Create a simple log system that logs messages with timestamps.
Exercise 2: Log with different levels
1. Modify the logging function to accept different log levels and format the output accordingly.
Exercise 3: Read and display log content
1. Write a function to read and display the last 10 entries from the log file.
Exercise 4: Implement log rotation
1. Create a log rotation function that renames the log file when it exceeds a specified size and starts a new log file.
Exercise 5: Filter logs by level
1. Implement a function to filter and display log messages based on their level.
Conclusion
In this lesson, students learned how to build a log management system using file handling in Python. They explored how to create and write log files, categorize log messages by levels, and manage log file size through rotation. Mastering log file management is essential for effective application monitoring, debugging, and performance evaluation.