Documentation
¶
Index ¶
- Constants
- Variables
- func DisableLogger(ctx context.Context, shouldDisable bool) context.Context
- func GetEnv(key string, defaultVal string) string
- func GetEnvAsBool(key string, defaultVal bool) bool
- func GetEnvAsInt(key string, defaultVal int) int
- func LogFromContext(ctx context.Context) *zerolog.Logger
- func LogFromEchoContext(c echo.Context) *zerolog.Logger
- func LogLevelFromString(s string) zerolog.Level
- func RequestIDFromContext(ctx context.Context) (string, error)
- func Retry(attempts int, sleep time.Duration, f func() error) error
- func ShouldDisableLogger(ctx context.Context) bool
- func WaitWithCancellableCtx[T any](ctx context.Context, operation func(context.Context) (T, error)) (T, error)
- func WaitWithTimeout[T any](ctx context.Context, timeout time.Duration, ...) (T, error)
Constants ¶
const ( CTXKeyUser contextKey = "user" CTXKeyAccessToken contextKey = "access_token" CTXKeyRequestID contextKey = "request_id" CTXKeyDisableLogger contextKey = "disable_logger" CTXKeyCacheControl contextKey = "cache_control" )
Variables ¶
var ErrTimeout = errors.New("timeout while waiting for operation to complete")
Functions ¶
func DisableLogger ¶ added in v1.1.0
DisableLogger toggles the indication whether `util.LogFromContext` should return a disabled logger for a context if none has been set by our logging middleware before. Whilst the usecase for a disabled logger are relatively minimal (we almost always want to have some log output, even if the context was not directly derived from a HTTP request), this functionality was provideds so you can switch back to the old zerolog behavior if so desired.
func GetEnvAsBool ¶
func GetEnvAsInt ¶
func LogFromContext ¶ added in v1.1.0
LogFromContext returns a request-specific zerolog instance using the provided context. The returned logger will have the request ID as well as some other value predefined. If no logger is associated with the context provided, the global zerolog instance will be returned instead - this function will _always_ return a valid (enabled) logger. Should you ever need to force a disabled logger for a context, use `util.DisableLogger(ctx, true)` and pass the context returned to other code/`LogFromContext`.
func LogFromEchoContext ¶ added in v1.1.0
LogFromEchoContext returns a request-specific zerolog instance using the echo.Context of the request. The returned logger will have the request ID as well as some other value predefined.
func LogLevelFromString ¶ added in v1.1.0
func RequestIDFromContext ¶ added in v1.1.0
RequestIDFromContext returns the ID of the (HTTP) request, returning an error if it is not present.
func ShouldDisableLogger ¶ added in v1.1.0
ShouldDisableLogger checks whether the logger instance should be disabled for the provided context. `util.LogFromContext` will use this function to check whether it should return a default logger if none has been set by our logging middleware before, or fall back to the disabled logger, suppressing all output. Use `ctx = util.DisableLogger(ctx, true)` to disable logging for the given context.
func WaitWithCancellableCtx ¶ added in v1.1.0
func WaitWithCancellableCtx[T any](ctx context.Context, operation func(context.Context) (T, error)) (T, error)
WaitWithCancellableCtx runs the operation tracking the context state. If the given context is cancelled, the function returns directly with ErrTimeout.
Types ¶
This section is empty.