Skip to content

Commit

Permalink
pkg/logger: initial commit
Browse files Browse the repository at this point in the history
Later to be used with "snapshot" and "clientv3" packages.

Signed-off-by: Gyuho Lee <gyuhox@gmail.com>
  • Loading branch information
gyuho committed Jan 22, 2018
1 parent b590d52 commit b3b2864
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
38 changes: 38 additions & 0 deletions pkg/logger/discard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2018 The etcd 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 logger

import "log"

// NewDiscardLogger returns a new Logger that discards everything except "fatal".
func NewDiscardLogger() Logger { return &discardLogger{} }

type discardLogger struct{}

func (l *discardLogger) Info(args ...interface{}) {}
func (l *discardLogger) Infoln(args ...interface{}) {}
func (l *discardLogger) Infof(format string, args ...interface{}) {}
func (l *discardLogger) Warning(args ...interface{}) {}
func (l *discardLogger) Warningln(args ...interface{}) {}
func (l *discardLogger) Warningf(format string, args ...interface{}) {}
func (l *discardLogger) Error(args ...interface{}) {}
func (l *discardLogger) Errorln(args ...interface{}) {}
func (l *discardLogger) Errorf(format string, args ...interface{}) {}
func (l *discardLogger) Fatal(args ...interface{}) { log.Fatal(args...) }
func (l *discardLogger) Fatalln(args ...interface{}) { log.Fatalln(args...) }
func (l *discardLogger) Fatalf(format string, args ...interface{}) { log.Fatalf(format, args...) }
func (l *discardLogger) V(lvl int) bool {
return false
}
16 changes: 16 additions & 0 deletions pkg/logger/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2018 The etcd 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 logger implements various logging utilities.
package logger
22 changes: 22 additions & 0 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2018 The etcd 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 logger

import "google.golang.org/grpc/grpclog"

// Logger defines logging interface.
// TODO: add "Lvl(lvl int)" for clientv3 loggers.
// TODO: make this settable with "Set" method.
type Logger grpclog.LoggerV2
49 changes: 49 additions & 0 deletions pkg/logger/package_logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2018 The etcd 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 logger

import "github.com/coreos/pkg/capnslog"

// NewPackageLogger wraps "*capnslog.PackageLogger" that implements "Logger" interface.
//
// For example:
//
// var defaultLogger Logger
// plog := capnslog.NewPackageLogger("github.com/coreos/etcd", "snapshot")
// defaultLogger = NewPackageLogger(plog)
//
func NewPackageLogger(p *capnslog.PackageLogger) Logger {
return &packageLogger{p: p}
}

type packageLogger struct {
p *capnslog.PackageLogger
}

func (l *packageLogger) Info(args ...interface{}) { l.p.Info(args...) }
func (l *packageLogger) Infoln(args ...interface{}) { l.p.Info(args...) }
func (l *packageLogger) Infof(format string, args ...interface{}) { l.p.Infof(format, args...) }
func (l *packageLogger) Warning(args ...interface{}) { l.p.Warning(args...) }
func (l *packageLogger) Warningln(args ...interface{}) { l.p.Warning(args...) }
func (l *packageLogger) Warningf(format string, args ...interface{}) { l.p.Warningf(format, args...) }
func (l *packageLogger) Error(args ...interface{}) { l.p.Error(args...) }
func (l *packageLogger) Errorln(args ...interface{}) { l.p.Error(args...) }
func (l *packageLogger) Errorf(format string, args ...interface{}) { l.p.Errorf(format, args...) }
func (l *packageLogger) Fatal(args ...interface{}) { l.p.Fatal(args...) }
func (l *packageLogger) Fatalln(args ...interface{}) { l.p.Fatal(args...) }
func (l *packageLogger) Fatalf(format string, args ...interface{}) { l.p.Fatalf(format, args...) }
func (l *packageLogger) V(lvl int) bool {
return l.p.LevelAt(capnslog.LogLevel(lvl))
}

0 comments on commit b3b2864

Please sign in to comment.