Practical Example: Writing Script Execution Logs
๐ท๏ธ File Handling / Writing to Files
When you run scripts in production or during testing, it's essential to keep a record of what happened. Writing execution logs helps you track errors, monitor performance, and debug issues later. Instead of relying on print statements that disappear after the terminal closes, you can save all that information directly into a file.
โ๏ธ Why Log Script Execution?
Logging provides a permanent record of your script's activity. Here are the key benefits:
- Troubleshooting: Review past runs to find where things went wrong
- Audit Trail: Know who ran what and when
- Performance Monitoring: Track how long operations take
- Error Tracking: Capture exceptions with full context
- Automation Support: Let other systems read your logs for alerts
๐ ๏ธ Basic Logging Approach
The simplest way to write execution logs is to open a file and write messages as your script runs. You can use different log levels to categorize the importance of each message:
Log Level Hierarchy: - INFO โ General progress updates (e.g., "Processing file X") - WARNING โ Something unexpected but not critical (e.g., "Disk space low") - ERROR โ A failure occurred (e.g., "Could not connect to database") - CRITICAL โ Script cannot continue (e.g., "Missing configuration file")
Example Log Entry Format: 2025-03-15 14:30:22 | INFO | Starting data import process 2025-03-15 14:30:25 | WARNING | File 'data.csv' has 3 missing values 2025-03-15 14:30:30 | ERROR | Failed to write to output directory
๐ Writing a Simple Execution Log
Here is a practical pattern for writing logs manually without any external libraries:
Step 1: Open the log file in append mode Use open("script.log", "a") so each run adds new entries without deleting previous logs.
Step 2: Write a timestamped entry Combine the current time with your message using f-strings for clean formatting.
Step 3: Close the file after each write Use file.close() or the with statement to ensure data is saved immediately.
Example Workflow: - Open "execution.log" in append mode - Write "2025-03-15 14:30:22 | INFO | Script started" - Perform your main task - Write "2025-03-15 14:30:25 | INFO | Task completed successfully" - Close the file
๐ต๏ธ Adding Error Handling to Logs
When your script encounters an error, you want to capture the full details. Wrap risky operations in a try-except block and write the error message to your log:
Pattern: - try: Run the operation that might fail - except Exception as e: Write "ERROR | {str(e)}" to the log file - else: Write "INFO | Operation succeeded" if no error occurred - finally: Close the log file or ensure cleanup happens
Example Scenario: - Attempt to read a configuration file - If file is missing, log "ERROR | Config file not found: config.ini" - If file exists, log "INFO | Config file loaded successfully"
๐ Comparison: Print vs. File Logging
| Feature | Print Statements | File Logging |
|---|---|---|
| Persistence | Lost after terminal closes | Saved permanently |
| Searchability | Manual scrolling | Can grep or search |
| Multiple Runs | No history | Append to same file |
| Error Context | Limited to screen output | Full traceback possible |
| Automation | Requires human watching | Can trigger alerts |
๐งช Practical Script Structure
Here is a recommended structure for any script that needs logging:
1. Setup Phase - Define the log file path (e.g., "logs/script_20250315.log") - Open the file in append mode - Write an initial entry: "INFO | Script started at {current_time}"
2. Execution Phase - Perform your main logic - After each major step, write an INFO entry - If something goes wrong, write an ERROR entry with details
3. Cleanup Phase - Write a final entry: "INFO | Script completed at {current_time}" - Close the log file
๐ฏ Key Takeaways
- Writing logs to a file gives you a permanent record of script activity
- Use timestamps and log levels to make entries meaningful
- Append mode ("a") preserves history across multiple runs
- Always handle errors and log them with full context
- A simple with open() block ensures files close properly
Logging turns your scripts from black boxes into transparent, debuggable tools. Start with a basic file write pattern, and you can always upgrade to more advanced logging libraries later.
This example shows how to record what a Python script does by writing timestamped messages to a log file.
๐ Example 1: Writing a single log entry
Opens a log file and writes one message with a timestamp.
import datetime
log_file = open("script.log", "w")
timestamp = str(datetime.datetime.now())
log_file.write(timestamp + " - Script started\n")
log_file.close()
๐ค Output: script.log contains: 2025-04-08 14:30:00.123456 - Script started
๐ Example 2: Appending multiple log entries
Adds new log messages to an existing log file without erasing previous entries.
import datetime
log_file = open("script.log", "a")
timestamp = str(datetime.datetime.now())
log_file.write(timestamp + " - Processing data\n")
log_file.write(timestamp + " - Validation passed\n")
log_file.close()
๐ค Output: script.log now contains 3 lines, each with a timestamp and message
๐ Example 3: Logging with automatic file closing
Uses the with statement so the log file closes automatically after writing.
import datetime
with open("script.log", "a") as log_file:
timestamp = str(datetime.datetime.now())
log_file.write(timestamp + " - Step 1 complete\n")
log_file.write(timestamp + " - Step 2 complete\n")
๐ค Output: script.log has 2 new lines added, file closed automatically
๐ Example 4: Logging script start and end times
Records when a script begins and finishes, with a simple calculation in between.
import datetime
with open("script.log", "w") as log_file:
start_time = str(datetime.datetime.now())
log_file.write("START: " + start_time + "\n")
result = 5 + 3
log_file.write("Calculation result: " + str(result) + "\n")
end_time = str(datetime.datetime.now())
log_file.write("END: " + end_time + "\n")
๐ค Output: script.log contains 3 lines: start time, calculation result, end time
๐ Example 5: Logging with error tracking
Writes success and error messages to the log, showing which steps passed or failed.
import datetime
with open("script.log", "w") as log_file:
timestamp = str(datetime.datetime.now())
log_file.write(timestamp + " - Script execution started\n")
try:
number = int("42")
log_file.write(timestamp + " - Conversion successful: " + str(number) + "\n")
except ValueError as error:
log_file.write(timestamp + " - ERROR: " + str(error) + "\n")
try:
number = int("hello")
log_file.write(timestamp + " - Conversion successful: " + str(number) + "\n")
except ValueError as error:
log_file.write(timestamp + " - ERROR: " + str(error) + "\n")
log_file.write(timestamp + " - Script execution finished\n")
๐ค Output: script.log contains 4 lines: start, success, error message, finish
Comparison Table
| Feature | Example 1 | Example 2 | Example 3 | Example 4 | Example 5 |
|---|---|---|---|---|---|
| File mode | write (w) | append (a) | append (a) | write (w) | write (w) |
| Auto-close | No | No | Yes | Yes | Yes |
| Timestamps | Yes | Yes | Yes | Yes | Yes |
| Error handling | No | No | No | No | Yes |
| Number of entries | 1 | 2 | 2 | 3 | 4 |
When you run scripts in production or during testing, it's essential to keep a record of what happened. Writing execution logs helps you track errors, monitor performance, and debug issues later. Instead of relying on print statements that disappear after the terminal closes, you can save all that information directly into a file.
โ๏ธ Why Log Script Execution?
Logging provides a permanent record of your script's activity. Here are the key benefits:
- Troubleshooting: Review past runs to find where things went wrong
- Audit Trail: Know who ran what and when
- Performance Monitoring: Track how long operations take
- Error Tracking: Capture exceptions with full context
- Automation Support: Let other systems read your logs for alerts
๐ ๏ธ Basic Logging Approach
The simplest way to write execution logs is to open a file and write messages as your script runs. You can use different log levels to categorize the importance of each message:
Log Level Hierarchy: - INFO โ General progress updates (e.g., "Processing file X") - WARNING โ Something unexpected but not critical (e.g., "Disk space low") - ERROR โ A failure occurred (e.g., "Could not connect to database") - CRITICAL โ Script cannot continue (e.g., "Missing configuration file")
Example Log Entry Format: 2025-03-15 14:30:22 | INFO | Starting data import process 2025-03-15 14:30:25 | WARNING | File 'data.csv' has 3 missing values 2025-03-15 14:30:30 | ERROR | Failed to write to output directory
๐ Writing a Simple Execution Log
Here is a practical pattern for writing logs manually without any external libraries:
Step 1: Open the log file in append mode Use open("script.log", "a") so each run adds new entries without deleting previous logs.
Step 2: Write a timestamped entry Combine the current time with your message using f-strings for clean formatting.
Step 3: Close the file after each write Use file.close() or the with statement to ensure data is saved immediately.
Example Workflow: - Open "execution.log" in append mode - Write "2025-03-15 14:30:22 | INFO | Script started" - Perform your main task - Write "2025-03-15 14:30:25 | INFO | Task completed successfully" - Close the file
๐ต๏ธ Adding Error Handling to Logs
When your script encounters an error, you want to capture the full details. Wrap risky operations in a try-except block and write the error message to your log:
Pattern: - try: Run the operation that might fail - except Exception as e: Write "ERROR | {str(e)}" to the log file - else: Write "INFO | Operation succeeded" if no error occurred - finally: Close the log file or ensure cleanup happens
Example Scenario: - Attempt to read a configuration file - If file is missing, log "ERROR | Config file not found: config.ini" - If file exists, log "INFO | Config file loaded successfully"
๐ Comparison: Print vs. File Logging
| Feature | Print Statements | File Logging |
|---|---|---|
| Persistence | Lost after terminal closes | Saved permanently |
| Searchability | Manual scrolling | Can grep or search |
| Multiple Runs | No history | Append to same file |
| Error Context | Limited to screen output | Full traceback possible |
| Automation | Requires human watching | Can trigger alerts |
๐งช Practical Script Structure
Here is a recommended structure for any script that needs logging:
1. Setup Phase - Define the log file path (e.g., "logs/script_20250315.log") - Open the file in append mode - Write an initial entry: "INFO | Script started at {current_time}"
2. Execution Phase - Perform your main logic - After each major step, write an INFO entry - If something goes wrong, write an ERROR entry with details
3. Cleanup Phase - Write a final entry: "INFO | Script completed at {current_time}" - Close the log file
๐ฏ Key Takeaways
- Writing logs to a file gives you a permanent record of script activity
- Use timestamps and log levels to make entries meaningful
- Append mode ("a") preserves history across multiple runs
- Always handle errors and log them with full context
- A simple with open() block ensures files close properly
Logging turns your scripts from black boxes into transparent, debuggable tools. Start with a basic file write pattern, and you can always upgrade to more advanced logging libraries later.
Interactive Views
You are currently in ๐ All-in-One mode. Use the tabs at the top to switch to ๐ Theory Only or ๐ป Code Only views.
This example shows how to record what a Python script does by writing timestamped messages to a log file.
๐ Example 1: Writing a single log entry
Opens a log file and writes one message with a timestamp.
import datetime
log_file = open("script.log", "w")
timestamp = str(datetime.datetime.now())
log_file.write(timestamp + " - Script started\n")
log_file.close()
๐ค Output: script.log contains: 2025-04-08 14:30:00.123456 - Script started
๐ Example 2: Appending multiple log entries
Adds new log messages to an existing log file without erasing previous entries.
import datetime
log_file = open("script.log", "a")
timestamp = str(datetime.datetime.now())
log_file.write(timestamp + " - Processing data\n")
log_file.write(timestamp + " - Validation passed\n")
log_file.close()
๐ค Output: script.log now contains 3 lines, each with a timestamp and message
๐ Example 3: Logging with automatic file closing
Uses the with statement so the log file closes automatically after writing.
import datetime
with open("script.log", "a") as log_file:
timestamp = str(datetime.datetime.now())
log_file.write(timestamp + " - Step 1 complete\n")
log_file.write(timestamp + " - Step 2 complete\n")
๐ค Output: script.log has 2 new lines added, file closed automatically
๐ Example 4: Logging script start and end times
Records when a script begins and finishes, with a simple calculation in between.
import datetime
with open("script.log", "w") as log_file:
start_time = str(datetime.datetime.now())
log_file.write("START: " + start_time + "\n")
result = 5 + 3
log_file.write("Calculation result: " + str(result) + "\n")
end_time = str(datetime.datetime.now())
log_file.write("END: " + end_time + "\n")
๐ค Output: script.log contains 3 lines: start time, calculation result, end time
๐ Example 5: Logging with error tracking
Writes success and error messages to the log, showing which steps passed or failed.
import datetime
with open("script.log", "w") as log_file:
timestamp = str(datetime.datetime.now())
log_file.write(timestamp + " - Script execution started\n")
try:
number = int("42")
log_file.write(timestamp + " - Conversion successful: " + str(number) + "\n")
except ValueError as error:
log_file.write(timestamp + " - ERROR: " + str(error) + "\n")
try:
number = int("hello")
log_file.write(timestamp + " - Conversion successful: " + str(number) + "\n")
except ValueError as error:
log_file.write(timestamp + " - ERROR: " + str(error) + "\n")
log_file.write(timestamp + " - Script execution finished\n")
๐ค Output: script.log contains 4 lines: start, success, error message, finish
Comparison Table
| Feature | Example 1 | Example 2 | Example 3 | Example 4 | Example 5 |
|---|---|---|---|---|---|
| File mode | write (w) | append (a) | append (a) | write (w) | write (w) |
| Auto-close | No | No | Yes | Yes | Yes |
| Timestamps | Yes | Yes | Yes | Yes | Yes |
| Error handling | No | No | No | No | Yes |
| Number of entries | 1 | 2 | 2 | 3 | 4 |