ctxval

package
v0.0.0-...-e1e9d1d Latest Latest
Warning

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

Go to latest
Published: May 20, 2021 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package ctxval provides type-safe functions to set and get values from a context. Because the context needs to be used in http handlers to store the request-specific logger (i.e. a logger with the current request's ID in its fields), it makes sense to be pragmatic about it and also store the main top-level logger in the context in the application's main function. This top-level logger will be properly overridden by the request's logger in a child context.

For this reason, the ctxval package also provides helper functions to log an error and an information message using the logger stored in the context, if one is present.

The current execution environment is also stored in the context and should be set in the application's main function (typically read from an environment variable). Again, it is a pragmatic choice to have it available here as the context is passed around all layers, instead of having a global variable somewhere that all those layers read from.

Index

Constants

This section is empty.

Variables

ValidEnvs is the set of valid environments. An application can change that set in its main function before storing the current environment in the top-level context, if necessary.

Functions

func EnvIn

func EnvIn(ctx context.Context, envs ...Environment) bool

EnvIn returns true if the execution environment stored in ctx is any of envs. It returns false otherwise.

func LogErr

func LogErr(ctx context.Context, err error, msg string)

LogErr is a helper function to log the provided error to the context logger if one is set.

func LogInfo

func LogInfo(ctx context.Context, msg string)

LogInfo is a helper function to log the provided information message to the context logger if one is set.

func Logger

func Logger(ctx context.Context) (log.Logger, bool)

Logger returns the logger stored in the context. If there is no logger in the context, it returns false as second value.

func SetEnv

func SetEnv(ctx context.Context, env Environment) context.Context

SetEnv sets the current execution environment in the context and returns the new context. It panics if env is not a valid environment.

func SetLogger

func SetLogger(ctx context.Context, l log.Logger) context.Context

SetLogger stores the logger l in the context and returns the new context.

Types

type Environment

type Environment string

Environment indicates an execution environment.

const (
	EnvLocal      Environment = "local"
	EnvTest       Environment = "test"
	EnvStaging    Environment = "staging"
	EnvProduction Environment = "production"
)

Standard list of valid execution environments.

func Env

func Env(ctx context.Context) (Environment, bool)

Env returns the current execution environment in the context. If there is no environment in the context, it returns false as second value.

Jump to

Keyboard shortcuts

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