util

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 30, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CTXKeyUser          contextKey = "user"
	CTXKeyAccessToken   contextKey = "access_token"
	CTXKeyRequestID     contextKey = "request_id"
	CTXKeyDisableLogger contextKey = "disable_logger"
	CTXKeyCacheControl  contextKey = "cache_control"
)

Variables

View Source
var ErrTimeout = errors.New("timeout while waiting for operation to complete")

Functions

func DisableLogger added in v1.1.0

func DisableLogger(ctx context.Context, shouldDisable bool) context.Context

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 GetEnv

func GetEnv(key string, defaultVal string) string

func GetEnvAsBool

func GetEnvAsBool(key string, defaultVal bool) bool

func GetEnvAsInt

func GetEnvAsInt(key string, defaultVal int) int

func LogFromContext added in v1.1.0

func LogFromContext(ctx context.Context) *zerolog.Logger

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

func LogFromEchoContext(c echo.Context) *zerolog.Logger

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 LogLevelFromString(s string) zerolog.Level

func RequestIDFromContext added in v1.1.0

func RequestIDFromContext(ctx context.Context) (string, error)

RequestIDFromContext returns the ID of the (HTTP) request, returning an error if it is not present.

func Retry

func Retry(attempts int, sleep time.Duration, f func() error) error

func ShouldDisableLogger added in v1.1.0

func ShouldDisableLogger(ctx context.Context) bool

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.

func WaitWithTimeout added in v1.1.0

func WaitWithTimeout[T any](ctx context.Context, timeout time.Duration, operation func(context.Context) (T, error)) (T, error)

WaitWithTimeout waits for the operation to complete of returns the ErrTimeout.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL