Documentation
¶
Overview ¶
Package logger provides structured logging for the MCP Gateway.
This file contains generic helper functions for managing global logger state with proper mutex handling. These helpers encapsulate common patterns for initializing and closing global loggers (FileLogger, JSONLLogger, MarkdownLogger) to reduce code duplication while maintaining thread safety.
Functions in this file follow a consistent pattern:
- init*: Initialize a global logger with proper locking and cleanup of any existing logger - close*: Close and clear a global logger with proper locking
These helpers are used internally by the logger package and should not be called directly by external code. Use the public Init* and Close* functions instead.
Package logger provides structured logging for the MCP Gateway.
This file contains formatting functions for RPC messages in text and markdown formats.
Text Format: Compact, single-line format optimized for grep and command-line tools
Example: "github→tools/list 1234b {...}"
Markdown Format: Human-readable with syntax highlighting, suitable for documentation
Example: "**github**→`tools/list`\n\n```json\n{...}\n```"
Both formats use directional arrows (→ for outbound, ← for inbound) and support special handling for tools/call methods by extracting and displaying tool names.
Package logger provides structured logging for the MCP Gateway.
This file contains helper functions for processing RPC message payloads.
Functions in this file:
- truncateAndSanitize: Combines secret sanitization with length truncation - extractEssentialFields: Extracts key JSON-RPC fields for compact logging - getMapKeys: Utility for extracting map keys without values - isEffectivelyEmpty: Checks if data is effectively empty (e.g., only params: null) - ExtractErrorMessage: Extracts clean error messages from log lines
These helpers are used by the RPC logging system to safely and efficiently process message payloads before logging them.
Package logger provides structured logging for the MCP Gateway.
This file contains RPC message logging coordination, managing the flow of messages across multiple output formats (text, markdown, JSONL).
File Organization:
- rpc_logger.go (this file): Coordination of RPC logging across formats - rpc_formatter.go: Text and markdown formatting functions - rpc_helpers.go: Utility functions for payload processing
The package supports logging RPC messages in three formats:
1. Text logs: Compact single-line format for grep-friendly searching 2. Markdown logs: Human-readable format with syntax highlighting 3. JSONL logs: Machine-readable format for structured analysis
Example:
logger.LogRPCRequest(logger.RPCDirectionOutbound, "github", "tools/list", payload) logger.LogRPCResponse(logger.RPCDirectionInbound, "github", responsePayload, nil)
Index ¶
- Constants
- func CloseGlobalLogger() error
- func CloseJSONLLogger() error
- func CloseMarkdownLogger() error
- func CloseServerFileLogger() error
- func Discard() *slog.Logger
- func ExtractErrorMessage(line string) string
- func InitFileLogger(logDir, fileName string) error
- func InitJSONLLogger(logDir, fileName string) error
- func InitMarkdownLogger(logDir, fileName string) error
- func InitServerFileLogger(logDir string) error
- func LogDebug(category, format string, args ...interface{})
- func LogDebugMd(category, format string, args ...interface{})
- func LogDebugWithServer(serverID, category, format string, args ...interface{})
- func LogError(category, format string, args ...interface{})
- func LogErrorMd(category, format string, args ...interface{})
- func LogErrorWithServer(serverID, category, format string, args ...interface{})
- func LogInfo(category, format string, args ...interface{})
- func LogInfoMd(category, format string, args ...interface{})
- func LogInfoWithServer(serverID, category, format string, args ...interface{})
- func LogRPCMessage(info *RPCMessageInfo)
- func LogRPCMessageJSONL(direction RPCMessageDirection, messageType RPCMessageType, ...)
- func LogRPCRequest(direction RPCMessageDirection, serverID, method string, payload []byte)
- func LogRPCResponse(direction RPCMessageDirection, serverID string, payload []byte, err error)
- func LogWarn(category, format string, args ...interface{})
- func LogWarnMd(category, format string, args ...interface{})
- func LogWarnWithServer(serverID, category, format string, args ...interface{})
- func NewSlogLogger(namespace string) *slog.Logger
- func NewSlogLoggerWithHandler(logger *Logger) *slog.Logger
- type FileLogger
- type JSONLLogger
- type JSONLRPCMessage
- type LogLevel
- type Logger
- type MarkdownLogger
- type RPCMessageDirection
- type RPCMessageInfo
- type RPCMessageType
- type ServerFileLogger
- type SlogHandler
Constants ¶
const ( // EnvDebug is the environment variable for debug logging patterns. // Supports patterns like "*", "namespace:*", "ns1,ns2", "ns:*,-ns:skip" EnvDebug = "DEBUG" // EnvDebugColors controls colored output in debug logs. // Set to "0" to disable colors. EnvDebugColors = "DEBUG_COLORS" )
Environment variable names for logging configuration
const ( // MaxPayloadPreviewLengthText is the maximum number of characters to include in text log preview (10KB) MaxPayloadPreviewLengthText = 10 * 1024 // 10KB // MaxPayloadPreviewLengthMarkdown is the maximum number of characters to include in markdown log preview MaxPayloadPreviewLengthMarkdown = 512 )
Variables ¶
This section is empty.
Functions ¶
func CloseGlobalLogger ¶
func CloseGlobalLogger() error
CloseGlobalLogger closes the global file logger
func CloseJSONLLogger ¶
func CloseJSONLLogger() error
CloseJSONLLogger closes the global JSONL logger
func CloseMarkdownLogger ¶
func CloseMarkdownLogger() error
CloseMarkdownLogger closes the global markdown logger
func CloseServerFileLogger ¶ added in v0.0.102
func CloseServerFileLogger() error
CloseServerFileLogger closes the global server file logger
func ExtractErrorMessage ¶
ExtractErrorMessage extracts a clean error message from a log line. It removes timestamps, log level prefixes, and other common noise. If the message is longer than 200 characters, it will be truncated.
func InitFileLogger ¶
InitFileLogger initializes the global file logger If the log directory doesn't exist and can't be created, falls back to stdout
func InitJSONLLogger ¶
InitJSONLLogger initializes the global JSONL logger
func InitMarkdownLogger ¶
InitMarkdownLogger initializes the global markdown logger
func InitServerFileLogger ¶ added in v0.0.102
InitServerFileLogger initializes the global server file logger
func LogDebug ¶
func LogDebug(category, format string, args ...interface{})
LogDebug logs a debug message
func LogDebugMd ¶
func LogDebugMd(category, format string, args ...interface{})
LogDebugMd logs to both regular and markdown loggers
func LogDebugWithServer ¶ added in v0.0.102
func LogDebugWithServer(serverID, category, format string, args ...interface{})
LogDebugWithServer logs a debug message to the server-specific log file
func LogError ¶
func LogError(category, format string, args ...interface{})
LogError logs an error message
func LogErrorMd ¶
func LogErrorMd(category, format string, args ...interface{})
LogErrorMd logs to both regular and markdown loggers
func LogErrorWithServer ¶ added in v0.0.102
func LogErrorWithServer(serverID, category, format string, args ...interface{})
LogErrorWithServer logs an error message to the server-specific log file
func LogInfo ¶
func LogInfo(category, format string, args ...interface{})
LogInfo logs an informational message
func LogInfoMd ¶
func LogInfoMd(category, format string, args ...interface{})
LogInfoMd logs to both regular and markdown loggers
func LogInfoWithServer ¶ added in v0.0.102
func LogInfoWithServer(serverID, category, format string, args ...interface{})
LogInfoWithServer logs an informational message to the server-specific log file
func LogRPCMessage ¶
func LogRPCMessage(info *RPCMessageInfo)
LogRPCMessage logs a generic RPC message with custom info
func LogRPCMessageJSONL ¶
func LogRPCMessageJSONL(direction RPCMessageDirection, messageType RPCMessageType, serverID, method string, payloadBytes []byte, err error)
LogRPCMessageJSONL logs an RPC message to the global JSONL logger
func LogRPCRequest ¶
func LogRPCRequest(direction RPCMessageDirection, serverID, method string, payload []byte)
LogRPCRequest logs an RPC request message to text, markdown, and JSONL logs
func LogRPCResponse ¶
func LogRPCResponse(direction RPCMessageDirection, serverID string, payload []byte, err error)
LogRPCResponse logs an RPC response message to text, markdown, and JSONL logs
func LogWarn ¶
func LogWarn(category, format string, args ...interface{})
LogWarn logs a warning message
func LogWarnMd ¶
func LogWarnMd(category, format string, args ...interface{})
LogWarnMd logs to both regular and markdown loggers
func LogWarnWithServer ¶ added in v0.0.102
func LogWarnWithServer(serverID, category, format string, args ...interface{})
LogWarnWithServer logs a warning message to the server-specific log file
func NewSlogLogger ¶
NewSlogLogger creates a new slog.Logger that uses gh-aw's logger package This allows integration with libraries that expect slog.Logger
func NewSlogLoggerWithHandler ¶
NewSlogLoggerWithHandler creates a new slog.Logger using an existing Logger instance
Types ¶
type FileLogger ¶
type FileLogger struct {
// contains filtered or unexported fields
}
FileLogger manages logging to a file with fallback to stdout
func (*FileLogger) GetWriter ¶
func (fl *FileLogger) GetWriter() io.Writer
GetWriter returns the underlying io.Writer for the file logger
func (*FileLogger) Log ¶
func (fl *FileLogger) Log(level LogLevel, category, format string, args ...interface{})
Log writes a log message with the specified level and category
type JSONLLogger ¶
type JSONLLogger struct {
// contains filtered or unexported fields
}
JSONLLogger manages logging RPC messages to a JSONL file (one JSON object per line)
func (*JSONLLogger) LogMessage ¶
func (jl *JSONLLogger) LogMessage(entry *JSONLRPCMessage) error
LogMessage logs an RPC message to the JSONL file
type JSONLRPCMessage ¶
type JSONLRPCMessage struct {
Timestamp string `json:"timestamp"`
Direction string `json:"direction"` // "IN" or "OUT"
Type string `json:"type"` // "REQUEST" or "RESPONSE"
ServerID string `json:"server_id"`
Method string `json:"method,omitempty"`
Error string `json:"error,omitempty"`
Payload json.RawMessage `json:"payload"` // Full sanitized payload as raw JSON
}
JSONLRPCMessage represents a single RPC message log entry in JSONL format
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger represents a debug logger for a specific namespace.
func New ¶
New creates a new Logger for the given namespace. The enabled state is computed at construction time based on the DEBUG environment variable. DEBUG syntax follows https://www.npmjs.com/package/debug patterns:
DEBUG=* - enables all loggers DEBUG=namespace:* - enables all loggers in a namespace DEBUG=ns1,ns2 - enables specific namespaces DEBUG=ns:*,-ns:skip - enables namespace but excludes specific patterns
Colors are automatically assigned to each namespace if DEBUG_COLORS != "0" and stderr is a TTY.
type MarkdownLogger ¶
type MarkdownLogger struct {
// contains filtered or unexported fields
}
MarkdownLogger manages logging to a markdown file for GitHub workflow previews
func (*MarkdownLogger) Close ¶
func (ml *MarkdownLogger) Close() error
Close closes the log file and writes the closing details tag
func (*MarkdownLogger) Log ¶
func (ml *MarkdownLogger) Log(level LogLevel, category, format string, args ...interface{})
Log writes a log message in markdown format with emoji bullet points
type RPCMessageDirection ¶
type RPCMessageDirection string
RPCMessageDirection represents whether the message is inbound or outbound
const ( // RPCDirectionInbound represents messages coming into the gateway RPCDirectionInbound RPCMessageDirection = "IN" // RPCDirectionOutbound represents messages going out from the gateway RPCDirectionOutbound RPCMessageDirection = "OUT" )
type RPCMessageInfo ¶
type RPCMessageInfo struct {
Direction RPCMessageDirection // IN or OUT
MessageType RPCMessageType // REQUEST or RESPONSE
ServerID string // Backend server ID or "client" for client messages
Method string // RPC method name (for requests)
PayloadSize int // Size of the payload in bytes
Payload string // First N characters of payload (sanitized)
Error string // Error message if any (for responses)
}
RPCMessageInfo contains information about an RPC message for logging
type RPCMessageType ¶
type RPCMessageType string
RPCMessageType represents the direction of an RPC message
const ( // RPCMessageRequest represents an outbound request or inbound client request RPCMessageRequest RPCMessageType = "REQUEST" // RPCMessageResponse represents an inbound response from backend or outbound response to client RPCMessageResponse RPCMessageType = "RESPONSE" )
type ServerFileLogger ¶ added in v0.0.102
type ServerFileLogger struct {
// contains filtered or unexported fields
}
ServerFileLogger manages per-serverID log files
func (*ServerFileLogger) Close ¶ added in v0.0.102
func (sfl *ServerFileLogger) Close() error
Close closes all server log files
type SlogHandler ¶
type SlogHandler struct {
// contains filtered or unexported fields
}
SlogHandler implements slog.Handler by wrapping a gh-aw Logger This allows integration with libraries that expect slog.Logger
func NewSlogHandler ¶
func NewSlogHandler(logger *Logger) *SlogHandler
NewSlogHandler creates a new slog.Handler that wraps a gh-aw Logger
func (*SlogHandler) Enabled ¶
Enabled reports whether the handler handles records at the given level. We enable all levels when our logger is enabled.
func (*SlogHandler) Handle ¶
Handle handles the Record. It will only be called when Enabled returns true.