server: Use default logging configuration instead of zap production one

This fixes problem where logs json changes format of timestamp.
This commit is contained in:
Marek Siarkowicz
2022-03-31 20:23:45 +02:00
parent 25556a08a8
commit a060b42e47
22 changed files with 135 additions and 70 deletions

View File

@@ -175,6 +175,7 @@ type etcdProcessClusterConfig struct {
v2deprecation string
rollingStart bool
logLevel string
}
// newEtcdProcessCluster launches a new cluster from etcd processes, returning
@@ -315,6 +316,10 @@ func (cfg *etcdProcessClusterConfig) etcdServerProcessConfigs(tb testing.TB) []*
args = append(args, "--v2-deprecation", cfg.v2deprecation)
}
if cfg.logLevel != "" {
args = append(args, "--log-level", cfg.logLevel)
}
etcdCfgs[i] = &etcdServerProcessConfig{
lg: lg,
execPath: cfg.execPath,

View File

@@ -0,0 +1,76 @@
// Copyright 2022 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.
//go:build !cov
// +build !cov
package e2e
import (
"encoding/json"
"testing"
"time"
)
func TestServerJsonLogging(t *testing.T) {
BeforeTest(t)
epc, err := newEtcdProcessCluster(t, &etcdProcessClusterConfig{
clusterSize: 1,
initialToken: "new",
logLevel: "debug",
})
if err != nil {
t.Fatalf("could not start etcd process cluster (%v)", err)
}
logs := epc.procs[0].Logs()
time.Sleep(time.Second)
if err = epc.Close(); err != nil {
t.Fatalf("error closing etcd processes (%v)", err)
}
var entry logEntry
lines := logs.Lines()
if len(lines) == 0 {
t.Errorf("Expected at least one log line")
}
for _, line := range lines {
err := json.Unmarshal([]byte(line), &entry)
if err != nil {
t.Errorf("Failed to parse log line as json, err: %q, line: %s", err, line)
continue
}
if entry.Level == "" {
t.Errorf(`Missing "level" key, line: %s`, line)
}
if entry.Timestamp == "" {
t.Errorf(`Missing "ts" key, line: %s`, line)
}
if _, err := time.Parse("2006-01-02T15:04:05.000Z0700", entry.Timestamp); entry.Timestamp != "" && err != nil {
t.Errorf(`Unexpected "ts" key format, err: %s`, err)
}
if entry.Caller == "" {
t.Errorf(`Missing "caller" key, line: %s`, line)
}
if entry.Message == "" {
t.Errorf(`Missing "message" key, line: %s`, line)
}
}
}
type logEntry struct {
Level string `json:"level"`
Timestamp string `json:"ts"`
Caller string `json:"caller"`
Message string `json:"msg"`
}

View File

@@ -18,6 +18,7 @@ package main
import (
"flag"
"go.etcd.io/etcd/client/pkg/v3/logutil"
"go.etcd.io/etcd/tests/v3/functional/agent"
"go.uber.org/zap"
@@ -27,7 +28,7 @@ var logger *zap.Logger
func init() {
var err error
logger, err = zap.NewProduction()
logger, err = logutil.CreateDefaultZapLogger(zap.InfoLevel)
if err != nil {
panic(err)
}

View File

@@ -28,6 +28,7 @@ import (
"syscall"
"time"
"go.etcd.io/etcd/client/pkg/v3/logutil"
"go.etcd.io/etcd/pkg/v3/proxy"
"go.uber.org/zap"
@@ -76,7 +77,11 @@ $ ./bin/etcdctl --endpoints localhost:23790 put foo bar`)
To: url.URL{Scheme: "tcp", Host: to},
}
if verbose {
cfg.Logger = zap.NewExample()
var err error
cfg.Logger, err = logutil.CreateDefaultZapLogger(zap.InfoLevel)
if err != nil {
panic(err)
}
}
p := proxy.NewServer(cfg)
<-p.Ready()

View File

@@ -19,6 +19,7 @@ import (
"flag"
_ "github.com/etcd-io/gofail/runtime"
"go.etcd.io/etcd/client/pkg/v3/logutil"
"go.etcd.io/etcd/tests/v3/functional/tester"
"go.uber.org/zap"
)
@@ -27,7 +28,7 @@ var logger *zap.Logger
func init() {
var err error
logger, err = zap.NewProduction()
logger, err = logutil.CreateDefaultZapLogger(zap.InfoLevel)
if err != nil {
panic(err)
}

View File

@@ -19,6 +19,7 @@ import (
"sort"
"testing"
"go.etcd.io/etcd/client/pkg/v3/logutil"
"go.etcd.io/etcd/tests/v3/functional/rpcpb"
"go.uber.org/zap"
@@ -256,7 +257,7 @@ func Test_read(t *testing.T) {
},
}
logger, err := zap.NewProduction()
logger, err := logutil.CreateDefaultZapLogger(zap.InfoLevel)
if err != nil {
t.Fatal(err)
}