-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Not sure if this should be in logr or in a new repo, but I wanted to get feedback. No tests yet, and I am not sure how much testing this warrants.
- Loading branch information
Showing
3 changed files
with
193 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module github.com/go-logr/logr | ||
|
||
go 1.16 | ||
|
||
require golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b |
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,65 @@ | ||
/* | ||
Copyright 2023 The logr Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// Package main is an example of using funcr. | ||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/go-logr/logr/slogr" | ||
"golang.org/x/exp/slog" | ||
) | ||
|
||
type e struct { | ||
str string | ||
} | ||
|
||
func (e e) Error() string { | ||
return e.str | ||
} | ||
|
||
func helper(log logr.Logger, msg string) { | ||
helper2(log, msg) | ||
} | ||
|
||
func helper2(log logr.Logger, msg string) { | ||
log.WithCallDepth(2).Info(msg) | ||
} | ||
|
||
func main() { | ||
opts := slog.HandlerOptions{ | ||
AddSource: true, | ||
Level: slog.Level(-1), | ||
} | ||
handler := slog.NewJSONHandler(os.Stderr, &opts) | ||
log := slogr.New(handler) | ||
example(log) | ||
} | ||
|
||
func example(log logr.Logger) { | ||
log = log.WithName("my") | ||
log = log.WithName("logger") | ||
log = log.WithName("name") | ||
log = log.WithValues("saved", "value") | ||
log.Info("1) hello", "val1", 1, "val2", map[string]int{"k": 1}) | ||
log.V(1).Info("2) you should see this") | ||
log.V(1).V(1).Info("you should NOT see this") | ||
log.Error(nil, "3) uh oh", "trouble", true, "reasons", []float64{0.1, 0.11, 3.14}) | ||
log.Error(e{"an error occurred"}, "4) goodbye", "code", -1) | ||
helper(log, "5) thru a helper") | ||
} |
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,126 @@ | ||
/* | ||
Copyright 2023 The logr Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// Package slogr implements the github.com/go-logr/logr API in terms of Go's | ||
// slog API. | ||
package slogr | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-logr/logr" | ||
"golang.org/x/exp/slog" | ||
) | ||
|
||
// New returns a logr.Logger which is implemented by an arbitrary slog Handler. | ||
func New(handler slog.Handler) logr.Logger { | ||
return logr.New(newSink(slog.New(handler))) | ||
} | ||
|
||
// Underlier exposes access to the underlying logging function. Since | ||
// callers only have a logr.Logger, they have to know which | ||
// implementation is in use, so this interface is less of an | ||
// abstraction and more of a way to test type conversion. | ||
type Underlier interface { | ||
//FIXME: should this return the Logger or Handler? | ||
GetUnderlying() slog.Handler | ||
} | ||
|
||
func newSink(slogger *slog.Logger) logr.LogSink { | ||
sink := &slogsink{ | ||
slogger: slogger, | ||
} | ||
/* // FIXME: | ||
// For skipping slogsink.Info and slogsink.Error. | ||
sink.Formatter.AddCallDepth(1) | ||
*/ | ||
return sink | ||
} | ||
|
||
// slogsink inherits some of its LogSink implementation from Formatter | ||
// and just needs to add some glue code. | ||
type slogsink struct { | ||
slogger *slog.Logger | ||
name string | ||
} | ||
|
||
// Init configures this Formatter from runtime info, such as the call depth | ||
// imposed by logr itself. | ||
// Note that this receiver is a pointer, so depth can be saved. | ||
func (sink *slogsink) Init(info logr.RuntimeInfo) { | ||
//f.depth += info.CallDepth | ||
} | ||
|
||
// Enabled checks whether an info message at the given level should be logged. | ||
func (sink slogsink) Enabled(level int) bool { | ||
return sink.slogger.Enabled(context.TODO(), slog.Level(-level)) | ||
} | ||
|
||
func (sink slogsink) WithName(name string) logr.LogSink { | ||
if len(sink.name) > 0 { | ||
sink.name += "/" | ||
} | ||
sink.name += name | ||
return &sink | ||
} | ||
|
||
func (sink slogsink) WithValues(kvList ...interface{}) logr.LogSink { | ||
newSlogger := sink.slogger.With(kvList...) | ||
return &slogsink{ | ||
slogger: newSlogger, | ||
name: sink.name, | ||
} | ||
} | ||
|
||
/* | ||
// FIXME: this is impossible with slog, as far as I can tell | ||
// https://cs.opensource.google/go/x/exp/+/d63ba01a:slog/logger.go;l=240 | ||
func (sink slogsink) WithCallDepth(depth int) logr.LogSink { | ||
sink.depth += depth | ||
sink.Formatter.AddCallDepth(depth) | ||
return &sink | ||
} | ||
*/ | ||
|
||
func (sink slogsink) Info(level int, msg string, kvList ...interface{}) { | ||
args := make([]interface{}, 0, len(kvList)+4) | ||
if len(sink.name) != 0 { | ||
args = append(args, "logger", sink.name) | ||
} | ||
args = append(args, "vlevel", level) | ||
args = append(args, kvList...) | ||
sink.slogger.Info(msg, args...) | ||
} | ||
|
||
func (sink slogsink) Error(err error, msg string, kvList ...interface{}) { | ||
args := make([]interface{}, 0, len(kvList)+4) | ||
if len(sink.name) != 0 { | ||
args = append(args, "logger", sink.name) | ||
} | ||
args = append(args, "err", err) | ||
args = append(args, kvList...) | ||
sink.slogger.Error(msg, args...) | ||
} | ||
|
||
func (sink slogsink) GetUnderlying() slog.Handler { | ||
return sink.slogger.Handler() | ||
} | ||
|
||
// Assert conformance to the interfaces. | ||
var _ logr.LogSink = &slogsink{} | ||
|
||
// FIXME: var _ logr.CallDepthLogSink = &slogsink{} | ||
var _ Underlier = &slogsink{} |