trpc.group/trpc-go/trpc-go@v1.0.3/log/zaplog_test.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package log
    15  
    16  import (
    17  	"bytes"
    18  
    19  	"go.uber.org/zap"
    20  	"go.uber.org/zap/zapcore"
    21  )
    22  
    23  // NewZapBufLogger return a buffer logger
    24  func NewZapBufLogger(buf *bytes.Buffer, skip int) Logger {
    25  	encoder := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
    26  	core := zapcore.NewCore(encoder, zapcore.AddSync(buf), zapcore.DebugLevel)
    27  	return &zapLog{
    28  		levels: []zap.AtomicLevel{},
    29  		logger: zap.New(
    30  			core,
    31  			zap.AddCallerSkip(skip),
    32  			zap.AddCaller(),
    33  		),
    34  	}
    35  }
    36  
    37  // NewZapFatalLogger return a fatal hook logger
    38  func NewZapFatalLogger(h zapcore.CheckWriteHook) Logger {
    39  	core, _ := newConsoleCore(&OutputConfig{
    40  		Writer:    "console",
    41  		Level:     "debug",
    42  		Formatter: "console",
    43  	})
    44  	return &zapLog{
    45  		levels: []zap.AtomicLevel{},
    46  		logger: zap.New(
    47  			core,
    48  			zap.AddCallerSkip(1),
    49  			zap.AddCaller(),
    50  			zap.WithFatalHook(h),
    51  		)}
    52  }