-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5155c4f
commit 7e55b91
Showing
8 changed files
with
149 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//go:build go1.21 | ||
// +build go1.21 | ||
|
||
package log | ||
|
||
import "log/slog" | ||
|
||
// fromSlogLevel converts slog.Level to log.Level. | ||
var fromSlogLevel = map[slog.Level]Level{ | ||
slog.LevelDebug: DebugLevel, | ||
slog.LevelInfo: InfoLevel, | ||
slog.LevelWarn: WarnLevel, | ||
slog.LevelError: ErrorLevel, | ||
slog.Level(12): FatalLevel, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//go:build !go1.21 | ||
// +build !go1.21 | ||
|
||
package log | ||
|
||
import "golang.org/x/exp/slog" | ||
|
||
// fromSlogLevel converts slog.Level to log.Level. | ||
var fromSlogLevel = map[slog.Level]Level{ | ||
slog.LevelDebug: DebugLevel, | ||
slog.LevelInfo: InfoLevel, | ||
slog.LevelWarn: WarnLevel, | ||
slog.LevelError: ErrorLevel, | ||
slog.Level(12): FatalLevel, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
//go:build go1.21 | ||
// +build go1.21 | ||
|
||
package log | ||
|
||
import ( | ||
"context" | ||
"runtime" | ||
"sync/atomic" | ||
|
||
"log/slog" | ||
) | ||
|
||
// Enabled reports whether the logger is enabled for the given level. | ||
// | ||
// Implements slog.Handler. | ||
func (l *Logger) Enabled(_ context.Context, level slog.Level) bool { | ||
return atomic.LoadInt32(&l.level) <= int32(fromSlogLevel[level]) | ||
} | ||
|
||
// Handle handles the Record. It will only be called if Enabled returns true. | ||
// | ||
// Implements slog.Handler. | ||
func (l *Logger) Handle(_ context.Context, record slog.Record) error { | ||
fields := make([]interface{}, 0, record.NumAttrs()*2) | ||
record.Attrs(func(a slog.Attr) bool { | ||
fields = append(fields, a.Key, a.Value.String()) | ||
return true | ||
}) | ||
// Get the caller frame using the record's PC. | ||
frames := runtime.CallersFrames([]uintptr{record.PC}) | ||
frame, _ := frames.Next() | ||
l.handle(fromSlogLevel[record.Level], record.Time, []runtime.Frame{frame}, record.Message, fields...) | ||
return nil | ||
} | ||
|
||
// WithAttrs returns a new Handler with the given attributes added. | ||
// | ||
// Implements slog.Handler. | ||
func (l *Logger) WithAttrs(attrs []slog.Attr) slog.Handler { | ||
fields := make([]interface{}, 0, len(attrs)*2) | ||
for _, attr := range attrs { | ||
fields = append(fields, attr.Key, attr.Value) | ||
} | ||
return l.With(fields...) | ||
} | ||
|
||
// WithGroup returns a new Handler with the given group name prepended to the | ||
// current group name or prefix. | ||
// | ||
// Implements slog.Handler. | ||
func (l *Logger) WithGroup(name string) slog.Handler { | ||
if l.prefix != "" { | ||
name = l.prefix + "." + name | ||
} | ||
return l.WithPrefix(name) | ||
} | ||
|
||
var _ slog.Handler = (*Logger)(nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
//go:build !go1.21 | ||
// +build !go1.21 | ||
|
||
package log | ||
|
||
import ( | ||
"context" | ||
"runtime" | ||
"sync/atomic" | ||
|
||
"golang.org/x/exp/slog" | ||
) | ||
|
||
// Enabled reports whether the logger is enabled for the given level. | ||
// | ||
// Implements slog.Handler. | ||
func (l *Logger) Enabled(_ context.Context, level slog.Level) bool { | ||
return atomic.LoadInt32(&l.level) <= int32(fromSlogLevel[level]) | ||
} | ||
|
||
// Handle handles the Record. It will only be called if Enabled returns true. | ||
// | ||
// Implements slog.Handler. | ||
func (l *Logger) Handle(_ context.Context, record slog.Record) error { | ||
fields := make([]interface{}, 0, record.NumAttrs()*2) | ||
record.Attrs(func(a slog.Attr) bool { | ||
fields = append(fields, a.Key, a.Value.String()) | ||
return true | ||
}) | ||
// Get the caller frame using the record's PC. | ||
frames := runtime.CallersFrames([]uintptr{record.PC}) | ||
frame, _ := frames.Next() | ||
l.handle(fromSlogLevel[record.Level], record.Time, []runtime.Frame{frame}, record.Message, fields...) | ||
return nil | ||
} | ||
|
||
// WithAttrs returns a new Handler with the given attributes added. | ||
// | ||
// Implements slog.Handler. | ||
func (l *Logger) WithAttrs(attrs []slog.Attr) slog.Handler { | ||
fields := make([]interface{}, 0, len(attrs)*2) | ||
for _, attr := range attrs { | ||
fields = append(fields, attr.Key, attr.Value) | ||
} | ||
return l.With(fields...) | ||
} | ||
|
||
// WithGroup returns a new Handler with the given group name prepended to the | ||
// current group name or prefix. | ||
// | ||
// Implements slog.Handler. | ||
func (l *Logger) WithGroup(name string) slog.Handler { | ||
if l.prefix != "" { | ||
name = l.prefix + "." + name | ||
} | ||
return l.WithPrefix(name) | ||
} | ||
|
||
var _ slog.Handler = (*Logger)(nil) |