go.etcd.io/etcd@v3.3.27+incompatible/pkg/logutil/zap_grpc_test.go (about)

     1  // Copyright 2018 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package logutil
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"io/ioutil"
    21  	"os"
    22  	"path/filepath"
    23  	"strings"
    24  	"testing"
    25  	"time"
    26  
    27  	"go.uber.org/zap"
    28  	"go.uber.org/zap/zapcore"
    29  )
    30  
    31  func TestNewGRPCLoggerV2(t *testing.T) {
    32  	logPath := filepath.Join(os.TempDir(), fmt.Sprintf("test-log-%d", time.Now().UnixNano()))
    33  	defer os.RemoveAll(logPath)
    34  
    35  	lcfg := zap.Config{
    36  		Level:       zap.NewAtomicLevelAt(zap.InfoLevel),
    37  		Development: false,
    38  		Sampling: &zap.SamplingConfig{
    39  			Initial:    100,
    40  			Thereafter: 100,
    41  		},
    42  		Encoding:         "json",
    43  		EncoderConfig:    DefaultZapLoggerConfig.EncoderConfig,
    44  		OutputPaths:      []string{logPath},
    45  		ErrorOutputPaths: []string{logPath},
    46  	}
    47  	gl, err := NewGRPCLoggerV2(lcfg)
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  
    52  	// debug level is not enabled,
    53  	// so info level gRPC-side logging is discarded
    54  	gl.Info("etcd-logutil-1")
    55  	data, err := ioutil.ReadFile(logPath)
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  	if bytes.Contains(data, []byte("etcd-logutil-1")) {
    60  		t.Fatalf("unexpected line %q", string(data))
    61  	}
    62  
    63  	gl.Warning("etcd-logutil-2")
    64  	data, err = ioutil.ReadFile(logPath)
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  	if !bytes.Contains(data, []byte("etcd-logutil-2")) {
    69  		t.Fatalf("can't find data in log %q", string(data))
    70  	}
    71  	if !bytes.Contains(data, []byte("logutil/zap_grpc_test.go:")) {
    72  		t.Fatalf("unexpected caller; %q", string(data))
    73  	}
    74  }
    75  
    76  func TestNewGRPCLoggerV2FromZapCore(t *testing.T) {
    77  	buf := bytes.NewBuffer(nil)
    78  	syncer := zapcore.AddSync(buf)
    79  	cr := zapcore.NewCore(
    80  		zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
    81  		syncer,
    82  		zap.NewAtomicLevelAt(zap.InfoLevel),
    83  	)
    84  
    85  	lg := NewGRPCLoggerV2FromZapCore(cr, syncer)
    86  	lg.Warning("TestNewGRPCLoggerV2FromZapCore")
    87  	txt := buf.String()
    88  	if !strings.Contains(txt, "TestNewGRPCLoggerV2FromZapCore") {
    89  		t.Fatalf("unexpected log %q", txt)
    90  	}
    91  }