...

Package grpc_logsettable

import "github.com/grpc-ecosystem/go-grpc-middleware/logging/settable"
Overview
Index
Examples

Overview ▾

grpc_logsettable contains a thread-safe wrapper around grpc-logging infrastructure.

The go-grpc assumes that logger can be only configured once as the `SetLoggerV2` method is: ```Not mutex-protected, should be called before any gRPC functions.```

This package allows to supply parent logger once ("before any grpc"), but later change underlying implementation in thread-safe way when needed.

It's in particular useful for testing, where each testcase might need its own logger.

type SettableLoggerV2

SettableLoggerV2 is thread-safe.

type SettableLoggerV2 interface {
    grpclog.LoggerV2
    // Sets given logger as the underlying implementation.
    Set(loggerv2 grpclog.LoggerV2)
    // Sets `discard` logger as the underlying implementation.
    Reset()
}

Example (Init)

Code:

l1 := grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, ioutil.Discard)
l2 := grpclog.NewLoggerV2(os.Stdout, os.Stdout, os.Stdout)

settableLogger := grpc_logsettable.ReplaceGrpcLoggerV2()
grpclog.Info("Discarded by default")

settableLogger.Set(l1)
grpclog.Info("Discarded log by l1")

settableLogger.Set(l2)
grpclog.Info("Emitted log by l2")
// Expected output: INFO: 2021/03/15 12:59:54 [Emitted log by l2]

func ReplaceGrpcLoggerV2

func ReplaceGrpcLoggerV2() SettableLoggerV2

ReplaceGrpcLoggerV2 creates and configures SettableLoggerV2 as grpc logger.