Takahiro Suzuki | 241c10e | 2020-12-17 20:17:57 +0900 | [diff] [blame] | 1 | // 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 | |
| 15 | package logutil |
| 16 | |
| 17 | import ( |
| 18 | "github.com/coreos/pkg/capnslog" |
| 19 | "google.golang.org/grpc/grpclog" |
| 20 | ) |
| 21 | |
| 22 | // assert that "packageLogger" satisfy "Logger" interface |
| 23 | var _ Logger = &packageLogger{} |
| 24 | |
| 25 | // NewPackageLogger wraps "*capnslog.PackageLogger" that implements "Logger" interface. |
| 26 | // |
| 27 | // For example: |
| 28 | // |
| 29 | // var defaultLogger Logger |
| 30 | // defaultLogger = NewPackageLogger("go.etcd.io/etcd", "snapshot") |
| 31 | // |
| 32 | func NewPackageLogger(repo, pkg string) Logger { |
| 33 | return &packageLogger{p: capnslog.NewPackageLogger(repo, pkg)} |
| 34 | } |
| 35 | |
| 36 | type packageLogger struct { |
| 37 | p *capnslog.PackageLogger |
| 38 | } |
| 39 | |
| 40 | func (l *packageLogger) Info(args ...interface{}) { l.p.Info(args...) } |
| 41 | func (l *packageLogger) Infoln(args ...interface{}) { l.p.Info(args...) } |
| 42 | func (l *packageLogger) Infof(format string, args ...interface{}) { l.p.Infof(format, args...) } |
| 43 | func (l *packageLogger) Warning(args ...interface{}) { l.p.Warning(args...) } |
| 44 | func (l *packageLogger) Warningln(args ...interface{}) { l.p.Warning(args...) } |
| 45 | func (l *packageLogger) Warningf(format string, args ...interface{}) { l.p.Warningf(format, args...) } |
| 46 | func (l *packageLogger) Error(args ...interface{}) { l.p.Error(args...) } |
| 47 | func (l *packageLogger) Errorln(args ...interface{}) { l.p.Error(args...) } |
| 48 | func (l *packageLogger) Errorf(format string, args ...interface{}) { l.p.Errorf(format, args...) } |
| 49 | func (l *packageLogger) Fatal(args ...interface{}) { l.p.Fatal(args...) } |
| 50 | func (l *packageLogger) Fatalln(args ...interface{}) { l.p.Fatal(args...) } |
| 51 | func (l *packageLogger) Fatalf(format string, args ...interface{}) { l.p.Fatalf(format, args...) } |
| 52 | func (l *packageLogger) V(lvl int) bool { |
| 53 | return l.p.LevelAt(capnslog.LogLevel(lvl)) |
| 54 | } |
| 55 | func (l *packageLogger) Lvl(lvl int) grpclog.LoggerV2 { |
| 56 | if l.p.LevelAt(capnslog.LogLevel(lvl)) { |
| 57 | return l |
| 58 | } |
| 59 | return &discardLogger{} |
| 60 | } |