sloghook

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2023 License: MIT Imports: 6 Imported by: 0

README

logrus-slog-formatter

logrus-slog-formatter is log/slog hook for logrus.

SYNOPSIS

package main

import (
	"io"
	"log/slog"
	"os"

	sloghook "github.com/shogo82148/logrus-slog-hook"
	"github.com/sirupsen/logrus"
)

func main() {
	h := slog.NewTextHandler(os.Stderr, nil)
	logrus.AddHook(sloghook.New(h))

	// logrus-slog-hook outputs the logs into STDERR.
	// I recommend that disable the default output.
	logrus.SetFormatter(sloghook.NewFormatter())
	logrus.SetOutput(io.Discard)

	logrus.WithFields(logrus.Fields{
		"name": "joe",
		"age":  42,
	}).Error("Hello world!")

	// Output:
	// time=2023-09-10T23:32:41.229+09:00 level=ERROR msg="Hello world!" age=42 name=joe
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Writer = io.Discard

Functions

func NewLogger

func NewLogger(h slog.Handler) *logrus.Logger

NewLogger returns a new pre-configured [logrus.Logger[.

- Configure Hook. - Output will be ignored all. - enable ReportCaller. - set logrus.TraceLevel. log level filtering is done by log/slog.

Types

type Formatter

type Formatter struct{}

func NewFormatter

func NewFormatter() *Formatter

func (*Formatter) Format

func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error)

type Hook

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

Hook is a logrus.Hook with slog.Handler.

func New

func New(h slog.Handler) *Hook

New returns a new logrus.Hook with slog.Handler.

func (*Hook) Fire

func (h *Hook) Fire(entry *logrus.Entry) error

Fire implements logrus.Hook interface.

func (*Hook) Levels

func (h *Hook) Levels() []logrus.Level

Levels implements logrus.Hook interface.

Jump to

Keyboard shortcuts

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