Custom Logger
Provide a custom logger implementation to control how the server logs messages:Logger Interface
TheLogger interface requires five methods:
Logging in Tools
Tools can log messages to the client using thelog object in the context:
Log Methods
Thelog object provides four methods:
| Method | Description | Use Case |
|---|---|---|
debug(message, data?) | Debug-level logging | Detailed diagnostic information |
error(message, data?) | Error-level logging | Error conditions |
info(message, data?) | Info-level logging | General informational messages |
warn(message, data?) | Warning-level logging | Warning conditions |
message: string- The log messagedata?: SerializableValue- Optional structured data
Integration Examples
- Winston
- Pino
- File-based
- Simple Custom
Real-World Example
Here’s a complete example from the FastMCP repository showing custom logging:src/examples/custom-logger.ts
Structured Logging
Log structured data alongside messages:Best Practices
Use Appropriate Log Levels
debug: Detailed diagnostic informationinfo: General informational messageswarn: Warning conditionserror: Error conditions
Log at Key Points
Log at important stages:
- Start of operations
- Completion of operations
- Error conditions
- State changes
Avoid Logging Sensitive Data
Never log:
- Passwords
- API keys
- Personal information (unless necessary and compliant)
- Credit card numbers
Production Considerations
Log Rotation
For file-based logging, implement log rotation:Performance Monitoring
Log performance metrics:Next Steps
Streaming
Learn how to stream content and report progress
Authentication
Secure your server with authentication