container

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2022 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func UpdateMap

func UpdateMap[K comparable, V any](a, b map[K]V) map[K]V

UpdateMap returns a new map with the merged keys and values of a and b.

Types

type Context

type Context interface {
	Data() Map[string, any]
	L() *logger.Logger
	Clone() Context
	WithContext(ctx context.Context) context.Context
}

Context holds a request-scoped data and a logger to partial structured logging.

Example
package main

import (
	"context"
	"log"
	"os"

	"github.com/berquerant/logger"
	"github.com/berquerant/logger/container"
)

func main() {
	log.SetFlags(0)
	log.SetOutput(os.Stdout)

	ctx := container.New(map[string]any{
		"RequestID": "stone1",
	}, logger.MustNewMapperFunc(logger.LogLevelToPrefixMapper).
		Next(logger.StandardLogConsumer)).WithContext(context.TODO())

	c1 := container.FromContext(ctx)
	c1.L().Info("first")
	c1.Data().Set("Path", "/update")
	c1.L().Info("second")

	func(ctx context.Context) {
		c2 := container.FromContext(ctx)
		c2.Data().Set("Verb", "POST")
		c2.L().Info("third")
	}(c1.Clone().WithContext(ctx))

	container.FromContext(ctx).L().Info("forth")
}
Output:

I | first | {"RequestID":"stone1"}
I | second | {"Path":"/update","RequestID":"stone1"}
I | third | {"Path":"/update","RequestID":"stone1","Verb":"POST"}
I | forth | {"Path":"/update","RequestID":"stone1"}

func FromContext

func FromContext(ctx context.Context) Context

func New

func New(data Map[string, any], mapper logger.MapperFunc) Context

type Map

type Map[K comparable, V any] map[K]V

func NewMap

func NewMap[K comparable, V any]() Map[K, V]

func (Map[K, V]) Clone

func (m Map[K, V]) Clone() Map[K, V]

func (Map[K, V]) Get

func (m Map[K, V]) Get(key K) (V, bool)

func (Map[K, V]) Set

func (m Map[K, V]) Set(key K, value V) Map[K, V]

func (Map[K, V]) StructMapper

func (m Map[K, V]) StructMapper(ev logger.Event) logger.Event

StructMapper appends the map as JSON at the tail.

func (Map[K, V]) Update

func (m Map[K, V]) Update(other Map[K, V]) Map[K, V]

Jump to

Keyboard shortcuts

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