Documentation
¶
Overview ¶
Package logevent provides utilities to implement canonical logging (wide log events) in Go.
Canonical logging emits a single structured log entry at the end of a unit of work (e.g., HTTP request, gRPC call, or background task) capturing the full lifecycle context, metadata, and status.
The package provides core context helpers (AddLogEventToContext, UpdateLogEvent, [LogItFunc], HandleWithLogEvent) as well as ready-to-use middleware for HTTP (in subpackage mw/http) and gRPC (in subpackage mw/grpc).
Index ¶
- Variables
- func AddLogEventToContext[L Logger, T any, PT PtrLogEvent[L, T]](parent context.Context, t T) (context.Context, func(l L))
- func HandleWithLogEvent[L Logger, T any, PT PtrLogEvent[L, T]](ctx context.Context, t T, logger L, handler func(context.Context))
- func UpdateLogEvent[L Logger, T any, PT PtrLogEvent[L, T]](ctx context.Context, f func(t PT)) error
- type DifferentLogEventTypeError
- type LogEvent
- type Logger
- type PtrLogEvent
Constants ¶
This section is empty.
Variables ¶
var ErrLogEventNotInitialized = errors.New("LogEvent not initialized")
ErrLogEventNotInitialized error returned when adding context to a log event but the log event was not initialized.
Functions ¶
func AddLogEventToContext ¶ added in v0.0.6
func AddLogEventToContext[L Logger, T any, PT PtrLogEvent[L, T]]( parent context.Context, t T, ) (context.Context, func(l L))
AddLogEventToContext adds a log event to the context. It can be used to custom add a log event to the context in any kind of scenario.
The function performs the following steps:
- Type-asserts the provided log event to get the pointer type (required by the constraint)
- Creates a wrapper around the pointer for concurrency support (sync.Once, sync.RWMutex)
- Stores the wrapper in the context under a type-safe key
This design allows handlers to update the log event during request processing and ensures the log event is only logged once and is thread-safe. To add more context to the log event, use UpdateLogEvent.
func HandleWithLogEvent ¶ added in v0.0.6
func HandleWithLogEvent[L Logger, T any, PT PtrLogEvent[L, T]]( ctx context.Context, t T, logger L, handler func(context.Context), )
HandleWithLogEvent is a generic helper function that encapsulates the common pattern of adding a log event to the context and executing a handler. It is used by both the HTTP middleware and gRPC interceptor.
The function performs the following steps:
- Creates a per-request copy of the log event struct (to avoid concurrent modifications)
- Type-asserts the copy to get the pointer type (required by the constraint)
- Creates a wrapper around the pointer for concurrency support (sync.Once, sync.RWMutex)
- Stores the wrapper in the context under a type-safe key
- Defers a call to log the event after the handler completes
- Calls the provided handler function with the updated context
- Checks if the handler updated the log event in the context and uses the updated version
This design allows handlers to update the log event during request processing and ensures the log event is only logged once and is thread-safe.
func UpdateLogEvent ¶ added in v0.0.6
UpdateLogEvent updates the log event stored in the context during request processing. It works with HTTP middleware, gRPC interceptors, or manual log event context lifecycle, allowing handlers to modify the log event that will be logged after the unit of work completes.
Parameters:
- ctx: The context containing the log event.
- f: A function that receives the pointer to the log event struct and modifies it.
Returns an error if the log event was not initialized (i.e., the request was not wrapped with AddLogEventMiddleware, UnaryServerInterceptor, or AddLogEventToContext).
Example with HTTP:
func myHandler(w http.ResponseWriter, r *http.Request) {
_ = logevent.UpdateLogEvent(r.Context(), func(log *RequestLog) {
log.Path = r.URL.Path
log.Method = r.Method
})
}
Example with gRPC:
func (s *server) MyRPC(ctx context.Context, req *pb.Request) (*pb.Response, error) {
_ = logevent.UpdateLogEvent(ctx, func(log *RPCLog) {
log.Method = "MyRPC"
})
return &pb.Response{}, nil
}
Types ¶
type DifferentLogEventTypeError ¶
type DifferentLogEventTypeError struct {
// contains filtered or unexported fields
}
DifferentLogEventTypeError is returned when the log event type is different from the previous one.
func (DifferentLogEventTypeError) Error ¶
func (l DifferentLogEventTypeError) Error() string
Error implements the error interface.
type PtrLogEvent ¶ added in v0.0.6
PtrLogEvent helps to make that only a pointer can be passed to the middleware. It is a constraint that ensures PT is a pointer to type T and implements logevent.LogEvent.