logrus

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2025 License: MIT Imports: 7 Imported by: 0

README

logrus

A temporary logrus proxy to log/slog. Only used for the transition period until our projects are fully migrated. Please keep in mind this is an incomplete implementation, only functions used in osbuild projects are present.

By default, the proxy does not exit on Fatal or panic on Panic calls, this needs to be explicitly enabled via argument in logrus.NewProxyFor function.

How to use

package main

import (
	"context"
	"log/slog"
	"os"

	"github.com/osbuild/logging/pkg/logrus"
)

func main() {
	ctx := context.Background()
	slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug})))
	logrus.SetDefault(logrus.NewProxy())

	logrus.Trace("a", "b", "c")
	logrus.Debug("a", "b", "c")
	logrus.Info("a", "b", "c")
	logrus.Warn("a", "b", "c")
	logrus.Error("a", "b", "c")
	logrus.Panic("a", "b", "c")

	logrus.Tracef("number: %d", 42)
	logrus.Debugf("number: %d", 42)
	logrus.Infof("number: %d", 42)
	logrus.Warnf("number: %d", 42)
	logrus.Errorf("number: %d", 42)
	logrus.Panicf("number: %d", 42)

	// context is supported and carries fields over to log/slog
	logrus.WithContext(ctx).Trace("msg with context")
	logrus.WithContext(ctx).Debug("msg with context")
	logrus.WithContext(ctx).Info("msg with context")
	logrus.WithContext(ctx).Warn("msg with context")
	logrus.WithContext(ctx).Error("msg with context")
	logrus.WithContext(ctx).Panic("msg with context")

	logrus.WithField("key", "value").Trace("msg with field")
	logrus.WithField("key", "value").Debug("msg with field")
	logrus.WithField("key", "value").Info("msg with field")
	logrus.WithField("key", "value").Warn("msg with field")
	logrus.WithField("key", "value").Error("msg with field")
	logrus.WithField("key", "value").Panic("msg with field")
}

Run the above example with:

go run github.com/osbuild/logging/internal/example_logrus/

Documentation

Overview

Implements the most important Logrus package functions as a proxy that forwards records to log/slog. Not meant for long-term use, but for easy migration to slog.

Index

Constants

This section is empty.

Variables

Functions

func Debug

func Debug(args ...any)

func Debugf

func Debugf(format string, args ...any)

func Debugln

func Debugln(args ...any)

func Error

func Error(args ...any)

func Errorf

func Errorf(format string, args ...any)

func Errorln

func Errorln(args ...any)

func Fatal

func Fatal(args ...any)

func Fatalf

func Fatalf(format string, args ...any)

func Fatalln

func Fatalln(args ...any)

func Info

func Info(args ...any)

func Infof

func Infof(format string, args ...any)

func Infoln

func Infoln(args ...any)

func Panic

func Panic(args ...any)

func Panicf

func Panicf(format string, args ...any)

func Panicln

func Panicln(args ...any)

func Print

func Print(args ...any)

func Printf

func Printf(format string, args ...any)

func Println

func Println(args ...any)

func SetDefault

func SetDefault(p *Proxy)

func SetLevel

func SetLevel(level Level)

func Trace

func Trace(args ...any)

func Tracef

func Tracef(format string, args ...any)

func Traceln

func Traceln(args ...any)

func Warn

func Warn(args ...any)

func Warnf

func Warnf(format string, args ...any)

func Warning

func Warning(args ...any)

func Warningf

func Warningf(format string, args ...any)

func Warningln

func Warningln(args ...any)

func Warnln

func Warnln(args ...any)

Types

type FieldLogger

type FieldLogger interface {
	WithField(key string, value interface{}) *Proxy
	WithFields(fields Fields) *Proxy
	WithError(err error) *Proxy

	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Printf(format string, args ...interface{})
	Warnf(format string, args ...interface{})
	Warningf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	Fatalf(format string, args ...interface{})
	Panicf(format string, args ...interface{})

	Debug(args ...interface{})
	Info(args ...interface{})
	Print(args ...interface{})
	Warn(args ...interface{})
	Warning(args ...interface{})
	Error(args ...interface{})
	Fatal(args ...interface{})
	Panic(args ...interface{})

	Debugln(args ...interface{})
	Infoln(args ...interface{})
	Println(args ...interface{})
	Warnln(args ...interface{})
	Warningln(args ...interface{})
	Errorln(args ...interface{})
	Fatalln(args ...interface{})
	Panicln(args ...interface{})

	Tracef(format string, args ...interface{})
	Trace(args ...interface{})
	Traceln(args ...interface{})
}

type Fields

type Fields map[string]interface{}

Fields type, used to pass to `WithFields`.

type Level

type Level uint32

Level type

const (
	// PanicLevel level, highest level of severity. Logs and then calls panic with the
	// message passed to Debug, Info, ...
	PanicLevel Level = iota
	// FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
	// logging level is set to Panic.
	FatalLevel
	// ErrorLevel level. Logs. Used for errors that should definitely be noted.
	// Commonly used for hooks to send errors to an error tracking service.
	ErrorLevel
	// WarnLevel level. Non-critical entries that deserve eyes.
	WarnLevel
	// InfoLevel level. General operational entries about what's going on inside the
	// application.
	InfoLevel
	// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
	DebugLevel
	// TraceLevel level. Designates finer-grained informational events than the Debug.
	TraceLevel
)

func GetLevel

func GetLevel() Level

func ParseLevel

func ParseLevel(lvl string) (Level, error)

func (Level) MarshalText

func (level Level) MarshalText() ([]byte, error)

func (Level) String

func (level Level) String() string

func (*Level) UnmarshalText

func (level *Level) UnmarshalText(text []byte) error

type Logger

type Logger interface {
	WithField(key string, value interface{}) *Proxy
	WithFields(fields Fields) *Proxy
	WithError(err error) *Proxy
	WithContext(ctx context.Context) *Proxy

	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Printf(format string, args ...interface{})
	Warnf(format string, args ...interface{})
	Warningf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	Fatalf(format string, args ...interface{})
	Panicf(format string, args ...interface{})

	Debug(args ...interface{})
	Info(args ...interface{})
	Print(args ...interface{})
	Warn(args ...interface{})
	Warning(args ...interface{})
	Error(args ...interface{})
	Fatal(args ...interface{})
	Panic(args ...interface{})

	Debugln(args ...interface{})
	Infoln(args ...interface{})
	Println(args ...interface{})
	Warnln(args ...interface{})
	Warningln(args ...interface{})
	Errorln(args ...interface{})
	Fatalln(args ...interface{})
	Panicln(args ...interface{})

	Tracef(format string, args ...interface{})
	Trace(args ...interface{})
	Traceln(args ...interface{})
}

Logger is interface in proxy, originally in logrus it is a struct. Can be useful to mock the logging interface but can cause compile errors when pointer is in use.

type Options added in v0.0.5

type Options struct {
	NoExit bool // If true, the process will not exit on fatal/panic log entries.
}

type Proxy

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

Proxy is a proxy type for logrus.Logger

func Default

func Default() *Proxy

func NewDiscardProxy

func NewDiscardProxy() *Proxy

NewDiscardProxy creates a new Proxy which discards all logs. This is the default logger when not set.

func NewEntry

func NewEntry(fl FieldLogger) *Proxy

NewEntry creates a new Proxy for the standard logger. Proxy must be passed as an argument.

func NewProxy

func NewProxy() *Proxy

NewProxy creates a new Proxy for the standard logger. It does not exit on fatal errors. To do that, use NewProxyFor with exitOnFatal set to true.

func NewProxyFor

func NewProxyFor(logger *slog.Logger, options Options) *Proxy

NewProxyFor creates a new Proxy for a particular logger. When options.NoExit is true, the logger will not exit the process on fatal errors and panic on panic calls.

func StandardLogger

func StandardLogger() *Proxy

StandardLogger returns a standard logger proxy.

func WithContext

func WithContext(ctx context.Context) *Proxy

func WithError

func WithError(err error) *Proxy

func WithField

func WithField(key string, value any) *Proxy

func WithFields

func WithFields(fields Fields) *Proxy

func (*Proxy) Debug

func (p *Proxy) Debug(args ...any)

func (*Proxy) Debugf

func (p *Proxy) Debugf(format string, args ...any)

func (*Proxy) Debugln

func (p *Proxy) Debugln(args ...any)

func (*Proxy) Error

func (p *Proxy) Error(args ...any)

func (*Proxy) Errorf

func (p *Proxy) Errorf(format string, args ...any)

func (*Proxy) Errorln

func (p *Proxy) Errorln(args ...any)

func (*Proxy) Fatal

func (p *Proxy) Fatal(args ...any)

func (*Proxy) Fatalf

func (p *Proxy) Fatalf(format string, args ...any)

func (*Proxy) Fatalln

func (p *Proxy) Fatalln(args ...any)

func (*Proxy) GetLevel

func (p *Proxy) GetLevel() Level

func (*Proxy) Info

func (p *Proxy) Info(args ...any)

func (*Proxy) Infof

func (p *Proxy) Infof(format string, args ...any)

func (*Proxy) Infoln

func (p *Proxy) Infoln(args ...any)

func (*Proxy) Panic

func (p *Proxy) Panic(args ...any)

func (*Proxy) Panicf

func (p *Proxy) Panicf(format string, args ...any)

func (*Proxy) Panicln

func (p *Proxy) Panicln(args ...any)

func (*Proxy) Print

func (p *Proxy) Print(args ...any)

func (*Proxy) Printf

func (p *Proxy) Printf(format string, args ...any)

func (*Proxy) Println

func (p *Proxy) Println(args ...any)

func (*Proxy) SetLevel

func (p *Proxy) SetLevel(level Level)

func (*Proxy) Trace

func (p *Proxy) Trace(args ...any)

func (*Proxy) Tracef

func (p *Proxy) Tracef(format string, args ...any)

func (*Proxy) Traceln

func (p *Proxy) Traceln(args ...any)

func (*Proxy) Warn

func (p *Proxy) Warn(args ...any)

func (*Proxy) Warnf

func (p *Proxy) Warnf(format string, args ...any)

func (*Proxy) Warning

func (p *Proxy) Warning(args ...any)

func (*Proxy) Warningf

func (p *Proxy) Warningf(format string, args ...any)

func (*Proxy) Warningln

func (p *Proxy) Warningln(args ...any)

func (*Proxy) Warnln

func (p *Proxy) Warnln(args ...any)

func (*Proxy) WithContext

func (p *Proxy) WithContext(ctx context.Context) *Proxy

func (*Proxy) WithError

func (p *Proxy) WithError(err error) *Proxy

func (*Proxy) WithField

func (p *Proxy) WithField(key string, value any) *Proxy

func (*Proxy) WithFields

func (p *Proxy) WithFields(fields Fields) *Proxy

type StdLogger

type StdLogger interface {
	Print(...interface{})
	Printf(string, ...interface{})
	Println(...interface{})

	Fatal(...interface{})
	Fatalf(string, ...interface{})
	Fatalln(...interface{})

	Panic(...interface{})
	Panicf(string, ...interface{})
	Panicln(...interface{})
}

Jump to

Keyboard shortcuts

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