Documentation ¶
Overview ¶
Package log contains the improved logger interface and it's nil implementation Log levels / verbosity go from lowest to highest: Debug, Info, Warn, Error. Setting the logger verbosity to a value will print that or higher level logs.
Index ¶
- Constants
- func ContextWithScope(ctx context.Context) context.Context
- func Debug(msg string, fields ...interface{})
- func DebugCtx(ctx context.Context, msg string, fields ...interface{})
- func Error(msg string, fields ...interface{})
- func ErrorCtx(ctx context.Context, msg string, fields ...interface{})
- func Fatal(msg string, fields ...interface{})
- func FatalCtx(ctx context.Context, msg string, fields ...interface{})
- func Info(msg string, fields ...interface{})
- func InfoCtx(ctx context.Context, msg string, fields ...interface{})
- func NewZapLogger(cfg *Config) *zapLogger
- func SetGlobal(l ContextLogger)
- func Warn(msg string, fields ...interface{})
- func WarnCtx(ctx context.Context, msg string, fields ...interface{})
- type Config
- type ContextLogger
- type Logger
- type NilLogger
- func (*NilLogger) Debug(_ string, _ ...interface{})
- func (*NilLogger) DebugCtx(_ context.Context, _ string, _ ...interface{})
- func (*NilLogger) Error(_ string, _ ...interface{})
- func (*NilLogger) ErrorCtx(_ context.Context, _ string, _ ...interface{})
- func (*NilLogger) Fatal(_ string, _ ...interface{})
- func (*NilLogger) FatalCtx(_ context.Context, _ string, _ ...interface{})
- func (*NilLogger) Info(_ string, _ ...interface{})
- func (*NilLogger) InfoCtx(_ context.Context, _ string, _ ...interface{})
- func (*NilLogger) Warn(_ string, _ ...interface{})
- func (*NilLogger) WarnCtx(_ context.Context, _ string, _ ...interface{})
Constants ¶
const ( // DebugVerbosity is used for printing debug logs and higher. DebugVerbosity = "DEBUG" // InfoVerbosity is used for printing info logs and higher. InfoVerbosity = "INFO" // WarnVerbosity is used for printing warning logs and higher. WarnVerbosity = "WARN" // ErrorVerbosity is used for printing error logs only. ErrorVerbosity = "ERROR" )
const ( FieldServiceName string = "service" FieldLogScopeID string = "log_scope_id" )
const ( CtxScopeKey scopeKey = "logScope" CtxHubKey hubKey = "logHub" CtxIDKey idKey = "logID" )
const LogIDHeader string = "x-log-scope-id"
Variables ¶
This section is empty.
Functions ¶
func ContextWithScope ¶
ContextWithScope will return the context with a new scope added. LogCtx functions will try to get the scope out of the context before logging so we can group logs by their scope.
func Debug ¶
func Debug(msg string, fields ...interface{})
Debug is a wrapper around global's log Debug
func Error ¶
func Error(msg string, fields ...interface{})
Error is a wrapper around global's log Error
func Fatal ¶
func Fatal(msg string, fields ...interface{})
Fatal is a wrapper around global's log Fatal
func NewZapLogger ¶
func NewZapLogger(cfg *Config) *zapLogger
NewZapLogger parses the config and returns a new logger. It will return an error only if it fails initializing a zap logger. It will return the logger if it fails setting up hooks, but will print the errors.
func SetGlobal ¶
func SetGlobal(l ContextLogger)
SetGlobal sets the global logger, unsafe to call if logger is already in use.
Types ¶
type Config ¶
type Config struct { // LogVerbosity is is used to declare a level of logging. // Valid values are log.Debug, log.Info, log.Warn and log.Error // Values go from the lowest to highest. Choosing a lower value will print all higher values. // Ignoring this field or setting it to an incompatible verbosity will default the logger to log.Info. LogVerbosity string // Name of the executable using the logger. ServiceName string }
Config is used to initialize a logger.
type ContextLogger ¶
type ContextLogger interface { Logger DebugCtx(ctx context.Context, msg string, fields ...interface{}) InfoCtx(ctx context.Context, msg string, fields ...interface{}) WarnCtx(ctx context.Context, msg string, fields ...interface{}) ErrorCtx(ctx context.Context, msg string, fields ...interface{}) FatalCtx(ctx context.Context, msg string, fields ...interface{}) }
ContextLogger is an interface which declares logging with context It contains all regular Logger methods Context logs will try to extract the current logging scope and group the logs.
type Logger ¶
type Logger interface { Debug(msg string, fields ...interface{}) Info(msg string, fields ...interface{}) Warn(msg string, fields ...interface{}) Error(msg string, fields ...interface{}) Fatal(msg string, fields ...interface{}) }
Logger is an interface which declares logging method of different levels. Every logger method accepts a message as the first parameter and key value pairs as varargs. If any of the keys do not have a pair they will not be printed. Keys must be of type string, if any of the keys are not, that key value pair will be skipped.
type NilLogger ¶
type NilLogger struct{}
NilLogger is a nil logger implementation used for testing.