Skip to main content
FastMCP provides flexible logging options, allowing you to customize how your server logs messages and integrate with existing logging infrastructure.

Custom Logger

Provide a custom logger implementation to control how the server logs messages:

Logger Interface

The Logger interface requires five methods:

Logging in Tools

Tools can log messages to the client using the log object in the context:

Log Methods

The log object provides four methods: All methods accept:
  • message: string - The log message
  • data?: SerializableValue - Optional structured data

Integration Examples

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

1

Use Appropriate Log Levels

  • debug: Detailed diagnostic information
  • info: General informational messages
  • warn: Warning conditions
  • error: Error conditions
2

Include Context in Logs

Always log relevant context data:
3

Log at Key Points

Log at important stages:
  • Start of operations
  • Completion of operations
  • Error conditions
  • State changes
4

Avoid Logging Sensitive Data

Never log:
  • Passwords
  • API keys
  • Personal information (unless necessary and compliant)
  • Credit card numbers
5

Use Structured Logging

Prefer structured data over string concatenation:

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