Debug Logging
How to log and analyze debug information in LoadForge.
When running load tests, it’s often essential to capture debug information to understand what’s happening during test execution. LoadForge provides a simple way to log debug information that appears in your test results.
Using Print Statements
The simplest way to log debug information in LoadForge is by using Python’s standard print()
function in your Locustfile. These print statements are captured and displayed in the “Errors & Logs” section of your test results.
Basic Logging Example
This example logs the status code of each request to the home page, helping you verify that requests are succeeding.
Logging Request Details
You can log more detailed information about requests and responses:
This example logs the status code and product count from an API response, with additional warnings for empty results or invalid JSON.
Logging Conditional Information
You can use conditional logic to only log information when certain conditions are met:
This example logs initialization information, selectively logs slow requests, and logs all cart operations.
Advanced Logging Techniques
Logging with Context
You can create more structured logs by including context information:
This example creates a custom logging method that includes session context, making it easier to track the flow of individual virtual users.
Viewing Logs in Test Results
After your test run completes, all print statements are collected and displayed in the “Errors & Logs” section of your test results. This provides valuable insights into what happened during the test execution.
Errors & Logs Section
Logs are collected from all workers and aggregated in the final report. This means you can see debug information from all virtual users across all LoadForge workers.
Best Practices for Logging
- Be Selective: Log only important information to avoid overwhelming the logs
- Include Context: Add timestamps, user IDs, or request identifiers to correlate logs
- Format Consistently: Use a consistent format for logs to make them easier to scan
- Log Failures: Always log details when errors or unexpected conditions occur
- Use for Debugging: Remove or reduce logging in production tests to improve performance
Excessive logging can impact test performance. Use logging judiciously, especially in high-volume tests.