Overview
The Cosmo Router plugin framework provides built-in logging capabilities that integrate seamlessly with your plugin implementation. You can enable structured logging with different output formats and log levels to help with debugging and monitoring your plugins.Logging Configuration
Logging is configured when creating your plugin instance using one of the available logger options:Standard Logger
For structured JSON logging (recommended for all environments):Custom Logger
For advanced use cases where you need full control over the logger configuration:WithCustomLogger function accepts any implementation of the hclog.Logger interface, giving you complete flexibility in your logging setup. This means you can:
- Use
hclog.New()with custom configuration - Implement your own logger that wraps other logging libraries (
logrus,zap, etc.) - Create adapters to existing logging infrastructure
- Build specialized loggers for specific requirements (e.g., filtering, routing, formatting)
Important: When using a custom logger with non-JSON format, you must disable timestamps in your logger configuration. The go-plugin framework parses log lines by checking if they start with the log level (e.g.,
[TRACE], [INFO]). If timestamps are included, the line will start with the timestamp instead of the level, causing the plugin framework to always default to debug level.This restriction does not apply to JSON-formatted logs, as they use structured parsing instead of line prefix detection.Log Levels
The logging system supports standard log levels from thehclog package:
Panic Recovery
The plugin framework provides automatic panic recovery to ensure system stability. When a panic occurs within your plugin:- Automatic Recovery: The panic is caught and gracefully handled without terminating the plugin
- Comprehensive Logging: Full stack traces are captured and logged for debugging
- Structured Error Data: Plugin stack traces are included in a dedicated
plugin_stackfield, separate from the router’s own error context
Plugin Setup
Configure logging when creating your plugin instance by passing the logger option toNewRouterPlugin:
Using the Logger
The logger is automatically injected into the context of each gRPC endpoint. Extract it usinghclog.FromContext():
Structured Logging
Add structured context to your log messages using key-value pairs:Log Output Examples
Since plugin logs are integrated into the router’s zap logger, they will appear in the router’s log output format. Here are examples of how your plugin logs will appear:Best Practices
Log Level Guidelines
- Info: Use for important business events (user actions, significant state changes)
- Debug: Use for detailed execution flow during development
- Warn: Use for recoverable issues or potential problems
- Error: Use for actual errors that affect functionality
- Trace: Use for very detailed debugging (usually disabled in production)
Structured Logging Tips
Environment-Specific Configuration
Consider different log levels for different environments:Integration with Monitoring
When using JSON logging in production, the structured output integrates well with log aggregation systems like:- ELK Stack (Elasticsearch, Logstash, Kibana)
- Fluentd/Fluent Bit
- Grafana Loki
- Cloud logging services (AWS CloudWatch, Google Cloud Logging, etc.)