Merge pull request #9396 from gyuho/lll

*: replace "pkg/logger" with "pkg/logutil"
This commit is contained in:
Gyuho Lee 2018-03-07 10:57:37 -08:00 committed by GitHub
commit 2a5911f98d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 39 additions and 44 deletions

View File

@ -18,14 +18,14 @@ import (
"io/ioutil" "io/ioutil"
"sync" "sync"
"github.com/coreos/etcd/pkg/logger" "github.com/coreos/etcd/pkg/logutil"
"google.golang.org/grpc/grpclog" "google.golang.org/grpc/grpclog"
) )
var ( var (
lgMu sync.RWMutex lgMu sync.RWMutex
lg logger.Logger lg logutil.Logger
) )
type settableLogger struct { type settableLogger struct {
@ -42,22 +42,22 @@ func init() {
// SetLogger sets client-side Logger. // SetLogger sets client-side Logger.
func SetLogger(l grpclog.LoggerV2) { func SetLogger(l grpclog.LoggerV2) {
lgMu.Lock() lgMu.Lock()
lg = logger.New(l) lg = logutil.NewLogger(l)
// override grpclog so that any changes happen with locking // override grpclog so that any changes happen with locking
grpclog.SetLoggerV2(lg) grpclog.SetLoggerV2(lg)
lgMu.Unlock() lgMu.Unlock()
} }
// GetLogger returns the current logger.Logger. // GetLogger returns the current logutil.Logger.
func GetLogger() logger.Logger { func GetLogger() logutil.Logger {
lgMu.RLock() lgMu.RLock()
l := lg l := lg
lgMu.RUnlock() lgMu.RUnlock()
return l return l
} }
// NewLogger returns a new Logger with logger.Logger. // NewLogger returns a new Logger with logutil.Logger.
func NewLogger(gl grpclog.LoggerV2) logger.Logger { func NewLogger(gl grpclog.LoggerV2) logutil.Logger {
return &settableLogger{l: gl} return &settableLogger{l: gl}
} }
@ -97,5 +97,5 @@ func (s *settableLogger) Lvl(lvl int) grpclog.LoggerV2 {
if l.V(lvl) { if l.V(lvl) {
return s return s
} }
return logger.NewDiscardLogger() return logutil.NewDiscardLogger()
} }

View File

@ -20,7 +20,7 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/coreos/etcd/pkg/logger" "github.com/coreos/etcd/pkg/logutil"
"github.com/coreos/etcd/pkg/types" "github.com/coreos/etcd/pkg/types"
"github.com/coreos/etcd/snapshot" "github.com/coreos/etcd/snapshot"
@ -96,13 +96,13 @@ func snapshotSaveCommandFunc(cmd *cobra.Command, args []string) {
ExitWithError(ExitBadArgs, err) ExitWithError(ExitBadArgs, err)
} }
lg := logger.NewDiscardLogger() lg := logutil.NewDiscardLogger()
debug, err := cmd.Flags().GetBool("debug") debug, err := cmd.Flags().GetBool("debug")
if err != nil { if err != nil {
ExitWithError(ExitError, err) ExitWithError(ExitError, err)
} }
if debug { if debug {
lg = logger.NewPackageLogger("github.com/coreos/etcd", "snapshot") lg = logutil.NewPackageLogger("github.com/coreos/etcd", "snapshot")
} }
sp := snapshot.NewV3(mustClientFromCmd(cmd), lg) sp := snapshot.NewV3(mustClientFromCmd(cmd), lg)
@ -120,13 +120,13 @@ func snapshotStatusCommandFunc(cmd *cobra.Command, args []string) {
} }
initDisplayFromCmd(cmd) initDisplayFromCmd(cmd)
lg := logger.NewDiscardLogger() lg := logutil.NewDiscardLogger()
debug, err := cmd.Flags().GetBool("debug") debug, err := cmd.Flags().GetBool("debug")
if err != nil { if err != nil {
ExitWithError(ExitError, err) ExitWithError(ExitError, err)
} }
if debug { if debug {
lg = logger.NewPackageLogger("github.com/coreos/etcd", "snapshot") lg = logutil.NewPackageLogger("github.com/coreos/etcd", "snapshot")
} }
sp := snapshot.NewV3(nil, lg) sp := snapshot.NewV3(nil, lg)
@ -158,13 +158,13 @@ func snapshotRestoreCommandFunc(cmd *cobra.Command, args []string) {
walDir = filepath.Join(dataDir, "member", "wal") walDir = filepath.Join(dataDir, "member", "wal")
} }
lg := logger.NewDiscardLogger() lg := logutil.NewDiscardLogger()
debug, err := cmd.Flags().GetBool("debug") debug, err := cmd.Flags().GetBool("debug")
if err != nil { if err != nil {
ExitWithError(ExitError, err) ExitWithError(ExitError, err)
} }
if debug { if debug {
lg = logger.NewPackageLogger("github.com/coreos/etcd", "snapshot") lg = logutil.NewPackageLogger("github.com/coreos/etcd", "snapshot")
} }
sp := snapshot.NewV3(nil, lg) sp := snapshot.NewV3(nil, lg)

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package logger package logutil
import ( import (
"log" "log"

View File

@ -12,5 +12,5 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// Package logger implements various logging utilities. // Package logutil includes utilities to facilitate logging.
package logger package logutil

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package logger package logutil
import "google.golang.org/grpc/grpclog" import "google.golang.org/grpc/grpclog"
@ -28,7 +28,7 @@ type Logger interface {
// assert that "defaultLogger" satisfy "Logger" interface // assert that "defaultLogger" satisfy "Logger" interface
var _ Logger = &defaultLogger{} var _ Logger = &defaultLogger{}
// New wraps "grpclog.LoggerV2" that implements "Logger" interface. // NewLogger wraps "grpclog.LoggerV2" that implements "Logger" interface.
// //
// For example: // For example:
// //
@ -36,7 +36,7 @@ var _ Logger = &defaultLogger{}
// g := grpclog.NewLoggerV2WithVerbosity(os.Stderr, os.Stderr, os.Stderr, 4) // g := grpclog.NewLoggerV2WithVerbosity(os.Stderr, os.Stderr, os.Stderr, 4)
// defaultLogger = New(g) // defaultLogger = New(g)
// //
func New(g grpclog.LoggerV2) Logger { return &defaultLogger{g: g} } func NewLogger(g grpclog.LoggerV2) Logger { return &defaultLogger{g: g} }
type defaultLogger struct { type defaultLogger struct {
g grpclog.LoggerV2 g grpclog.LoggerV2

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package logger_test package logutil_test
import ( import (
"bytes" "bytes"
@ -20,7 +20,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/coreos/etcd/pkg/logger" "github.com/coreos/etcd/pkg/logutil"
"google.golang.org/grpc/grpclog" "google.golang.org/grpc/grpclog"
) )
@ -28,7 +28,7 @@ import (
func TestLogger(t *testing.T) { func TestLogger(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
l := logger.New(grpclog.NewLoggerV2WithVerbosity(buf, buf, buf, 10)) l := logutil.NewLogger(grpclog.NewLoggerV2WithVerbosity(buf, buf, buf, 10))
l.Infof("hello world!") l.Infof("hello world!")
if !strings.Contains(buf.String(), "hello world!") { if !strings.Contains(buf.String(), "hello world!") {
t.Fatalf("expected 'hello world!', got %q", buf.String()) t.Fatalf("expected 'hello world!', got %q", buf.String())
@ -45,7 +45,7 @@ func TestLogger(t *testing.T) {
} }
buf.Reset() buf.Reset()
l = logger.New(grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, ioutil.Discard)) l = logutil.NewLogger(grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, ioutil.Discard))
l.Infof("ignore this") l.Infof("ignore this")
if len(buf.Bytes()) > 0 { if len(buf.Bytes()) > 0 {
t.Fatalf("unexpected logs %q", buf.String()) t.Fatalf("unexpected logs %q", buf.String())

View File

@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// Package logutil includes utilities to facilitate logging.
package logutil package logutil
import ( import (

View File

@ -23,16 +23,12 @@ import (
"github.com/coreos/pkg/capnslog" "github.com/coreos/pkg/capnslog"
) )
var (
testLogger = capnslog.NewPackageLogger("github.com/coreos/etcd", "pkg/logutil")
)
func TestMergeLogger(t *testing.T) { func TestMergeLogger(t *testing.T) {
var ( var (
txt = "hello" txt = "hello"
repeatN = 6 repeatN = 6
duration = 2049843762 * time.Nanosecond duration = 2049843762 * time.Nanosecond
mg = NewMergeLogger(testLogger) mg = NewMergeLogger(capnslog.NewPackageLogger("github.com/coreos/etcd", "pkg/logutil"))
) )
// overwrite this for testing // overwrite this for testing
defaultMergePeriod = time.Minute defaultMergePeriod = time.Minute

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package logger package logutil
import ( import (
"github.com/coreos/pkg/capnslog" "github.com/coreos/pkg/capnslog"

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package logger_test package logutil_test
import ( import (
"bytes" "bytes"
@ -20,16 +20,16 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/coreos/pkg/capnslog" "github.com/coreos/etcd/pkg/logutil"
"github.com/coreos/etcd/pkg/logger" "github.com/coreos/pkg/capnslog"
) )
func TestPackageLogger(t *testing.T) { func TestPackageLogger(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
capnslog.SetFormatter(capnslog.NewDefaultFormatter(buf)) capnslog.SetFormatter(capnslog.NewDefaultFormatter(buf))
l := logger.NewPackageLogger("github.com/coreos/etcd", "logger") l := logutil.NewPackageLogger("github.com/coreos/etcd", "logger")
r := capnslog.MustRepoLogger("github.com/coreos/etcd") r := capnslog.MustRepoLogger("github.com/coreos/etcd")
r.SetLogLevel(map[string]capnslog.LogLevel{"logger": capnslog.INFO}) r.SetLogLevel(map[string]capnslog.LogLevel{"logger": capnslog.INFO})

View File

@ -35,7 +35,7 @@ import (
"github.com/coreos/etcd/mvcc" "github.com/coreos/etcd/mvcc"
"github.com/coreos/etcd/mvcc/backend" "github.com/coreos/etcd/mvcc/backend"
"github.com/coreos/etcd/pkg/fileutil" "github.com/coreos/etcd/pkg/fileutil"
"github.com/coreos/etcd/pkg/logger" "github.com/coreos/etcd/pkg/logutil"
"github.com/coreos/etcd/pkg/types" "github.com/coreos/etcd/pkg/types"
"github.com/coreos/etcd/raft" "github.com/coreos/etcd/raft"
"github.com/coreos/etcd/raft/raftpb" "github.com/coreos/etcd/raft/raftpb"
@ -97,9 +97,9 @@ type RestoreConfig struct {
// NewV3 returns a new snapshot Manager for v3.x snapshot. // NewV3 returns a new snapshot Manager for v3.x snapshot.
// "*clientv3.Client" is only used for "Save" method. // "*clientv3.Client" is only used for "Save" method.
// Otherwise, pass "nil". // Otherwise, pass "nil".
func NewV3(cli *clientv3.Client, lg logger.Logger) Manager { func NewV3(cli *clientv3.Client, lg logutil.Logger) Manager {
if lg == nil { if lg == nil {
lg = logger.NewDiscardLogger() lg = logutil.NewDiscardLogger()
} }
return &v3Manager{cli: cli, logger: lg} return &v3Manager{cli: cli, logger: lg}
} }
@ -114,7 +114,7 @@ type v3Manager struct {
cl *membership.RaftCluster cl *membership.RaftCluster
skipHashCheck bool skipHashCheck bool
logger logger.Logger logger logutil.Logger
} }
func (s *v3Manager) Save(ctx context.Context, dbPath string) error { func (s *v3Manager) Save(ctx context.Context, dbPath string) error {

View File

@ -26,7 +26,7 @@ import (
"github.com/coreos/etcd/clientv3" "github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/embed" "github.com/coreos/etcd/embed"
"github.com/coreos/etcd/pkg/logger" "github.com/coreos/etcd/pkg/logutil"
"github.com/coreos/etcd/pkg/testutil" "github.com/coreos/etcd/pkg/testutil"
"github.com/coreos/etcd/pkg/types" "github.com/coreos/etcd/pkg/types"
) )
@ -51,7 +51,7 @@ func TestSnapshotV3RestoreSingle(t *testing.T) {
cfg.InitialCluster = fmt.Sprintf("%s=%s", cfg.Name, pURLs[0].String()) cfg.InitialCluster = fmt.Sprintf("%s=%s", cfg.Name, pURLs[0].String())
cfg.Dir = filepath.Join(os.TempDir(), fmt.Sprint(time.Now().Nanosecond())) cfg.Dir = filepath.Join(os.TempDir(), fmt.Sprint(time.Now().Nanosecond()))
sp := NewV3(nil, logger.NewPackageLogger("github.com/coreos/etcd", "snapshot")) sp := NewV3(nil, logutil.NewPackageLogger("github.com/coreos/etcd", "snapshot"))
err := sp.Restore(dbPath, RestoreConfig{}) err := sp.Restore(dbPath, RestoreConfig{})
if err.Error() != `couldn't find local name "" in the initial cluster configuration` { if err.Error() != `couldn't find local name "" in the initial cluster configuration` {
@ -188,7 +188,7 @@ func createSnapshotFile(t *testing.T, kvs []kv) string {
} }
} }
sp := NewV3(cli, logger.NewPackageLogger("github.com/coreos/etcd", "snapshot")) sp := NewV3(cli, logutil.NewPackageLogger("github.com/coreos/etcd", "snapshot"))
dpPath := filepath.Join(os.TempDir(), fmt.Sprintf("snapshot%d.db", time.Now().Nanosecond())) dpPath := filepath.Join(os.TempDir(), fmt.Sprintf("snapshot%d.db", time.Now().Nanosecond()))
if err = sp.Save(context.Background(), dpPath); err != nil { if err = sp.Save(context.Background(), dpPath); err != nil {
t.Fatal(err) t.Fatal(err)
@ -229,7 +229,7 @@ func restoreCluster(t *testing.T, clusterN int, dbPath string) (
cfg.InitialCluster = ics cfg.InitialCluster = ics
cfg.Dir = filepath.Join(os.TempDir(), fmt.Sprint(time.Now().Nanosecond()+i)) cfg.Dir = filepath.Join(os.TempDir(), fmt.Sprint(time.Now().Nanosecond()+i))
sp := NewV3(nil, logger.NewPackageLogger("github.com/coreos/etcd", "snapshot")) sp := NewV3(nil, logutil.NewPackageLogger("github.com/coreos/etcd", "snapshot"))
if err := sp.Restore(dbPath, RestoreConfig{ if err := sp.Restore(dbPath, RestoreConfig{
Name: cfg.Name, Name: cfg.Name,
OutputDataDir: cfg.Dir, OutputDataDir: cfg.Dir,