Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/logger: initial commit #9194

Merged
merged 1 commit into from
Jan 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
51 changes: 51 additions & 0 deletions pkg/logger/package_logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably do an assert that Logger satisfies grpc.Logger

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/cc @gyuho

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something like

var _ MyInterface = MyType{}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just added.

// assert that Logger satisfies grpclog.LoggerV2
var _ Logger = &packageLogger{}
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))
}