Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions loggermigration/docs/logging-migration-plan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Logging Migration Plan

## Goals

- Decouple from logrus
- Support pluggable logging backends
- Preserve compatibility
- Keep migration incremental

## Phase 1
- Introduce abstraction layer
- Add noop logger
- Add context propagation
- Add logrus adapter

## Phase 2
- Migrate wrapper packages
- Remove direct concrete coupling

## Phase 3
- Add slog/zap adapters
- Deprecate direct logrus exposure

## Non-goals
- Mass rewrites
- Log format changes
- Breaking public APIs
26 changes: 26 additions & 0 deletions loggermigration/internal/logging/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package logging

import "context"

type loggerKey struct{}

func WithLogger(
ctx context.Context,
l Logger,
) context.Context {
return context.WithValue(
ctx,
loggerKey{},
l,
)
}

func G(ctx context.Context) Logger {
l, ok := ctx.Value(loggerKey{}).(Logger)

if !ok || l == nil {
return Nop
}

return l
}
28 changes: 28 additions & 0 deletions loggermigration/internal/logging/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package logging

import "context"

type Level int

const (
Debug Level = iota
Info
Warn
Error
)

type Attr struct {
Key string
Value any
}

type Logger interface {
Log(
ctx context.Context,
level Level,
msg string,
attrs ...Attr,
)

With(attrs ...Attr) Logger
}
64 changes: 64 additions & 0 deletions loggermigration/internal/logging/logrusadapter/adapter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package logrusadapter

import (
"context"

"github.com/sirupsen/logrus"

"your/module/internal/logging"
)

type Adapter struct {
entry *logrus.Entry
}

func New(
entry *logrus.Entry,
) *Adapter {
return &Adapter{
entry: entry,
}
}

func (a *Adapter) Log(
ctx context.Context,
level logging.Level,
msg string,
attrs ...logging.Attr,
) {
fields := logrus.Fields{}

for _, attr := range attrs {
fields[attr.Key] = attr.Value
}

e := a.entry.WithFields(fields)

switch level {
case logging.Debug:
e.Debug(msg)

case logging.Info:
e.Info(msg)

case logging.Warn:
e.Warn(msg)

case logging.Error:
e.Error(msg)
}
}

func (a *Adapter) With(
attrs ...logging.Attr,
) logging.Logger {
fields := logrus.Fields{}

for _, attr := range attrs {
fields[attr.Key] = attr.Value
}

return &Adapter{
entry: a.entry.WithFields(fields),
}
}
21 changes: 21 additions & 0 deletions loggermigration/internal/logging/noop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package logging

import "context"

type noopLogger struct{}

func (n noopLogger) Log(
ctx context.Context,
level Level,
msg string,
attrs ...Attr,
) {
}

func (n noopLogger) With(
attrs ...Attr,
) Logger {
return n
}

var Nop Logger = noopLogger{}
Loading