github.com/matrixorigin/matrixone@v1.2.0/pkg/logutil/report_test.go (about)

     1  // Copyright 2022 Matrix Origin
     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  	"context"
    19  	"sync"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  	"go.uber.org/zap"
    24  	"go.uber.org/zap/buffer"
    25  	"go.uber.org/zap/zapcore"
    26  )
    27  
    28  var reportMux sync.Mutex
    29  
    30  func TestNoop(t *testing.T) {
    31  	require.Equal(t, zap.String("span", "{}"), noopContextField(context.Background()))
    32  	buf, err := noopReportZap(nil, zapcore.Entry{}, nil)
    33  	require.Equal(t, nil, err)
    34  	require.Equal(t, "", buf.String())
    35  	require.Equal(t, zap.Bool(MOInternalFiledKeyNoopReport, true), NoReportFiled())
    36  }
    37  
    38  func TestReport(t *testing.T) {
    39  	reportMux.Lock()
    40  	defer reportMux.Unlock()
    41  	type fields struct {
    42  		Level      string
    43  		Format     string
    44  		Filename   string
    45  		MaxSize    int
    46  		MaxDays    int
    47  		MaxBackups int
    48  
    49  		Entry zapcore.Entry
    50  	}
    51  	tests := []struct {
    52  		name         string
    53  		fields       fields
    54  		wantLevel    zap.AtomicLevel
    55  		wantOpts     []zap.Option
    56  		wantSyncer   zapcore.WriteSyncer
    57  		wantEncoder  zapcore.Encoder
    58  		wantSinksLen int
    59  	}{
    60  		{
    61  			name: "normal",
    62  			fields: fields{
    63  				Level:      "debug",
    64  				Format:     "console",
    65  				Filename:   "",
    66  				MaxSize:    0,
    67  				MaxDays:    0,
    68  				MaxBackups: 0,
    69  
    70  				Entry: zapcore.Entry{Level: zapcore.DebugLevel, Message: "console msg"},
    71  			},
    72  			wantLevel:    zap.NewAtomicLevelAt(zap.DebugLevel),
    73  			wantOpts:     []zap.Option{zap.AddStacktrace(zapcore.FatalLevel), zap.AddCaller()},
    74  			wantSyncer:   getConsoleSyncer(),
    75  			wantEncoder:  getLoggerEncoder("console"),
    76  			wantSinksLen: 2,
    77  		},
    78  	}
    79  
    80  	zapReporter.Store(reportZapFunc(func(encoder zapcore.Encoder, entry zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error) {
    81  		buffer, err := encoder.EncodeEntry(entry, fields)
    82  		return buffer, err
    83  	}))
    84  	for _, tt := range tests {
    85  
    86  		t.Run(tt.name, func(t *testing.T) {
    87  			cfg := &LogConfig{
    88  				Level:      tt.fields.Level,
    89  				Format:     tt.fields.Format,
    90  				Filename:   tt.fields.Filename,
    91  				MaxSize:    tt.fields.MaxSize,
    92  				MaxDays:    tt.fields.MaxDays,
    93  				MaxBackups: tt.fields.MaxBackups,
    94  			}
    95  			require.Equal(t, tt.wantLevel, cfg.getLevel())
    96  			require.Equal(t, len(tt.wantOpts), len(cfg.getOptions()))
    97  			require.Equal(t, tt.wantSyncer, cfg.getSyncer())
    98  			wantMsg, _ := tt.wantEncoder.EncodeEntry(tt.fields.Entry, nil)
    99  			gotMsg, _ := cfg.getEncoder().EncodeEntry(tt.fields.Entry, nil)
   100  			require.Equal(t, wantMsg.String(), gotMsg.String())
   101  			require.Equal(t, 2, len(cfg.getSinks()))
   102  			SetupMOLogger(cfg)
   103  
   104  			gotCfg := getGlobalLogConfig()
   105  			require.Equal(t, cfg.DisableStore, gotCfg.DisableStore)
   106  
   107  			Info("hello", GetContextFieldFunc()(context.Background()), zap.Int("int", 0))
   108  			Info("hello, noop", NoReportFiled())
   109  
   110  		})
   111  	}
   112  }