Container Logs¶
Testcontainers-Python provides several ways to access and follow container logs. This is essential for debugging and monitoring container behavior during tests.
Basic Log Access¶
The simplest way to access logs is using the get_logs
method:
from testcontainers.generic import GenericContainer
with GenericContainer("nginx:alpine") as container:
# Get all logs
stdout, stderr = container.get_logs()
print(f"STDOUT: {stdout}")
print(f"STDERR: {stderr}")
Following Logs¶
To follow logs in real-time:
with GenericContainer("nginx:alpine") as container:
# Follow logs
for line in container.follow_logs():
print(line) # Each line as it appears
Log Access with Options¶
You can customize log access with various options:
with GenericContainer("nginx:alpine") as container:
# Get logs with timestamps
stdout, stderr = container.get_logs(timestamps=True)
# Get logs since a specific time
import datetime
since = datetime.datetime.now() - datetime.timedelta(minutes=5)
stdout, stderr = container.get_logs(since=since)
# Get logs with tail
stdout, stderr = container.get_logs(tail=100) # Last 100 lines
Log Streams¶
You can access specific log streams:
with GenericContainer("nginx:alpine") as container:
# Get only stdout
stdout, _ = container.get_logs()
# Get only stderr
_, stderr = container.get_logs()
# Get both streams
stdout, stderr = container.get_logs()
Log Following with Callback¶
You can use a callback function to process logs:
def log_callback(line):
print(f"Log line: {line}")
with GenericContainer("nginx:alpine") as container:
# Follow logs with callback
container.follow_logs(callback=log_callback)
Log Access in Tests¶
Here's how to use logs in tests:
import pytest
from testcontainers.generic import GenericContainer
def test_container_logs():
with GenericContainer("nginx:alpine") as container:
# Wait for specific log message
for line in container.follow_logs():
if "Configuration complete" in line:
break
# Verify log content
stdout, stderr = container.get_logs()
assert "Configuration complete" in stdout
Best Practices¶
- Use appropriate log levels
- Handle log streams separately
- Use timestamps for debugging
- Consider log rotation
- Use log following for real-time monitoring
- Clean up log resources
- Use appropriate log formats
- Consider log volume
Common Use Cases¶
Application Startup Verification¶
with GenericContainer("myapp:latest") as container:
# Wait for application to start
for line in container.follow_logs():
if "Application started" in line:
break
Error Detection¶
with GenericContainer("myapp:latest") as container:
# Monitor for errors
for line in container.follow_logs():
if "ERROR" in line:
print(f"Error detected: {line}")
Performance Monitoring¶
with GenericContainer("myapp:latest") as container:
# Monitor performance metrics
for line in container.follow_logs():
if "Performance" in line:
print(f"Performance metric: {line}")
Troubleshooting¶
If you encounter issues with log access:
- Check container state
- Verify log configuration
- Check for log rotation
- Verify log permissions
- Check for log volume
- Verify log format
- Check for log buffering
- Verify log drivers