logrus

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: May 1, 2026 License: MIT Imports: 9 Imported by: 0

README

logrus

Go Reference

Bidirectional adapter between Logrus and darvaza.org/slog.

This package provides two-way integration:

  • logrus → slog: Use a logrus logger as the backend for slog.Logger interface
  • slog → logrus: Use logrus hooks to forward logs to any slog implementation

Installation

go get darvaza.org/slog/handlers/logrus

Quick Start

Using logrus as slog backend (logrus → slog)
import (
    "github.com/sirupsen/logrus"
    slogrus "darvaza.org/slog/handlers/logrus"
)

// Configure logrus
logrusLogger := logrus.New()
logrusLogger.SetLevel(logrus.DebugLevel)
logrusLogger.SetFormatter(&logrus.JSONFormatter{})

// Important: Disable ReportCaller to avoid duplicate caller info
logrusLogger.SetReportCaller(false)

// Create slog adapter
slogLogger := slogrus.New(logrusLogger)

// Use with slog interface
slogLogger.Info().
    WithField("user", "alice").
    WithField("action", "login").
    Print("User authenticated")
Using slog as logrus backend (slog → logrus)
import (
    "github.com/sirupsen/logrus"
    slogrus "darvaza.org/slog/handlers/logrus"
    "darvaza.org/slog/handlers/discard"
)

// Create any slog implementation
slogLogger := discard.New() // or any other slog handler

// Create a logrus logger that forwards to slog
logrusLogger := slogrus.NewLogrusLogger(slogLogger)

// Use with logrus interface
logrusLogger.WithFields(logrus.Fields{
    "component": "api",
    "version":   "1.0",
}).Info("Service started")

// Or configure an existing logrus logger to use slog
existingLogger := logrus.StandardLogger()
slogrus.SetupLogrusToSlog(existingLogger, slogLogger)

Features

  • Bidirectional Integration: Convert between logrus and slog in both directions.
  • Hook-based Design: Uses logrus hooks for clean integration.
  • Full Compatibility: Works with existing logrus code and configurations.
  • Preserves Fields: Structured fields are maintained across conversions.
  • Stack Trace Support: Via WithStack() when using logrus → slog.
  • Level Mapping: Automatic conversion between slog and logrus levels.
  • Thread-Safe: Immutable logger instances for concurrent use.

Important Notes

When using logrus → slog
  • Disable SetReportCaller() to avoid duplicate method fields.
  • WithStack() adds both method info and full call stack.
  • Fatal and Panic levels maintain logrus behaviour (exit/panic).
When using slog → logrus
  • The hook disables logrus's native output to prevent double logging.
  • Trace level is mapped to Debug (slog doesn't have Trace).
  • All logrus features (formatters, other hooks) still work normally.

Documentation

Documentation

Overview

Package logrus provides a slog.Logger using github.com/sirupsen/logrus Logger as backend

Index

Constants

View Source
const (
	// CallerFieldName is the field name to be used by WithStack()
	// attempting to mimic the effect of logrus' own SetReportCaller()
	CallerFieldName = "method"

	// StackFieldName is the field name used to store the formatted callstack
	StackFieldName = "call-stack"
)

Variables

This section is empty.

Functions

func New

func New(logger *logrus.Logger) slog.Logger

New creates a slog.Logger adaptor using a logrus as backend

func NewLogrusLogger added in v0.7.2

func NewLogrusLogger(slogLogger slog.Logger) *logrus.Logger

NewLogrusLogger creates a new logrus.Logger that outputs to slog

func SetupLogrusToSlog added in v0.7.2

func SetupLogrusToSlog(logrusLogger *logrus.Logger, slogLogger slog.Logger)

SetupLogrusToSlog configures a logrus logger to send all output to slog

Types

type Logger

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

Logger is an adaptor for using github.com/sirupsen/logrus as slog.Logger

func (*Logger) Debug

func (rl *Logger) Debug() slog.Logger

Debug returns a new logger set to add entries as level Debug

func (*Logger) Enabled

func (rl *Logger) Enabled() bool

Enabled tells if the logger is enabled

func (*Logger) Error

func (rl *Logger) Error() slog.Logger

Error returns a new logger set to add entries as level Error

func (*Logger) Fatal

func (rl *Logger) Fatal() slog.Logger

Fatal returns a new logger set to add entries as level Fatal

func (*Logger) Info

func (rl *Logger) Info() slog.Logger

Info returns a new logger set to add entries as level Info

func (*Logger) Level added in v0.7.3

func (rl *Logger) Level() slog.LogLevel

Level returns the current log level. Exposed for testing only.

func (*Logger) Panic

func (rl *Logger) Panic() slog.Logger

Panic returns a new logger set to add entries as level Panic

func (*Logger) Print

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

Print adds a log entry with arguments handled in the manner of fmt.Print

func (*Logger) Printf

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

Printf adds a log entry with arguments handled in the manner of fmt.Printf

func (*Logger) Println

func (rl *Logger) Println(args ...any)

Println adds a log entry with arguments handled in the manner of fmt.Println

func (*Logger) Warn

func (rl *Logger) Warn() slog.Logger

Warn returns a new logger set to add entries as level Warn

func (*Logger) WithEnabled

func (rl *Logger) WithEnabled() (slog.Logger, bool)

WithEnabled tells if the logger would log or not

func (*Logger) WithField

func (rl *Logger) WithField(label string, value any) slog.Logger

WithField adds a field to the log entry

func (*Logger) WithFields

func (rl *Logger) WithFields(fields map[string]any) slog.Logger

WithFields adds fields to the log entry

func (*Logger) WithLevel

func (rl *Logger) WithLevel(level slog.LogLevel) slog.Logger

WithLevel returns a new logger set to add entries to the specified level

func (*Logger) WithStack

func (rl *Logger) WithStack(skip int) slog.Logger

WithStack attaches a call stack to the log entry

type SlogHook added in v0.7.2

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

SlogHook is a logrus hook that forwards all log entries to a slog.Logger

func NewSlogHook added in v0.7.2

func NewSlogHook(logger slog.Logger) *SlogHook

NewSlogHook creates a logrus Hook that forwards to slog

func (*SlogHook) Fire added in v0.7.2

func (h *SlogHook) Fire(entry *logrus.Entry) (err error)

Fire is called when logging happens

func (*SlogHook) Levels added in v0.7.2

func (*SlogHook) Levels() []logrus.Level

Levels returns all log levels (hook is called for all levels)

Jump to

Keyboard shortcuts

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