github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/sink/pulsar/logger_test.go (about)

     1  // Copyright 2024 PingCAP, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package pulsar
    15  
    16  import (
    17  	"errors"
    18  	"runtime"
    19  	"testing"
    20  
    21  	"github.com/apache/pulsar-client-go/pulsar/log"
    22  	"github.com/stretchr/testify/assert"
    23  	"go.uber.org/zap"
    24  	"go.uber.org/zap/zapcore"
    25  	"go.uber.org/zap/zaptest/observer"
    26  )
    27  
    28  // TestPulsarLog ensures Pulsar's use of our logger behaves as expected of a typical zap logger:
    29  //  1. all fields injected via .With*() are present in the final log
    30  //  2. the [file:line] points to the actual call site rather than `logger.go`.
    31  func TestPulsarLog(t *testing.T) {
    32  	core, observedLogs := observer.New(zapcore.DebugLevel)
    33  	logger := NewPulsarLogger(zap.New(core).WithOptions(zap.AddCaller()))
    34  
    35  	pc, file, line, ok := runtime.Caller(0)
    36  	assert.True(t, ok)
    37  	functionName := runtime.FuncForPC(pc).Name()
    38  
    39  	logger.Infof("1 + 2 = %d", 1+2)
    40  	logger.WithError(errors.ErrUnsupported).Warn("3", "+", "4", "=", 3+4)
    41  	logger.WithFields(log.Fields{"x": 1234, "y": 9.75}).Info("connected")
    42  
    43  	allEntries := observedLogs.AllUntimed()
    44  	// we can't reliably test the `pc` address of the caller information. remove them.
    45  	for i := range allEntries {
    46  		allEntries[i].Caller.PC = 0
    47  	}
    48  
    49  	assert.Len(t, allEntries, 3)
    50  	assert.Equal(t, allEntries[0:2], []observer.LoggedEntry{
    51  		{
    52  			Entry: zapcore.Entry{
    53  				Level:   zapcore.InfoLevel,
    54  				Message: "1 + 2 = 3",
    55  				Caller: zapcore.EntryCaller{
    56  					Defined:  true,
    57  					File:     file,
    58  					Line:     line + 4,
    59  					Function: functionName,
    60  				},
    61  			},
    62  			Context: []zap.Field{},
    63  		},
    64  		{
    65  			Entry: zapcore.Entry{
    66  				Level:   zapcore.WarnLevel,
    67  				Message: "3+4=7",
    68  				Caller: zapcore.EntryCaller{
    69  					Defined:  true,
    70  					File:     file,
    71  					Line:     line + 5,
    72  					Function: functionName,
    73  				},
    74  			},
    75  			Context: []zap.Field{zap.Error(errors.ErrUnsupported)},
    76  		},
    77  	})
    78  	assert.Equal(t, allEntries[2].Entry, zapcore.Entry{
    79  		Level:   zapcore.InfoLevel,
    80  		Message: "connected",
    81  		Caller: zapcore.EntryCaller{
    82  			Defined:  true,
    83  			File:     file,
    84  			Line:     line + 6,
    85  			Function: functionName,
    86  		},
    87  	})
    88  	assert.ElementsMatch(t, allEntries[2].Context, []zap.Field{
    89  		zap.Int("x", 1234),
    90  		zap.Float64("y", 9.75),
    91  	})
    92  }