-
Notifications
You must be signed in to change notification settings - Fork 219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
logr v1.4.1 + SetSlogLogger #396
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//go:build go1.21 | ||
// +build go1.21 | ||
|
||
/* | ||
Copyright 2021 The Kubernetes 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 klog | ||
|
||
import ( | ||
"log/slog" | ||
|
||
"github.com/go-logr/logr" | ||
) | ||
|
||
// SetSlogLogger reconfigures klog to log through the slog logger. The logger must not be nil. | ||
func SetSlogLogger(logger *slog.Logger) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a corresponding unit test for this function? This can also be used as an example. It is similar to https://github.com/kubernetes/kubernetes/pull/120696/files#diff-e284ecee88b71e68a5cb5630bad5227e3787043dd7ef880d55b5529294e98b47R34-R39 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, thanks for ensuring that I don't take shortcuts... 🥵 I added a |
||
SetLoggerWithOptions(logr.FromSlogHandler(logger.Handler()), ContextualLogger(true)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//go:build go1.21 | ||
// +build go1.21 | ||
|
||
/* | ||
Copyright 2021 The Kubernetes 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 klog_test | ||
|
||
import ( | ||
"log/slog" | ||
"os" | ||
|
||
"k8s.io/klog/v2" | ||
) | ||
|
||
func ExampleSetSlogLogger() { | ||
state := klog.CaptureState() | ||
defer state.Restore() | ||
|
||
handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ | ||
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { | ||
if a.Key == slog.TimeKey { | ||
// Avoid non-deterministic output. | ||
return slog.Attr{} | ||
} | ||
return a | ||
}, | ||
}) | ||
logger := slog.New(handler) | ||
klog.SetSlogLogger(logger) | ||
klog.Info("hello world") | ||
|
||
// Output: | ||
// level=INFO msg="hello world" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module k8s.io/klog/v2 | ||
|
||
go 1.13 | ||
go 1.18 | ||
|
||
require github.com/go-logr/logr v1.3.0 | ||
require github.com/go-logr/logr v1.4.1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY= | ||
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= | ||
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= | ||
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,8 @@ import ( | |
"os" | ||
"time" | ||
|
||
"github.com/go-logr/logr/slogr" | ||
"github.com/go-logr/logr" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This blank line should have been added already earlier. |
||
"k8s.io/klog/v2" | ||
internal "k8s.io/klog/v2/internal/buffer" | ||
) | ||
|
@@ -71,7 +72,7 @@ func ExampleBackground_Slog() { | |
internal.Pid = 123 | ||
|
||
logrLogger := klog.Background() | ||
slogHandler := slogr.NewSlogHandler(logrLogger) | ||
slogHandler := logr.ToSlogHandler(logrLogger) | ||
slogLogger := slog.New(slogHandler) | ||
|
||
// Note that -vmodule does not work when using the slog API because | ||
|
@@ -107,10 +108,10 @@ func ExampleBackground_Slog() { | |
) | ||
|
||
// Output: | ||
// I1224 12:30:40.000000 123 klogr_slog_test.go:80] "A debug message" | ||
// I1224 12:30:40.000000 123 klogr_slog_test.go:82] "An info message" | ||
// W1224 12:30:40.000000 123 klogr_slog_test.go:83] "A warning" | ||
// E1224 12:30:40.000000 123 klogr_slog_test.go:84] "An error" err="fake error" | ||
// I1224 12:30:40.000000 123 klogr_slog_test.go:87] "Grouping" top.sub={"str":"abc","bool":true,"bottom":{"coordinates":{"X":-1,"Y":-2}}} top.duration="1s" top.pi=3.12 top.e=2.71 top.moreCoordinates={"X":100,"Y":200} | ||
// I1224 12:30:40.000000 123 klogr_slog_test.go:103] "slog values" variables={"a":1,"b":2} duration="1s" coordinates={"X":100,"Y":200} | ||
// I1224 12:30:40.000000 123 klogr_slog_test.go:81] "A debug message" | ||
// I1224 12:30:40.000000 123 klogr_slog_test.go:83] "An info message" | ||
// W1224 12:30:40.000000 123 klogr_slog_test.go:84] "A warning" | ||
// E1224 12:30:40.000000 123 klogr_slog_test.go:85] "An error" err="fake error" | ||
// I1224 12:30:40.000000 123 klogr_slog_test.go:88] "Grouping" top.sub={"str":"abc","bool":true,"bottom":{"coordinates":{"X":-1,"Y":-2}}} top.duration="1s" top.pi=3.12 top.e=2.71 top.moreCoordinates={"X":100,"Y":200} | ||
// I1224 12:30:40.000000 123 klogr_slog_test.go:104] "slog values" variables={"a":1,"b":2} duration="1s" coordinates={"X":100,"Y":200} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We were testing with Go 1.2 (float 1.20 = 1.2)...