-
Notifications
You must be signed in to change notification settings - Fork 7
/
glogr.go
122 lines (98 loc) · 3.13 KB
/
glogr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
Copyright 2019 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 glogr implements github.com/go-logr/logr.Logger in terms of
// github.com/golang/glog.
package glogr
import (
"github.com/go-logr/logr"
"github.com/go-logr/logr/funcr"
"github.com/golang/glog"
)
// New returns a logr.Logger which is implemented by glog.
func New() logr.Logger {
return NewWithOptions(Options{})
}
// NewWithOptions returns a logr.Logger which is implemented by glog.
func NewWithOptions(opts Options) logr.Logger {
if opts.Depth < 0 {
opts.Depth = 0
}
fopts := funcr.Options{
LogCaller: funcr.MessageClass(opts.LogCaller),
}
gl := &glogger{
Formatter: funcr.NewFormatter(fopts),
}
// For skipping glogger.Info and glogger.Error.
gl.Formatter.AddCallDepth(opts.Depth + 1)
return logr.New(gl)
}
// Options carries parameters which influence the way logs are generated.
type Options struct {
// Depth biases the assumed number of call frames to the "true" caller.
// This is useful when the calling code calls a function which then calls
// glogr (e.g. a logging shim to another API). Values less than zero will
// be treated as zero.
Depth int
// LogCaller tells glogr to add a "caller" key to some or all log lines.
// The glog implementation always logs this information in its per-line
// header, whether this option is set or not.
LogCaller MessageClass
// TODO: add an option to log the date/time
}
// MessageClass indicates which category or categories of messages to consider.
type MessageClass int
const (
// None ignores all message classes.
None MessageClass = iota
// All considers all message classes.
All
// Info only considers info messages.
Info
// Error only considers error messages.
Error
)
type glogger struct {
funcr.Formatter
}
var _ logr.LogSink = &glogger{}
var _ logr.CallDepthLogSink = &glogger{}
func (l glogger) Enabled(level int) bool {
return bool(glog.V(glog.Level(level)))
}
func (l glogger) Info(level int, msg string, kvList ...interface{}) {
prefix, args := l.FormatInfo(level, msg, kvList)
if prefix != "" {
args = prefix + ": " + args
}
glog.InfoDepth(l.Formatter.GetDepth(), args)
}
func (l glogger) Error(err error, msg string, kvList ...interface{}) {
prefix, args := l.FormatError(err, msg, kvList)
if prefix != "" {
args = prefix + ": " + args
}
glog.ErrorDepth(l.Formatter.GetDepth(), args)
}
func (l glogger) WithName(name string) logr.LogSink {
l.Formatter.AddName(name)
return &l
}
func (l glogger) WithValues(kvList ...interface{}) logr.LogSink {
l.Formatter.AddValues(kvList)
return &l
}
func (l glogger) WithCallDepth(depth int) logr.LogSink {
l.Formatter.AddCallDepth(depth)
return &l
}