Debian Codegraph @

funcr/example/main.go

raw
1 /*
2 Copyright 2021 The logr Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 // Package main is an example of using funcr.
18 package main
19
20 import (
21 "fmt"
22
23 "github.com/go-logr/logr"
24 "github.com/go-logr/logr/funcr"
25 )
26
27 type e struct {
28 str string
29 }
30
31 func (e e) Error() string {
32 return e.str
33 }
34
35 func helper(log logr.Logger, msg string) {
36 helper2(log, msg)
37 }
38
39 func helper2(log logr.Logger, msg string) {
40 log.WithCallDepth(2).Info(msg)
41 }
42
43 func main() {
44 // logr
45 log := funcr.NewJSON(
46 func(arg string) { fmt.Println(arg) },
47 funcr.Options{
48 LogCaller: funcr.All,
49 LogTimestamp: true,
50 Verbosity: 1,
51 })
52 logrExample(log.WithName("logr").WithValues("mode", "funcr"))
53
54 // slog (if possible)
55 doSlog(log)
56 }
57
58 func logrExample(log logr.Logger) {
59 log.Info("hello", "val1", 1, "val2", map[string]int{"k": 1})
60 log.V(1).Info("you should see this")
61 log.V(1).V(1).Info("you should NOT see this")
62 log.Error(nil, "uh oh", "trouble", true, "reasons", []float64{0.1, 0.11, 3.14})
63 log.Error(e{"an error occurred"}, "goodbye", "code", -1)
64 helper(log, "thru a helper")
65 }