devlog

package module
v0.0.0-...-692ff54 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2025 License: MIT Imports: 16 Imported by: 0

README

devlog - ConnectRPC Call Logger

Go Reference License

A feature-rich RPC call logger for ConnectRPC with protocol-agnostic support and structured logging capabilities.

Features

  • Protocol Support: Works with gRPC, Connect, and gRPC-Web protocols
  • Structured Logging: Clear formatting for requests, responses, and errors
  • Protobuf Integration: Built-in support for Protocol Buffer message formatting
  • Header/Metadata Logging: Optional logging of request/response headers and metadata
  • Error Diagnostics: Detailed error information including ConnectRPC error codes
  • Streaming Support: Tracks bidirectional streaming messages with counters
  • Thread-Safe: Safe for concurrent use across goroutines
  • Customizable: Configurable timestamp formats, header visibility, and message formatting

Installation

go get github.com/mdigger/connect-dev-log

Usage

Basic Setup
import (
	"os"
	"connectrpc.com/connect"
	devlog "github.com/mdigger/connect-dev-log"
)

func main() {
	logger := devlog.New(os.Stdout)
	client := connect.NewClient[YourRequest, YourResponse](
		http.DefaultClient,
		"http://localhost:8080",
		connect.WithInterceptors(logger),
	)
	// Use client as normal...
}
With Custom Options
logger := devlog.New(
	os.Stdout,
	devlog.WithTimeFormat(time.DateTime),
	devlog.WithHeaders(true),
)
Sample Output
[2023-11-15T14:23:45.123456Z] /package.Service/Method unary [::1]:54321
  Request:
    {"field":"value"}
  Response:
    {"result":"success"}
  Completed in: 12.345ms

Configuration Options

Option Description Default
WithTimeFormat Sets timestamp format (empty string disables) time.RFC3339Nano
WithHeaders Enables/disables header logging false
WithFormatter Custom protobuf message formatter protojson.MarshalOptions

Streaming Support

The logger provides detailed tracking for streaming RPCs, including:

  • Individual message logging for send/receive
  • Message counters
  • Stream duration metrics
  • Client/Server disconnect detection

Performance Considerations

  • Uses a buffer pool to minimize allocations
  • Thread-safe implementation with mutex protection

Limitations

  • Client-side streaming logging not yet implemented (WrapStreamingClient)
  • No built-in log level filtering (handle at writer level)

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Documentation

Overview

Package devlog provides a RPC call logger for ConnectRPC.

Features: - Protocol-agnostic logging (gRPC, Connect, gRPC-Web) - Structured request/response logging - Protobuf message formatting - Header and metadata support - Error diagnostics - Streaming message tracking - Thread-safe implementation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

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

Logger implements connect.Interceptor for RPC call logging.

func New

func New(w io.Writer, opts ...Option) *Logger

New creates a configured Logger instance. The provided writer must be thread-safe if shared across goroutines.

func (*Logger) WrapStreamingClient

func (l *Logger) WrapStreamingClient(next connect.StreamingClientFunc) connect.StreamingClientFunc

func (*Logger) WrapStreamingHandler

func (l *Logger) WrapStreamingHandler(next connect.StreamingHandlerFunc) connect.StreamingHandlerFunc

func (*Logger) WrapUnary

func (l *Logger) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc

WrapUnary implements connect.UnaryInterceptorFunc.

type Option

type Option func(*Logger)

Option configures Logger behavior.

func WithFormatter

func WithFormatter(opts ProtoFormatter) Option

WithFormatter configures JSON, Text or other proto formatting settings.

func WithHeaderExcludes

func WithHeaderExcludes(exclude []string) Option

WithHeadersExcludes replaces the header value with `[** REDACTED **]`.

func WithHeaders

func WithHeaders(show bool) Option

WithHeaders enables/disables header logging (default: false).

func WithTimeFormat

func WithTimeFormat(format string) Option

WithTimeFormat sets the timestamp format (default: RFC3339Nano).

type ProtoFormatter

type ProtoFormatter interface {
	Format(proto.Message) string
}

ProtoFormatter defines the interface for message formatting.

Jump to

Keyboard shortcuts

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