Skip to content

Commit

Permalink
feat(slog): implement slog.Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Feb 22, 2023
1 parent 6e4f0e1 commit 2a0704b
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 16 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ require (
github.com/go-logfmt/logfmt v0.6.0
github.com/mattn/go-isatty v0.0.17
github.com/stretchr/testify v1.8.1
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb
)
35 changes: 34 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
Expand All @@ -29,9 +30,41 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb h1:PaBZQdo+iSDyHT053FjUCgZQ/9uqVwPOcl7KSWhKn6w=
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
18 changes: 9 additions & 9 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func New(opts ...LoggerOption) Logger {
return l
}

func (l *logger) log(level Level, msg interface{}, keyvals ...interface{}) {
func (l *logger) log(ts time.Time, level Level, skip int, msg interface{}, keyvals ...interface{}) {
if l.isDiscard.Load() {
return
}
Expand All @@ -92,7 +92,7 @@ func (l *logger) log(level Level, msg interface{}, keyvals ...interface{}) {

var kvs []interface{}
if l.timestamp {
kvs = append(kvs, tsKey, l.timeFunc())
kvs = append(kvs, tsKey, ts)
}

if level != noLevel {
Expand All @@ -101,7 +101,7 @@ func (l *logger) log(level Level, msg interface{}, keyvals ...interface{}) {

if l.caller {
// Call stack is log.Error -> log.log (2)
file, line, _ := l.fillLoc(l.callerOffset + 2)
file, line, _ := l.fillLoc(l.callerOffset + skip + 2)
caller := fmt.Sprintf("%s:%d", trimCallerPath(file), line)
kvs = append(kvs, callerKey, caller)
}
Expand Down Expand Up @@ -292,31 +292,31 @@ func (l *logger) With(keyvals ...interface{}) Logger {

// Debug prints a debug message.
func (l *logger) Debug(msg interface{}, keyvals ...interface{}) {
l.log(DebugLevel, msg, keyvals...)
l.log(l.timeFunc(), DebugLevel, 0, msg, keyvals...)
}

// Info prints an info message.
func (l *logger) Info(msg interface{}, keyvals ...interface{}) {
l.log(InfoLevel, msg, keyvals...)
l.log(l.timeFunc(), InfoLevel, 0, msg, keyvals...)
}

// Warn prints a warning message.
func (l *logger) Warn(msg interface{}, keyvals ...interface{}) {
l.log(WarnLevel, msg, keyvals...)
l.log(l.timeFunc(), WarnLevel, 0, msg, keyvals...)
}

// Error prints an error message.
func (l *logger) Error(msg interface{}, keyvals ...interface{}) {
l.log(ErrorLevel, msg, keyvals...)
l.log(l.timeFunc(), ErrorLevel, 0, msg, keyvals...)
}

// Fatal prints a fatal message and exits.
func (l *logger) Fatal(msg interface{}, keyvals ...interface{}) {
l.log(FatalLevel, msg, keyvals...)
l.log(l.timeFunc(), FatalLevel, 0, msg, keyvals...)
os.Exit(1)
}

// Print prints a message with no level.
func (l *logger) Print(msg interface{}, keyvals ...interface{}) {
l.log(noLevel, msg, keyvals...)
l.log(l.timeFunc(), noLevel, 0, msg, keyvals...)
}
4 changes: 4 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"io"
"log"
"time"

"golang.org/x/exp/slog"
)

// DefaultTimeFormat is the default time format.
Expand All @@ -25,6 +27,8 @@ func NowUTC() time.Time {

// Logger is an interface for logging.
type Logger interface {
slog.Handler

// SetLevel sets the allowed level.
SetLevel(level Level)
// GetLevel returns the allowed level.
Expand Down
18 changes: 12 additions & 6 deletions pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,33 +78,39 @@ func Helper() {

// Debug logs a debug message.
func Debug(msg interface{}, keyvals ...interface{}) {
defaultLogger.log(DebugLevel, msg, keyvals...)
ts := defaultLogger.timeFunc()
defaultLogger.log(ts, DebugLevel, 0, msg, keyvals...)
}

// Info logs an info message.
func Info(msg interface{}, keyvals ...interface{}) {
defaultLogger.log(InfoLevel, msg, keyvals...)
ts := defaultLogger.timeFunc()
defaultLogger.log(ts, InfoLevel, 0, msg, keyvals...)
}

// Warn logs a warning message.
func Warn(msg interface{}, keyvals ...interface{}) {
defaultLogger.log(WarnLevel, msg, keyvals...)
ts := defaultLogger.timeFunc()
defaultLogger.log(ts, WarnLevel, 0, msg, keyvals...)
}

// Error logs an error message.
func Error(msg interface{}, keyvals ...interface{}) {
defaultLogger.log(ErrorLevel, msg, keyvals...)
ts := defaultLogger.timeFunc()
defaultLogger.log(ts, ErrorLevel, 0, msg, keyvals...)
}

// Fatal logs a fatal message and exit.
func Fatal(msg interface{}, keyvals ...interface{}) {
defaultLogger.log(FatalLevel, msg, keyvals...)
ts := defaultLogger.timeFunc()
defaultLogger.log(ts, FatalLevel, 0, msg, keyvals...)
os.Exit(1)
}

// Print logs a message with no level.
func Print(msg interface{}, keyvals ...interface{}) {
defaultLogger.log(noLevel, msg, keyvals...)
ts := defaultLogger.timeFunc()
defaultLogger.log(ts, noLevel, 0, msg, keyvals...)
}

// StandardLog returns a standard logger from the default logger.
Expand Down
66 changes: 66 additions & 0 deletions slog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package log

import (
"context"

"golang.org/x/exp/slog"
)

func (l Level) Level() slog.Level {
switch l {
case DebugLevel:
return slog.LevelDebug
case InfoLevel:
return slog.LevelInfo
case WarnLevel:
return slog.LevelWarn
case ErrorLevel:
return slog.LevelError
case FatalLevel:
return 16
default:
return 0
}
}

// Enabled implements slog.Handler
func (l *logger) Enabled(_ context.Context, level slog.Level) bool {
return l.level.Level() <= level
}

// Handle implements slog.Handler
func (l *logger) Handle(r slog.Record) error {
level := l.level
switch r.Level {
case slog.LevelDebug:
level = DebugLevel
case slog.LevelInfo:
level = InfoLevel
case slog.LevelWarn:
level = WarnLevel
case slog.LevelError:
level = ErrorLevel
}
keyvals := make([]interface{}, 0, r.NumAttrs()*2)
r.Attrs(func(a slog.Attr) {
keyvals = append(keyvals, a.Key, a.Value)
})
l.log(r.Time, level, 3, r.Message, keyvals...)
return nil
}

// WithAttrs implements slog.Handler
func (l *logger) WithAttrs(attrs []slog.Attr) slog.Handler {
keyvals := make([]interface{}, 0, len(attrs)*2)
for _, attr := range attrs {
keyvals = append(keyvals, attr.Key, attr.Value)
}
return l.With(keyvals...)
}

// WithGroup implements slog.Handler
func (l *logger) WithGroup(name string) slog.Handler {
h := l.With()
h.SetPrefix(name)
return h
}

0 comments on commit 2a0704b

Please sign in to comment.