kit

package module
v0.0.0-...-eb2c8a6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2020 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Example (Initialization)

Initialization shows a relatively complex initialization sequence.

package main

import (
	"github.com/go-kit/kit/log"
	middleware "github.com/grpc-ecosystem/go-grpc-middleware"
	"github.com/grpc-ecosystem/go-grpc-middleware/interceptors/logging"
	"github.com/grpc-ecosystem/go-grpc-middleware/interceptors/tags"
	"github.com/grpc-ecosystem/go-grpc-middleware/providers/kit"
	"google.golang.org/grpc"
)

var customFunc logging.CodeToLevel

func main() {
	// Logger is used, allowing pre-definition of certain fields by the user.
	logger := log.NewNopLogger()
	// Shared options for the logger, with a custom gRPC code to log level function.
	opts := []logging.Option{
		logging.WithLevels(customFunc),
	}
	// Create a server, make sure we put the tags context before everything else.
	_ = grpc.NewServer(
		middleware.WithUnaryServerChain(
			tags.UnaryServerInterceptor(tags.WithFieldExtractor(tags.CodeGenRequestFieldExtractor)),
			logging.UnaryServerInterceptor(kit.InterceptorLogger(logger), opts...),
		),
		middleware.WithStreamServerChain(
			tags.StreamServerInterceptor(tags.WithFieldExtractor(tags.CodeGenRequestFieldExtractor)),
			logging.StreamServerInterceptor(kit.InterceptorLogger(logger), opts...),
		),
	)
}
Output:

Example (InitializationWithDurationFieldOverride)
package main

import (
	"github.com/go-kit/kit/log"
	middleware "github.com/grpc-ecosystem/go-grpc-middleware"
	"github.com/grpc-ecosystem/go-grpc-middleware/interceptors/logging"
	"github.com/grpc-ecosystem/go-grpc-middleware/interceptors/tags"
	"github.com/grpc-ecosystem/go-grpc-middleware/providers/kit"
	"google.golang.org/grpc"
)

var customDurationToFields logging.DurationToFields

func main() {
	// Logger is used, allowing pre-definition of certain fields by the user.
	logger := log.NewNopLogger()
	// Shared options for the logger, with a custom duration to log field function.
	opts := []logging.Option{
		logging.WithDurationField(customDurationToFields),
	}
	// Create a server, make sure we put the tags context before everything else.
	_ = grpc.NewServer(
		middleware.WithUnaryServerChain(
			tags.UnaryServerInterceptor(),
			logging.UnaryServerInterceptor(kit.InterceptorLogger(logger), opts...),
		),
		middleware.WithStreamServerChain(
			tags.StreamServerInterceptor(),
			logging.StreamServerInterceptor(kit.InterceptorLogger(logger), opts...),
		),
	)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

type Logger struct {
	log.Logger
}

func InterceptorLogger

func InterceptorLogger(logger log.Logger) *Logger

func (*Logger) Log

func (l *Logger) Log(lvl logging.Level, msg string)

func (*Logger) With

func (l *Logger) With(fields ...string) logging.Logger

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL