Skip to content

Commit cbcc5e1

Browse files
authored
Merge pull request #23 from thockin/master
Bump to logr v1.2.0
2 parents 35c7cdc + d462415 commit cbcc5e1

File tree

4 files changed

+74
-6
lines changed

4 files changed

+74
-6
lines changed

example_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 glogr_test
18+
19+
import (
20+
"errors"
21+
"flag"
22+
"os"
23+
"sync"
24+
25+
"github.com/go-logr/glogr"
26+
)
27+
28+
var glogInit = sync.Once{}
29+
30+
func initGlog() {
31+
glogInit.Do(func() {
32+
_ = flag.Set("v", "1")
33+
_ = flag.Set("logtostderr", "true")
34+
flag.Parse()
35+
})
36+
os.Stderr = os.Stdout
37+
}
38+
39+
var errSome = errors.New("some error")
40+
41+
func ExampleNew() {
42+
initGlog()
43+
log := glogr.New()
44+
log.Info("info message with default options")
45+
log.Error(errSome, "error message with default options")
46+
log.Info("invalid key", 42, "answer")
47+
log.Info("missing value", "answer")
48+
// I1015 08:59:26.952954 2385059 example_test.go:44] "level"=0 "msg"="info message with default options"
49+
// E1015 08:59:26.953000 2385059 example_test.go:45] "msg"="error message with default options" "error"="some error"
50+
// I1015 08:59:26.953005 2385059 example_test.go:46] "level"=0 "msg"="invalid key" "<non-string-key: 42>"="answer"
51+
// I1015 08:59:26.953013 2385059 example_test.go:47] "level"=0 "msg"="missing value" "answer"="<no-value>"
52+
}
53+
54+
func ExampleNew_withName() {
55+
initGlog()
56+
log := glogr.New()
57+
log.WithName("hello").WithName("world").Info("thanks for the fish")
58+
// I1015 08:59:26.953089 2385059 example_test.go:54] hello/world: "level"=0 "msg"="thanks for the fish"
59+
}
60+
61+
func ExampleNewWithOptions() {
62+
initGlog()
63+
log := glogr.NewWithOptions(glogr.Options{LogCaller: glogr.Error})
64+
log.Info("Info log with LogCaller=Error")
65+
log.Error(nil, "Error log with LogCaller=All")
66+
// I1015 09:10:36.754010 2392854 example_test.go:64] "level"=0 "msg"="Info log with LogCaller=Error"
67+
// E1015 09:10:36.754057 2392854 example_test.go:65] "caller"={"file":"example_test.go","line":65} "msg"="Error log with LogCaller=All" "error"=null
68+
}

glogr.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func NewWithOptions(opts Options) logr.Logger {
4444
}
4545

4646
// For skipping glogger.Info and glogger.Error.
47-
gl.Formatter.AddCallDepth(opts.Depth)
47+
gl.Formatter.AddCallDepth(opts.Depth + 1)
4848

4949
return logr.New(gl)
5050
}
@@ -95,15 +95,15 @@ func (l glogger) Info(level int, msg string, kvList ...interface{}) {
9595
if prefix != "" {
9696
args = prefix + ": " + args
9797
}
98-
glog.InfoDepth(l.Formatter.GetDepth()+1, args)
98+
glog.InfoDepth(l.Formatter.GetDepth(), args)
9999
}
100100

101101
func (l glogger) Error(err error, msg string, kvList ...interface{}) {
102102
prefix, args := l.FormatError(err, msg, kvList)
103103
if prefix != "" {
104104
args = prefix + ": " + args
105105
}
106-
glog.ErrorDepth(l.Formatter.GetDepth()+1, args)
106+
glog.ErrorDepth(l.Formatter.GetDepth(), args)
107107
}
108108

109109
func (l glogger) WithName(name string) logr.LogSink {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ module github.com/go-logr/glogr
33
go 1.16
44

55
require (
6-
github.com/go-logr/logr v1.1.0
6+
github.com/go-logr/logr v1.2.0
77
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
88
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
github.com/go-logr/logr v1.1.0 h1:nAbevmWlS2Ic4m4+/An5NXkaGqlqpbBgdcuThZxnZyI=
2-
github.com/go-logr/logr v1.1.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
1+
github.com/go-logr/logr v1.2.0 h1:QK40JKJyMdUDz+h+xvCsru/bJhvG0UxvePV0ufL/AcE=
2+
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
33
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
44
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=

0 commit comments

Comments
 (0)