logging module to provide detailed information about what’s happening in your bot.
Why Use Logging?
- Debug issues faster - See exactly what’s happening internally
- Monitor production - Track errors and performance
- Audit actions - Keep records of bot activities
- Diagnose connection issues - View WebSocket and API events
Quick Start
The simplest way to enable logging:Logging Levels
Python’s logging module has five standard levels:| Level | Value | Usage |
|---|---|---|
DEBUG | 10 | Detailed diagnostic information |
INFO | 20 | General informational messages |
WARNING | 30 | Warning messages for unexpected events |
ERROR | 40 | Error messages for serious issues |
CRITICAL | 50 | Critical errors that may crash the bot |
- INFO Level
- DEBUG Level
- WARNING Level
Show general information and warnings:Output:
Configuring Pycord Loggers
Pycord uses multiple loggers for different components. You can configure them individually:Available Loggers
discord- Main logger (parent of all others)discord.client- Client events and connection statusdiscord.gateway- WebSocket gateway communicationdiscord.http- HTTP API requestsdiscord.state- Cache state managementdiscord.webhook- Webhook operationsdiscord.ext.commands- Command framework
Formatting Log Output
Customize how log messages appear:- Basic Format
- Colored Output
- Minimal Format
Logging to Files
Save logs to a file for later analysis:- Basic File Logging
- Console and File
- Rotating File Handler
- Time-Based Rotation
Custom Application Logging
Add logging to your own bot code:Complete Logging Setup
Here’s a production-ready logging configuration:Debugging Specific Issues
Connection Issues
Connection Issues
Enable detailed gateway logging:This shows:
- WebSocket connection attempts
- HEARTBEAT/HEARTBEAT_ACK exchanges
- IDENTIFY and RESUME operations
- Disconnection reasons
API Rate Limits
API Rate Limits
Monitor HTTP requests and rate limits:This shows:
- All API requests
- Rate limit headers
- 429 responses
- Retry attempts
Event Dispatching
Event Dispatching
See which events are being dispatched:Look for lines like:
Command Issues
Command Issues
Debug command framework:
Best Practices
- Use appropriate levels - Don’t log everything at DEBUG in production
- Rotate log files - Prevent logs from consuming all disk space
- Log errors with stack traces - Use
exc_info=Truefor errors - Include context - Log user IDs, guild names, command names, etc.
- Separate error logs - Keep errors in a separate file for easy review
- Don’t log tokens - Be careful not to log sensitive information
- Use structured logging - Consider JSON logging for easier parsing
- Monitor log size - Large log files can indicate issues
Common Issues
No Logs Appearing
No Logs Appearing
Problem: Logging statements don’t show upSolution: Make sure you’ve called
logging.basicConfig() or set up handlers before creating the bot:Too Much Output
Too Much Output
Problem: Logs are overwhelmingSolution: Reduce logging level or filter specific loggers:
Logs Not Saving
Logs Not Saving
Problem: File handler not writing logsSolution: Ensure the directory exists and permissions are correct:
