forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
glog.go
101 lines (82 loc) · 2.15 KB
/
glog.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
package glog
import (
"fmt"
"io"
"strings"
"github.com/golang/glog"
)
// Logger is a simple interface that is roughly equivalent to glog.
type Logger interface {
Is(level int) bool
V(level int) Logger
Infof(format string, args ...interface{})
}
// ToFile creates a logger that will log any items at level or below to file, and defer
// any other output to glog (no matter what the level is.)
func ToFile(w io.Writer, level int) Logger {
return file{w, level}
}
var (
// None implements the Logger interface but does nothing with the log output
None Logger = discard{}
// Log implements the Logger interface for Glog
Log Logger = glogger{}
)
// discard is a Logger that outputs nothing.
type discard struct{}
func (discard) Is(level int) bool { return false }
func (discard) V(level int) Logger { return None }
func (discard) Infof(_ string, _ ...interface{}) {}
// glogger outputs log messages to glog
type glogger struct{}
func (glogger) Is(level int) bool {
return bool(glog.V(glog.Level(level)))
}
func (glogger) V(level int) Logger {
return gverbose{glog.V(glog.Level(level))}
}
func (glogger) Infof(format string, args ...interface{}) {
glog.Infof(format, args...)
}
// gverbose handles glog.V(x) calls
type gverbose struct {
glog.Verbose
}
func (gverbose) Is(level int) bool {
return bool(glog.V(glog.Level(level)))
}
func (gverbose) V(level int) Logger {
if glog.V(glog.Level(level)) {
return Log
}
return None
}
func (g gverbose) Infof(format string, args ...interface{}) {
g.Verbose.Infof(format, args...)
}
// file logs the provided messages at level or below to the writer, or delegates
// to glog.
type file struct {
w io.Writer
level int
}
func (f file) Is(level int) bool {
return level <= f.level || bool(glog.V(glog.Level(level)))
}
func (f file) V(level int) Logger {
// only log things that glog allows
if !glog.V(glog.Level(level)) {
return None
}
// send anything above our level to glog
if level > f.level {
return Log
}
return f
}
func (f file) Infof(format string, args ...interface{}) {
fmt.Fprintf(f.w, format, args...)
if !strings.HasSuffix(format, "\n") {
fmt.Fprintln(f.w)
}
}