log

package
v0.0.0-...-7457a68 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2022 License: GPL-2.0 Imports: 5 Imported by: 0

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

View Source
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"
)
View Source
const (
	FieldServiceName string = "service"
	FieldLogScopeID  string = "log_scope_id"
)
View Source
const (
	CtxScopeKey scopeKey = "logScope"
	CtxHubKey   hubKey   = "logHub"
	CtxIDKey    idKey    = "logID"
)
View Source
const LogIDHeader string = "x-log-scope-id"

Variables

This section is empty.

Functions

func ContextWithScope

func ContextWithScope(ctx context.Context) context.Context

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 DebugCtx

func DebugCtx(ctx context.Context, msg string, fields ...interface{})

DebugCtx is a wrapper around global's log DebugCtx

func Error

func Error(msg string, fields ...interface{})

Error is a wrapper around global's log Error

func ErrorCtx

func ErrorCtx(ctx context.Context, msg string, fields ...interface{})

ErrorCtx is a wrapper around global's log ErrorCtx

func Fatal

func Fatal(msg string, fields ...interface{})

Fatal is a wrapper around global's log Fatal

func FatalCtx

func FatalCtx(ctx context.Context, msg string, fields ...interface{})

FatalCtx is a wrapper around global's log FatalCtx

func Info

func Info(msg string, fields ...interface{})

Info is a wrapper around global's log Info

func InfoCtx

func InfoCtx(ctx context.Context, msg string, fields ...interface{})

InfoCtx is a wrapper around global's log InfoCtx

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.

func Warn

func Warn(msg string, fields ...interface{})

Warn is a wrapper around global's log Warn

func WarnCtx

func WarnCtx(ctx context.Context, msg string, fields ...interface{})

WarnCtx is a wrapper around global's log Warn

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.

func NewNilLogger

func NewNilLogger() Logger

NewNilLogger returns an empty nil logger.

type NilLogger

type NilLogger struct{}

NilLogger is a nil logger implementation used for testing.

func (*NilLogger) Debug

func (*NilLogger) Debug(_ string, _ ...interface{})

Debug is a empty stub for nil logger. It does nothing.

func (*NilLogger) DebugCtx

func (*NilLogger) DebugCtx(_ context.Context, _ string, _ ...interface{})

Debug is a empty stub for nil logger. It does nothing.

func (*NilLogger) Error

func (*NilLogger) Error(_ string, _ ...interface{})

Error is a empty stub for nil logger. It does nothing.

func (*NilLogger) ErrorCtx

func (*NilLogger) ErrorCtx(_ context.Context, _ string, _ ...interface{})

ErrorCtx is a empty stub for nil logger. It does nothing.

func (*NilLogger) Fatal

func (*NilLogger) Fatal(_ string, _ ...interface{})

Fatal is a empty stub for nil logger. It does nothing.

func (*NilLogger) FatalCtx

func (*NilLogger) FatalCtx(_ context.Context, _ string, _ ...interface{})

FatalCtx is a empty stub for nil logger. It does nothing.

func (*NilLogger) Info

func (*NilLogger) Info(_ string, _ ...interface{})

Info is a empty stub for nil logger. It does nothing.

func (*NilLogger) InfoCtx

func (*NilLogger) InfoCtx(_ context.Context, _ string, _ ...interface{})

InfoCtx is a empty stub for nil logger. It does nothing.

func (*NilLogger) Warn

func (*NilLogger) Warn(_ string, _ ...interface{})

Warn is a empty stub for nil logger. It does nothing.

func (*NilLogger) WarnCtx

func (*NilLogger) WarnCtx(_ context.Context, _ string, _ ...interface{})

WarnCtx is a empty stub for nil logger. It does nothing.

Jump to

Keyboard shortcuts

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