Skip to content

Commit 2420abc

Browse files
committed
Use slog (stdlib) as default logger, move logrus to subpackage
This change makes the logger package use Go's standard library log/slog as the default implementation instead of logrus. The logrus implementation is moved to a subpackage (logrus/) for users who still need it. Changes: - Root package now uses log/slog (stdlib) - zero external dependencies - logrus implementation moved to logrus/ subpackage - Updated go.mod to require Go 1.25 - Modernized code: use 'any' instead of 'interface{}' - Added package-level documentation - Removed GO111MODULE checks from Makefile Benefits: - Users only depend on logrus if they explicitly import the subpackage - Smaller dependency tree for most users - Modern Go practices with slog BREAKING CHANGE: The default logger is now slog-based. Users relying on logrus-specific behavior should import github.com/gobuffalo/logger/logrus
1 parent 797577e commit 2420abc

11 files changed

Lines changed: 298 additions & 112 deletions

File tree

Makefile

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,29 @@
11
TAGS ?= ""
22
GO_BIN ?= "go"
33

4-
install:
4+
install:
55
$(GO_BIN) install -tags ${TAGS} -v .
66
make tidy
77

88
tidy:
9-
ifeq ($(GO111MODULE),on)
109
$(GO_BIN) mod tidy
11-
else
12-
echo skipping go mod tidy
13-
endif
1410

1511
deps:
1612
$(GO_BIN) get -tags ${TAGS} -t ./...
1713
make tidy
1814

19-
build:
15+
build:
2016
$(GO_BIN) build -v .
2117
make tidy
2218

23-
test:
19+
test:
2420
$(GO_BIN) test -cover -tags ${TAGS} ./...
2521
make tidy
2622

27-
ci-deps:
23+
ci-deps:
2824
$(GO_BIN) get -tags ${TAGS} -t ./...
2925

30-
ci-test:
26+
ci-test:
3127
$(GO_BIN) test -tags ${TAGS} -race ./...
3228

3329
lint:
@@ -36,18 +32,14 @@ lint:
3632
make tidy
3733

3834
update:
39-
ifeq ($(GO111MODULE),on)
4035
rm go.*
4136
$(GO_BIN) mod init
4237
$(GO_BIN) mod tidy
43-
else
44-
$(GO_BIN) get -u -tags ${TAGS}
45-
endif
4638
make test
4739
make install
4840
make tidy
4941

50-
release-test:
42+
release-test:
5143
$(GO_BIN) test -tags ${TAGS} -race ./...
5244
make tidy
5345

@@ -56,6 +48,3 @@ release:
5648
make tidy
5749
release -y -f version.go --skip-packr
5850
make tidy
59-
60-
61-

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
module github.com/gobuffalo/logger
22

3-
go 1.16
3+
go 1.25
44

55
require (
66
github.com/sirupsen/logrus v1.9.1
7-
github.com/stretchr/testify v1.8.0 // indirect
87
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035
98
)
9+
10+
require golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect

go.sum

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,13 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
66
github.com/sirupsen/logrus v1.9.1 h1:Ou41VVR3nMWWmTiEUnj0OlsgOSCUFgsPAOl6jRIcVtQ=
77
github.com/sirupsen/logrus v1.9.1/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
88
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
9-
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
9+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
1010
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
11-
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
12-
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
13-
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
14-
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
15-
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
1611
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
13+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1714
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 h1:Q5284mrmYTpACcm+eAKjKJH48BBwSyfJqmmGDTtT8Vc=
1815
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
1916
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
17+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
2018
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
21-
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
22-
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

level.go

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,44 @@
11
package logger
22

3-
import "github.com/sirupsen/logrus"
4-
5-
// Level of the logger
6-
type Level = logrus.Level
3+
// Level represents the logging level
4+
type Level uint32
75

6+
// These are the different logging levels.
87
const (
98
// PanicLevel level, highest level of severity. Logs and then calls panic with the
109
// message passed to Debug, Info, ...
11-
PanicLevel = logrus.PanicLevel
10+
PanicLevel Level = iota
1211
// FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
1312
// logging level is set to Panic.
14-
FatalLevel = logrus.FatalLevel
13+
FatalLevel
1514
// ErrorLevel level. Logs. Used for errors that should definitely be noted.
1615
// Commonly used for hooks to send errors to an error tracking service.
17-
ErrorLevel = logrus.ErrorLevel
16+
ErrorLevel
1817
// WarnLevel level. Non-critical entries that deserve eyes.
19-
WarnLevel = logrus.WarnLevel
18+
WarnLevel
2019
// InfoLevel level. General operational entries about what's going on inside the
2120
// application.
22-
InfoLevel = logrus.InfoLevel
21+
InfoLevel
2322
// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
24-
DebugLevel = logrus.DebugLevel
23+
DebugLevel
2524
)
25+
26+
// String returns the string representation of the logging level.
27+
func (level Level) String() string {
28+
switch level {
29+
case DebugLevel:
30+
return "debug"
31+
case InfoLevel:
32+
return "info"
33+
case WarnLevel:
34+
return "warning"
35+
case ErrorLevel:
36+
return "error"
37+
case FatalLevel:
38+
return "fatal"
39+
case PanicLevel:
40+
return "panic"
41+
}
42+
43+
return "unknown"
44+
}

logger.go

Lines changed: 73 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,102 @@
1+
// Package logger provides a structured logging interface for Go applications.
2+
// It uses the standard library's log/slog package as the default implementation,
3+
// with support for structured fields and multiple log levels.
4+
//
5+
// The package defines Logger and FieldLogger interfaces that are used throughout
6+
// Buffalo apps and other systems. The default implementation writes to stdout
7+
// in a human-readable but parseable format.
8+
//
9+
// Basic usage:
10+
//
11+
// log := logger.New(logger.InfoLevel)
12+
// log.Info("Server starting")
13+
// log.WithField("port", 8080).Info("Server started")
14+
//
15+
// For users who prefer the logrus backend, import the logrus subpackage:
16+
//
17+
// import "github.com/gobuffalo/logger/logrus"
18+
// log := logrus.New(logrus.InfoLevel)
119
package logger
220

321
import (
4-
"os"
5-
6-
"github.com/sirupsen/logrus"
22+
"fmt"
23+
"log/slog"
724
)
825

9-
// FieldLogger interface
1026
type FieldLogger interface {
1127
Logger
12-
WithField(string, interface{}) FieldLogger
13-
WithFields(map[string]interface{}) FieldLogger
28+
WithField(string, any) FieldLogger
29+
WithFields(map[string]any) FieldLogger
1430
}
1531

16-
// Logger interface is used throughout Buffalo
17-
// apps to log a whole manner of things.
1832
type Logger interface {
19-
Debugf(string, ...interface{})
20-
Infof(string, ...interface{})
21-
Printf(string, ...interface{})
22-
Warnf(string, ...interface{})
23-
Errorf(string, ...interface{})
24-
Fatalf(string, ...interface{})
25-
Debug(...interface{})
26-
Info(...interface{})
27-
Warn(...interface{})
28-
Error(...interface{})
29-
Fatal(...interface{})
30-
Panic(...interface{})
33+
Debugf(string, ...any)
34+
Infof(string, ...any)
35+
Printf(string, ...any)
36+
Warnf(string, ...any)
37+
Errorf(string, ...any)
38+
Fatalf(string, ...any)
39+
Debug(...any)
40+
Info(...any)
41+
Warn(...any)
42+
Error(...any)
43+
Fatal(...any)
44+
Panic(...any)
3145
}
3246

47+
// ParseLevel parses a string level into a Level.
3348
func ParseLevel(level string) (Level, error) {
34-
l, err := logrus.ParseLevel(level)
35-
return Level(l), err
49+
switch level {
50+
case "panic":
51+
return PanicLevel, nil
52+
case "fatal":
53+
return FatalLevel, nil
54+
case "error":
55+
return ErrorLevel, nil
56+
case "warn", "warning":
57+
return WarnLevel, nil
58+
case "info":
59+
return InfoLevel, nil
60+
case "debug":
61+
return DebugLevel, nil
62+
}
63+
64+
var l Level
65+
return l, fmt.Errorf("not a valid Level: %q", level)
3666
}
3767

3868
// NewLogger based on the specified log level, defaults to "debug".
3969
// See `New` for more details.
4070
func NewLogger(level string) FieldLogger {
41-
lvl, err := logrus.ParseLevel(level)
71+
lvl, err := ParseLevel(level)
4272
if err != nil {
43-
lvl = logrus.DebugLevel
73+
lvl = DebugLevel
4474
}
4575
return New(lvl)
4676
}
4777

4878
// New based on the specified log level, defaults to "debug".
4979
// This logger will log to the STDOUT in a human readable,
5080
// but parseable form.
51-
/*
52-
Example: time="2016-12-01T21:02:07-05:00" level=info duration=225.283µs human_size="106 B" method=GET path="/" render=199.79µs request_id=2265736089 size=106 status=200
53-
*/
81+
//
82+
// Example: time="2016-12-01T21:02:07-05:00" level=info duration=225.283µs human_size="106 B" method=GET path="/" render=199.79µs request_id=2265736089 size=106 status=200
5483
func New(lvl Level) FieldLogger {
55-
e := os.Getenv("GO_ENV")
56-
if len(e) == 0 {
57-
e = "development"
58-
}
59-
dev := e == "development"
60-
l := logrus.New()
61-
l.SetOutput(os.Stdout)
62-
l.Level = lvl
63-
l.Formatter = &textFormatter{
64-
ForceColors: dev,
84+
return newSlog(lvl)
85+
}
86+
87+
// toSlogLevel converts our Level to slog.Level.
88+
func toSlogLevel(l Level) slog.Level {
89+
switch l {
90+
case PanicLevel, FatalLevel:
91+
return slog.Level(12) // custom level above error
92+
case ErrorLevel:
93+
return slog.LevelError
94+
case WarnLevel:
95+
return slog.LevelWarn
96+
case InfoLevel:
97+
return slog.LevelInfo
98+
case DebugLevel:
99+
return slog.LevelDebug
65100
}
66-
return Logrus{l}
101+
return slog.LevelDebug
67102
}

logrus.go

Lines changed: 0 additions & 34 deletions
This file was deleted.

formatter.go renamed to logrus/formatter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package logger
1+
package logrus
22

33
// I really don't want to have this, but until (if) https://github.com/sirupsen/logrus/pull/606 is merged we're stuck with all this code. And yes, this is ALL needed just to remove some blank space in the logs
44

0 commit comments

Comments
 (0)