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 ¶
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]) StructMapper ¶
StructMapper appends the map as JSON at the tail.
Click to show internal directories.
Click to hide internal directories.