Documentation
¶
Overview ¶
Package logging provides helper functions to use a structured logger.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewJsonLogger ¶
NewJsonLogger creates a new structured logger in JSON format.
Example ¶
This example has no Output comment on purpose: the logger writes JSON with a timestamp, so the bytes differ on every run.
package main
import (
"log/slog"
"github.com/andygeiss/cloud-native-utils/logging"
)
func main() {
// LOGGING_LEVEL picks the level; it defaults to INFO.
logger := logging.NewJsonLogger()
logger.Info("server started", slog.Int("port", 8080))
logger.Error("upstream unreachable", slog.String("host", "db.internal"))
}
Output:
func WithLogging ¶ added in v0.1.93
func WithLogging(logger *slog.Logger, next http.HandlerFunc) http.HandlerFunc
WithLogging logs the request with method, path and duration without path masking. To mask sensitive path segments, use WithLoggingCustom with a configured PathSanitizer.
func WithLoggingCustom ¶ added in v0.4.9
func WithLoggingCustom(logger *slog.Logger, sanitizer *PathSanitizer, next http.HandlerFunc) http.HandlerFunc
WithLoggingCustom logs the request with method, path and duration using a custom path sanitizer.
Types ¶
type PathSanitizer ¶ added in v0.4.9
type PathSanitizer struct {
// PathValues maps path parameter names to their placeholder representations.
PathValues map[string]string
}
PathSanitizer configures which path segments should be masked during logging. Example usage:
sanitizer := logging.NewPathSanitizer(map[string]string{
"session_id": "{session_id}",
"user_id": "{user_id}",
"order_id": "{order_id}",
})
handler := logging.WithLoggingCustom(logger, sanitizer, nextHandler)
func NewPathSanitizer ¶ added in v0.4.9
func NewPathSanitizer(pathValues map[string]string) *PathSanitizer
NewPathSanitizer creates a custom path sanitizer with specified path values.