Documentation
¶
Overview ¶
Package logging provides utilities for creating logging callbacks from standard log.Logger instances. It offers a convenient way to configure logging for functional programming patterns where separate loggers for success and error cases are needed.
Index ¶
- func GetLogger() *slog.Logger
- func GetLoggerFromContext(ctx context.Context) *slog.Logger
- func LoggingCallbacks(loggers ...*log.Logger) (func(string, ...any), func(string, ...any))
- func SetLogger(l *slog.Logger) *slog.Logger
- func WithLogger(l *slog.Logger) pair.Kleisli[context.CancelFunc, context.Context, context.Context]
- type ContextCancel
- type Endomorphism
- type Pair
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetLogger ¶
GetLogger returns the current global logger instance. If no logger has been set via SetLogger, it returns slog.Default().
Returns:
- The current global *slog.Logger instance
Example:
logger := GetLogger()
logger.Info("Application started")
func GetLoggerFromContext ¶
GetLoggerFromContext retrieves a logger from the provided context. If no logger is found in the context, it returns the global logger.
This function is useful in applications where different parts of the code need access to context-specific loggers, such as in request handlers where each request might have its own logger with specific attributes.
Parameters:
- ctx: The context.Context from which to retrieve the logger
Returns:
- A *slog.Logger instance, either from the context or the global logger
Example:
func handleRequest(ctx context.Context) {
logger := GetLoggerFromContext(ctx)
logger.Info("Processing request")
}
func LoggingCallbacks ¶
LoggingCallbacks creates a pair of logging callback functions from the provided loggers. It returns two functions that can be used for logging messages, typically one for success cases and one for error cases.
The behavior depends on the number of loggers provided:
- 0 loggers: Returns two callbacks using log.Default() for both success and error logging
- 1 logger: Returns two callbacks both using the provided logger
- 2+ loggers: Returns callbacks using the first logger for success and second for errors
Parameters:
- loggers: Variable number of *log.Logger instances (0, 1, or more)
Returns:
- First function: Callback for success/info logging (signature: func(string, ...any))
- Second function: Callback for error logging (signature: func(string, ...any))
Example:
// Using default logger for both infoLog, errLog := LoggingCallbacks() // Using custom logger for both customLogger := log.New(os.Stdout, "APP: ", log.LstdFlags) infoLog, errLog := LoggingCallbacks(customLogger) // Using separate loggers for info and errors infoLogger := log.New(os.Stdout, "INFO: ", log.LstdFlags) errorLogger := log.New(os.Stderr, "ERROR: ", log.LstdFlags) infoLog, errLog := LoggingCallbacks(infoLogger, errorLogger)
func SetLogger ¶
SetLogger sets the global logger instance and returns the previous logger. This function is useful for configuring application-wide logging behavior.
Parameters:
- l: The new *slog.Logger to set as the global logger
Returns:
- The previous *slog.Logger that was set as the global logger
Example:
oldLogger := SetLogger(slog.New(slog.NewJSONHandler(os.Stdout, nil))) defer SetLogger(oldLogger) // Restore previous logger
func WithLogger ¶
WithLogger returns a Kleisli arrow that adds a logger to a context. A Kleisli arrow transforms a context into a ContextCancel pair containing a no-op cancel function and the new context with the embedded logger.
This is particularly useful in functional programming patterns where you want to compose context transformations, or when working with middleware that needs to inject loggers into request contexts.
Parameters:
- l: The *slog.Logger to embed in the context
Returns:
- A Kleisli arrow (function from context.Context to ContextCancel) that adds the logger to a context
Example:
// Create a logger transformation
addLogger := WithLogger(slog.New(slog.NewJSONHandler(os.Stdout, nil)))
// Apply it to a context
ctx := context.Background()
result := addLogger(ctx)
ctxWithLogger := pair.Second(result)
// Retrieve the logger later
logger := GetLoggerFromContext(ctxWithLogger)
logger.Info("Using context logger")
Types ¶
type ContextCancel ¶ added in v2.2.41
type ContextCancel = Pair[context.CancelFunc, context.Context]
ContextCancel represents a pair of a cancel function and a context. It is used in operations that create new contexts with cancellation capabilities.
The first element is the CancelFunc that should be called to release resources. The second element is the new Context that was created.
type Endomorphism ¶
type Endomorphism[A any] = endomorphism.Endomorphism[A]
Endomorphism represents a function that takes a value of type A and returns a value of the same type A. This is a type alias for endomorphism.Endomorphism[A], which is a fundamental concept in functional programming representing transformations that preserve type.
In the context of the logging package, this is primarily used for context transformations, such as adding a logger to a context while maintaining the context.Context type.
Type Parameters:
- A: The type that the endomorphism operates on
Example:
// An endomorphism that adds a logger to a context var addLogger Endomorphism[context.Context] = WithLogger(myLogger) // Apply the transformation ctx := context.Background() newCtx := addLogger(ctx) // Both ctx and newCtx are context.Context