blob: e7da80eff1b0bb71e06282a46b54bf8cf85fb64c [file] [log] [blame]
Scott Baker2c1c4822019-10-16 11:02:41 -07001// Copyright 2018 The etcd Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package logutil
16
17import "google.golang.org/grpc/grpclog"
18
19// Logger defines logging interface.
20// TODO: deprecate in v3.5.
21type Logger interface {
22 grpclog.LoggerV2
23
24 // Lvl returns logger if logger's verbosity level >= "lvl".
25 // Otherwise, logger that discards everything.
26 Lvl(lvl int) grpclog.LoggerV2
27}
28
29// assert that "defaultLogger" satisfy "Logger" interface
30var _ Logger = &defaultLogger{}
31
32// NewLogger wraps "grpclog.LoggerV2" that implements "Logger" interface.
33//
34// For example:
35//
36// var defaultLogger Logger
37// g := grpclog.NewLoggerV2WithVerbosity(os.Stderr, os.Stderr, os.Stderr, 4)
38// defaultLogger = NewLogger(g)
39//
40func NewLogger(g grpclog.LoggerV2) Logger { return &defaultLogger{g: g} }
41
42type defaultLogger struct {
43 g grpclog.LoggerV2
44}
45
46func (l *defaultLogger) Info(args ...interface{}) { l.g.Info(args...) }
47func (l *defaultLogger) Infoln(args ...interface{}) { l.g.Info(args...) }
48func (l *defaultLogger) Infof(format string, args ...interface{}) { l.g.Infof(format, args...) }
49func (l *defaultLogger) Warning(args ...interface{}) { l.g.Warning(args...) }
50func (l *defaultLogger) Warningln(args ...interface{}) { l.g.Warning(args...) }
51func (l *defaultLogger) Warningf(format string, args ...interface{}) { l.g.Warningf(format, args...) }
52func (l *defaultLogger) Error(args ...interface{}) { l.g.Error(args...) }
53func (l *defaultLogger) Errorln(args ...interface{}) { l.g.Error(args...) }
54func (l *defaultLogger) Errorf(format string, args ...interface{}) { l.g.Errorf(format, args...) }
55func (l *defaultLogger) Fatal(args ...interface{}) { l.g.Fatal(args...) }
56func (l *defaultLogger) Fatalln(args ...interface{}) { l.g.Fatal(args...) }
57func (l *defaultLogger) Fatalf(format string, args ...interface{}) { l.g.Fatalf(format, args...) }
58func (l *defaultLogger) V(lvl int) bool { return l.g.V(lvl) }
59func (l *defaultLogger) Lvl(lvl int) grpclog.LoggerV2 {
60 if l.g.V(lvl) {
61 return l
62 }
63 return &discardLogger{}
64}