logger

package
v0.0.114 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 11, 2026 License: MIT Imports: 16 Imported by: 0

README

Logger Package

A simple, debug-style logging framework for Go that follows the pattern matching syntax of the debug npm package.

Features

  • Namespace-based logging: Each logger has a namespace (e.g., workflow:compiler, cli:audit)
  • Pattern matching: Enable/disable loggers using wildcards and exclusions via the DEBUG environment variable
  • Printf interface: Standard printf-style formatting
  • Time diff display: Shows time elapsed since last log call (like debug npm package)
  • Automatic color coding: Each namespace gets a unique color when stderr is a TTY
  • Zero overhead: Logger enabled state is computed once at construction time
  • Thread-safe: Safe for concurrent use
  • Per-ServerID Logs: Separate log files for each backend MCP server for easier troubleshooting

Per-ServerID Logging

The logger package supports creating separate log files for each backend MCP server (identified by serverID). This makes it much easier to troubleshoot specific servers without sifting through unified logs.

How It Works
  • Each serverID gets its own log file: {serverID}.log in the log directory
  • Logs are also written to the main mcp-gateway.log for a unified view
  • Concurrent writes to different serverID logs are handled safely
  • Fallback to unified logging if per-serverID logging cannot be initialized
Usage
import "github.com/github/gh-aw-mcpg/internal/logger"

// Log to both the unified log and the server-specific log
logger.LogInfoWithServer("github", "backend", "Server started successfully")
logger.LogWarnWithServer("slack", "backend", "Connection timeout")
logger.LogErrorWithServer("github", "backend", "Failed to authenticate: %v", err)
logger.LogDebugWithServer("notion", "backend", "Processing request: %v", req)
Log File Structure

When per-serverID logging is enabled, the log directory contains:

/tmp/gh-aw/mcp-logs/
├── mcp-gateway.log    # Unified log with all messages
├── github.log          # Only logs for the "github" server
├── slack.log           # Only logs for the "slack" server
└── notion.log          # Only logs for the "notion" server

Each server-specific log file contains only the messages related to that serverID, making it easier to debug issues with individual backend servers.

Initialization

Per-serverID logging is automatically initialized when the gateway starts:

// In internal/cmd/root.go
logger.InitServerFileLogger(logDir)
defer logger.CloseServerFileLogger()
Benefits
  • Easier Debugging: View all logs for a specific server in isolation
  • Reduced Noise: No need to filter through logs from other servers
  • Better Troubleshooting: Quickly identify which server is having issues
  • Concurrent Access: Safe to log to multiple servers simultaneously
  • Backward Compatible: Falls back gracefully if initialization fails

Usage

Basic Usage
package main

import "github.com/github/gh-aw-mcpg/internal/logger"

var log = logger.New("myapp:feature")

func main() {
    log.Printf("Starting application with config: %s", config)
    log.Print("Multiple", " ", "arguments")
}

Output shows namespace, message, and time diff:

myapp:feature Starting application with config: production +0ns
myapp:feature Multiple arguments +125ms
Avoiding Expensive Operations

Check if a logger is enabled before performing expensive operations:

if log.Enabled() {
    // Do expensive work only if logging is enabled
    result := expensiveOperation()
    log.Printf("Result: %v", result)
}
Time Diff Display

Like the debug npm package, each log shows the time elapsed since the last log call:

log.Printf("Starting task")
// ... do some work ...
log.Printf("Task completed")  // Shows +2.5s (or +500ms, +100µs, etc.)

DEBUG Environment Variable

Control which loggers are enabled using the DEBUG environment variable with patterns:

Examples
# Enable all loggers
DEBUG=*

# Enable all loggers in the 'workflow' namespace
DEBUG=workflow:*

# Enable specific loggers
DEBUG=workflow:compiler,cli:audit

# Enable all except specific loggers
DEBUG=*,-workflow:compiler

# Enable namespace but exclude specific patterns
DEBUG=workflow:*,-workflow:compiler:cache

# Multiple patterns with exclusions
DEBUG=workflow:*,cli:*,-workflow:test

Color Support

Colors are automatically assigned to each namespace when:

  • Stderr is a TTY (terminal)
  • DEBUG_COLORS is not set to 0

Each namespace gets a consistent color based on a hash of its name. This makes it easy to visually distinguish between different loggers.

Disabling Colors
# Disable colors
DEBUG_COLORS=0 DEBUG=* gh aw compile workflow.md

# Colors are automatically disabled when piping output
DEBUG=* gh aw compile workflow.md 2>&1 | tee output.log
Pattern Syntax
  • * - Matches all loggers
  • namespace:* - Matches all loggers with the given prefix
  • *:suffix - Matches all loggers with the given suffix
  • prefix:*:suffix - Matches loggers with both prefix and suffix
  • -pattern - Excludes loggers matching the pattern (takes precedence)
  • pattern1,pattern2 - Multiple patterns separated by commas

Design Decisions

Logger Enabled State

The enabled state is computed once at logger construction time based on the DEBUG environment variable. This means:

  • Zero overhead for disabled loggers (simple boolean check)
  • DEBUG changes after the process starts won't affect existing loggers
Time Diff Tracking

Each logger tracks the time of its last log call to display elapsed time, similar to the debug npm package. This helps identify performance bottlenecks and understand timing relationships between log messages.

Output Destination

Log output goes to two destinations:

  • stderr - Colorized output with time diffs (controlled by DEBUG environment variable)
  • file logger - Text-only output without colors or time diffs (always logged when enabled)

This dual output approach allows:

  • Real-time debugging with colored, timestamped output during development
  • Persistent, parseable log files for production troubleshooting
  • All debug logs are captured to file, making it easier to diagnose issues after the fact
Printf Interface

The logger provides a familiar printf-style interface that Go developers expect:

  • Printf(format, args...) - Formatted output (always adds newline)
  • Print(args...) - Simple concatenation (always adds newline)

Example Patterns

File-based Namespaces
// In pkg/workflow/compiler.go
var log = logger.New("workflow:compiler")

// In pkg/cli/audit.go  
var log = logger.New("cli:audit")

// In pkg/parser/frontmatter.go
var log = logger.New("parser:frontmatter")

Enable with:

DEBUG=workflow:* go run main.go      # Only workflow package
DEBUG=cli:*,parser:* go run main.go  # CLI and parser packages
DEBUG=* go run main.go                # Everything
Feature-based Namespaces
var compileLog = logger.New("compile")
var parseLog = logger.New("parse")
var validateLog = logger.New("validate")

Implementation Notes

  • The DEBUG environment variable is read once when the package is initialized
  • Thread-safe using sync.Mutex for time tracking
  • Simple pattern matching without regex (prefix, suffix, and middle wildcards only)
  • Exclusion patterns (prefixed with -) take precedence over inclusion patterns
  • Time diff formatted like debug npm package (ns, µs, ms, s, m, h)
  • Colors assigned using FNV-1a hash for consistent namespace-to-color mapping
  • Color palette chosen for readability on both light and dark terminals
  • Uses ANSI 256-color codes for better compatibility

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

View Source
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

View Source
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 Discard

func Discard() *slog.Logger

Discard returns a slog.Logger that discards all output

func ExtractErrorMessage

func ExtractErrorMessage(line string) string

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

func InitFileLogger(logDir, fileName string) error

InitFileLogger initializes the global file logger If the log directory doesn't exist and can't be created, falls back to stdout

func InitJSONLLogger

func InitJSONLLogger(logDir, fileName string) error

InitJSONLLogger initializes the global JSONL logger

func InitMarkdownLogger

func InitMarkdownLogger(logDir, fileName string) error

InitMarkdownLogger initializes the global markdown logger

func InitServerFileLogger added in v0.0.102

func InitServerFileLogger(logDir string) error

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

func NewSlogLogger(namespace string) *slog.Logger

NewSlogLogger creates a new slog.Logger that uses gh-aw's logger package This allows integration with libraries that expect slog.Logger

func NewSlogLoggerWithHandler

func NewSlogLoggerWithHandler(logger *Logger) *slog.Logger

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) Close

func (fl *FileLogger) Close() error

Close closes the log file

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) Close

func (jl *JSONLLogger) Close() error

Close closes the JSONL log file

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 LogLevel

type LogLevel string

LogLevel represents the severity of a log message

const (
	LogLevelInfo  LogLevel = "INFO"
	LogLevelWarn  LogLevel = "WARN"
	LogLevelError LogLevel = "ERROR"
	LogLevelDebug LogLevel = "DEBUG"
)

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger represents a debug logger for a specific namespace.

func New

func New(namespace string) *Logger

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.

func (*Logger) Enabled

func (l *Logger) Enabled() bool

Enabled returns whether this logger is enabled

func (*Logger) Print

func (l *Logger) Print(args ...any)

Print prints a message if the logger is enabled. A newline is always added at the end. Time diff since last log is displayed like the debug npm package. Also writes to the file logger in text-only format.

func (*Logger) Printf

func (l *Logger) Printf(format string, args ...any)

Printf prints a formatted message if the logger is enabled. A newline is always added at the end. Time diff since last log is displayed like the debug npm package. Also writes to the file logger in text-only format.

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

func (*ServerFileLogger) Log added in v0.0.102

func (sfl *ServerFileLogger) Log(serverID string, level LogLevel, category, format string, args ...interface{})

Log writes a log message to the server-specific log file

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

func (h *SlogHandler) Enabled(_ context.Context, level slog.Level) bool

Enabled reports whether the handler handles records at the given level. We enable all levels when our logger is enabled.

func (*SlogHandler) Handle

func (h *SlogHandler) Handle(_ context.Context, r slog.Record) error

Handle handles the Record. It will only be called when Enabled returns true.

func (*SlogHandler) WithAttrs

func (h *SlogHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs returns a new Handler whose attributes consist of both the receiver's attributes and the arguments. This implementation does not persist attributes.

func (*SlogHandler) WithGroup

func (h *SlogHandler) WithGroup(name string) slog.Handler

WithGroup returns a new Handler with the given group appended to the receiver's existing groups. This implementation does not persist groups.

Directories

Path Synopsis
Package sanitize provides utilities for redacting sensitive information from logs.
Package sanitize provides utilities for redacting sensitive information from logs.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL