Documentation
¶
Overview ¶
Package alog represents application level logger, it's based on the structured log package provided within the standard library called slog.
Shortcut agreements:
- "a" is for slog.Attr (attribute)
- "v" is for slog.Value (value)
Index ¶
- Constants
- Variables
- func Debug(msg string, args ...any)
- func DefaultReplaceAttr(groups []string, a slog.Attr) slog.Attr
- func Emergency(msg string, args ...any)
- func Env(envPrefixes ...string) func(string) string
- func Error(msg string, args ...any)
- func Info(msg string, args ...any)
- func Log(ctx context.Context, level slog.Level, msg string, args ...any)
- func LogLevelString(level slog.Level) string
- func NewContext(ctx context.Context, logger *Logger) context.Context
- func NewContextWithCancel(ctx context.Context, logger *Logger) (context.Context, context.CancelFunc)
- func Notice(msg string, args ...any)
- func ParseSilent() bool
- func ParseVerbosity() (level slog.Level)
- func ParseVerbosityInt() (verbosity int)
- func RemoveTime(groups []string, a slog.Attr) slog.Attr
- func ReplaceDefault(logger *Logger)
- func SetDefault(logger *Logger)
- func SetEnv(envPrefixes ...string)
- func Trace(msg string, args ...any)
- func Warning(msg string, args ...any)
- type Logger
- type Sensitive
Constants ¶
const ( // LevelTrace is tracing level keeps debug-related information, usefull // to trace system behavior step by step. LevelTrace = slog.Level(-8) // LevelDebug is debugging level. At debugging level there might be // additional information about how application serves its actions. // Debug could be used to inspect how data is processed. // The difference between trace and debug is that debug should not consume // to chunky data to print it. LevelDebug = slog.LevelDebug // LevelInfo standard information level everything which is useful for // operation specialists. LevelInfo = slog.LevelInfo // LevelNotice stands for "enhanced" information, i.e. emphasized information which // might be helpful to inspect (but it's still ok). The difference between // warning and info is that knowledge does not require user actions (i.e. nothing is broken), // but it might be important to inform user with disabled info level. // The level could be treated as notification level. LevelNotice = slog.Level(2) // LevelWarning stands for warnings, warnings might expose information about // possible issues during running application, however, the app in the case // of such issues still available to maintain properly. LevelWarning = slog.LevelWarn // LevelError stands for errors occurred while application was/is running. // It means something should be done outside the application scope to make it fixed/start // running properly. LevelError = slog.LevelError // LevelEmergency stands for extraordinary errors. LevelEmergency = slog.Level(12) )
Logging Levels.
const ( // EnvVerbosity is an environment variable (suffix) to use for manipulating with verbosity. // verbosity takes integer values like 0, 1, 2, etc. // It can be used with ${APP}_VERBOSITY where ${APP} is set over [Env], [SetEnv] helpers. EnvVerbosity = "VERBOSITY" // EnvSilent is an environment variable to use for manipulating with logger in general to // switch it off. EnvSilent = "SILENT" // FlagSilent is a full argument flag for silent option. FlagSilent = "--silent" )
Env settings for the package.
const ( // EnvSensitiveDisable keeps environment variable that // disables sensitive formatter. Use it only for debug // purposes and never set it on. EnvSensitiveDisable = "DISABLE_SENSITIVE_REDUCTION" )
Variables ¶
var ( // DefaultHandlerOptions is used for new logger instances created objects. DefaultHandlerOptions = &slog.HandlerOptions{ AddSource: false, Level: LevelWarning, ReplaceAttr: DefaultReplaceAttr, } )
Functions ¶
func DefaultReplaceAttr ¶
DefaultReplaceAttr serves to expose additional log levels.
func Env ¶
Env provides a helper to join environment variable prefixes together and return one argument function as a helper.
func LogLevelString ¶
LogLevelString shows log level in a human-readable format based on level.
func NewContext ¶
NewContext creates a context and set logger for its further use.
func NewContextWithCancel ¶
func NewContextWithCancel(ctx context.Context, logger *Logger) (context.Context, context.CancelFunc)
NewContextWithCancel creates a context with set logger and cancel function.
func ParseSilent ¶
func ParseSilent() bool
ParseSilent identifies if silent is set over cli arguments or environment variables.
func ParseVerbosity ¶
ParseVerbosity parses verbosity level for the application
Emergency: -2 Error: -1 Warning: 0 Notice: 1 Info: 2 Debug: 3 Trace: 4
func ParseVerbosityInt ¶
func ParseVerbosityInt() (verbosity int)
ParseVerbosityInt returns verbosity in [int] form.
func RemoveTime ¶
RemoveTime removes the top-level time attribute. It is intended to be used as a ReplaceAttr function, to make example output deterministic.
func ReplaceDefault ¶
func ReplaceDefault(logger *Logger)
ReplaceDefault replaces default logger link.
func SetDefault ¶
func SetDefault(logger *Logger)
SetDefault sets given logger as a default module logger.
func SetEnv ¶
func SetEnv(envPrefixes ...string)
SetEnv sets environment variables prefix (works on a global level, so you can set it once for whole application). Example:
SetEnv("MY", "APP") // results as MY_APP_<ARGUMENT>
Types ¶
type Logger ¶
Logger extends slog.Logger with additional helpers.
func FromContext ¶
FromContext extracts logger instance from context, if there's no pre-set logger => default will be used.