| 1 |
//go:build go1.21 |
| 2 |
|
| 3 |
/* |
| 4 |
Copyright 2023 The logr Authors. |
| 5 |
|
| 6 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
you may not use this file except in compliance with the License. |
| 8 |
You may obtain a copy of the License at |
| 9 |
|
| 10 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
|
| 12 |
Unless required by applicable law or agreed to in writing, software |
| 13 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
See the License for the specific language governing permissions and |
| 16 |
limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
// Package main is an example of using slogr. |
| 20 |
package main |
| 21 |
|
| 22 |
import ( |
| 23 |
"context" |
| 24 |
"fmt" |
| 25 |
"log/slog" |
| 26 |
"os" |
| 27 |
|
| 28 |
"github.com/go-logr/logr" |
| 29 |
"github.com/go-logr/logr/funcr" |
| 30 |
) |
| 31 |
|
| 32 |
type e struct { |
| 33 |
str string |
| 34 |
} |
| 35 |
|
| 36 |
func (e e) Error() string { |
| 37 |
return e.str |
| 38 |
} |
| 39 |
|
| 40 |
func logrHelper(log logr.Logger, msg string) { |
| 41 |
logrHelper2(log, msg) |
| 42 |
} |
| 43 |
|
| 44 |
func logrHelper2(log logr.Logger, msg string) { |
| 45 |
log.WithCallDepth(2).Info(msg) |
| 46 |
} |
| 47 |
|
| 48 |
func slogHelper(log *slog.Logger, msg string) { |
| 49 |
slogHelper2(log, msg) |
| 50 |
} |
| 51 |
|
| 52 |
func slogHelper2(log *slog.Logger, msg string) { |
| 53 |
// slog.Logger has no API for skipping helper functions, so this gets logged as call location. |
| 54 |
log.Info(msg) |
| 55 |
} |
| 56 |
|
| 57 |
func main() { |
| 58 |
opts := slog.HandlerOptions{ |
| 59 |
AddSource: true, |
| 60 |
Level: slog.Level(-1), |
| 61 |
} |
| 62 |
handler := slog.NewJSONHandler(os.Stderr, &opts) |
| 63 |
logrLogger := logr.FromSlogHandler(handler) |
| 64 |
logrExample(logrLogger) |
| 65 |
|
| 66 |
logrLogger = funcr.NewJSON( |
| 67 |
func(obj string) { fmt.Println(obj) }, |
| 68 |
funcr.Options{ |
| 69 |
LogCaller: funcr.All, |
| 70 |
LogTimestamp: true, |
| 71 |
Verbosity: 1, |
| 72 |
}) |
| 73 |
slogLogger := slog.New(logr.ToSlogHandler(logrLogger)) |
| 74 |
slogExample(slogLogger) |
| 75 |
} |
| 76 |
|
| 77 |
func logrExample(log logr.Logger) { |
| 78 |
log = log.WithName("my") |
| 79 |
log = log.WithName("logger") |
| 80 |
log = log.WithName("name") |
| 81 |
log = log.WithValues("saved", "value") |
| 82 |
log.Info("1) hello", "val1", 1, "val2", map[string]int{"k": 1}) |
| 83 |
log.V(1).Info("2) you should see this") |
| 84 |
log.V(1).V(1).Info("you should NOT see this") |
| 85 |
log.Error(nil, "3) uh oh", "trouble", true, "reasons", []float64{0.1, 0.11, 3.14}) |
| 86 |
log.Error(e{"an error occurred"}, "4) goodbye", "code", -1) |
| 87 |
logrHelper(log, "5) thru a helper") |
| 88 |
} |
| 89 |
|
| 90 |
func slogExample(log *slog.Logger) { |
| 91 |
// There's no guarantee that this logs the right source code location. |
| 92 |
// It works for Go 1.21.0 by compensating in logr.ToSlogHandler |
| 93 |
// for the additional callers, but those might change. |
| 94 |
log = log.With("saved", "value") |
| 95 |
log.Info("1) hello", "val1", 1, "val2", map[string]int{"k": 1}) |
| 96 |
log.Log(context.TODO(), slog.Level(-1), "2) you should see this") |
| 97 |
log.Log(context.TODO(), slog.Level(-2), "you should NOT see this") |
| 98 |
log.Error("3) uh oh", "trouble", true, "reasons", []float64{0.1, 0.11, 3.14}) |
| 99 |
log.Error("4) goodbye", "code", -1, "err", e{"an error occurred"}) |
| 100 |
slogHelper(log, "5) thru a helper") |
| 101 |
} |