package grpc_logsettable

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

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.

Index

Examples

Types

type SettableLoggerV2

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

SettableLoggerV2 is thread-safe.

Example (Init)

Code:play 

package main

import (
	"io/ioutil"
	"os"

	grpc_logsettable "github.com/grpc-ecosystem/go-grpc-middleware/logging/settable"
	"google.golang.org/grpc/grpclog"
)

func main() {
	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.

Source Files

doc.go logsettable.go

Version
v1.4.0 (latest)
Published
Mar 15, 2023
Platform
windows/amd64
Imports
3 packages
Last checked
2 hours ago

Tools for package owners.