mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
pkg/logutil: add "NewRaftLoggerFromZapCore"
Signed-off-by: Gyuho Lee <gyuhox@gmail.com>
This commit is contained in:
parent
48d5542a76
commit
6df3179c06
@ -1,8 +1,24 @@
|
|||||||
|
// 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 logutil
|
package logutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/coreos/etcd/raft"
|
"github.com/coreos/etcd/raft"
|
||||||
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
"go.uber.org/zap/zapcore"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewRaftLogger converts "*zap.Logger" to "raft.Logger".
|
// NewRaftLogger converts "*zap.Logger" to "raft.Logger".
|
||||||
@ -14,6 +30,14 @@ func NewRaftLogger(lcfg zap.Config) (raft.Logger, error) {
|
|||||||
return &zapRaftLogger{lg: lg, sugar: lg.Sugar()}, nil
|
return &zapRaftLogger{lg: lg, sugar: lg.Sugar()}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewRaftLoggerFromZapCore creates "raft.Logger" from "zap.Core"
|
||||||
|
// and "zapcore.WriteSyncer".
|
||||||
|
func NewRaftLoggerFromZapCore(cr zapcore.Core, syncer zapcore.WriteSyncer) raft.Logger {
|
||||||
|
// "AddCallerSkip" to annotate caller outside of "logutil"
|
||||||
|
lg := zap.New(cr, zap.AddCaller(), zap.AddCallerSkip(1), zap.ErrorOutput(syncer))
|
||||||
|
return &zapRaftLogger{lg: lg, sugar: lg.Sugar()}
|
||||||
|
}
|
||||||
|
|
||||||
type zapRaftLogger struct {
|
type zapRaftLogger struct {
|
||||||
lg *zap.Logger
|
lg *zap.Logger
|
||||||
sugar *zap.SugaredLogger
|
sugar *zap.SugaredLogger
|
||||||
|
@ -20,10 +20,12 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
"go.uber.org/zap/zapcore"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNewRaftLogger(t *testing.T) {
|
func TestNewRaftLogger(t *testing.T) {
|
||||||
@ -68,3 +70,20 @@ func TestNewRaftLogger(t *testing.T) {
|
|||||||
t.Fatalf("unexpected caller; %q", string(data))
|
t.Fatalf("unexpected caller; %q", string(data))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNewRaftLoggerFromZapCore(t *testing.T) {
|
||||||
|
buf := bytes.NewBuffer(nil)
|
||||||
|
syncer := zapcore.AddSync(buf)
|
||||||
|
cr := zapcore.NewCore(
|
||||||
|
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
|
||||||
|
syncer,
|
||||||
|
zap.NewAtomicLevelAt(zap.InfoLevel),
|
||||||
|
)
|
||||||
|
|
||||||
|
lg := NewRaftLoggerFromZapCore(cr, syncer)
|
||||||
|
lg.Info("TestNewRaftLoggerFromZapCore")
|
||||||
|
txt := buf.String()
|
||||||
|
if !strings.Contains(txt, "TestNewRaftLoggerFromZapCore") {
|
||||||
|
t.Fatalf("unexpected log %q", txt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user