logger

package module
v0.0.0-...-d27b3e9 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2021 License: MIT Imports: 10 Imported by: 0

README

logger

Simple structured logging with severity awareness. Logging is one essential observability technique in order to not fly blindly. Structuring logs is simple and yet powerful because it supports different formats that can be addapted to any application's needs.

Log Level Awareness

There are 4 log levels supported, namely debug, info, warning and error. Logs are emitted based on filter configurations provided to the logger creation. Below is shown which logger setting causes which behaviour of emitted logs.

logger setting emitted logs
debug debug, info, warning, error
info info, warning, error
warning warning, error
error error
Logging In Code

Below is an example of emitting info logs providing information of expected business logic behaviour.

r.logger.Log(ctx, "level", "info", "message", "something important happened")

Below is an example of emitting error logs providing information of unexpected business logic behaviour. Note that stack trace logging is wel integrated with tracer.

r.logger.Log(ctx, "level", "error", "message", "something bad happened", "stack", tracer.JSON(err))
Log Line Printing

Using logger prints log lines like shown in the example below. Note that you can configure the io.Writer that actually handles the byte streams. The default is configured to be os.Stdout.

{
	"caller": "--REPLACED--/logger_test.go:129",
	"level": "info",
	"message": "something important happened",
	"time": "2006-01-02 15:04:05"
}

Documentation

Index

Constants

View Source
const (
	LevelDebug   = "debug"
	LevelInfo    = "info"
	LevelWarning = "warning"
	LevelError   = "error"
)
View Source
const (
	KeyCaller = "caller"
	KeyStack  = "stack"
	KeyTime   = "time"
)
View Source
const (
	LevelKey = "level"
)

Variables

View Source
var DefaultCaller = func() string {
	return fmt.Sprintf("%+v", stack.Caller(2))
}
View Source
var DefaultFilter = NewLevelFilter(LevelInfo)
View Source
var DefaultFormatter = JSONFormatter
View Source
var DefaultTimer = func() string {
	return time.Now().UTC().Format("2006-01-02 15:04:05")
}
View Source
var DefaultWriter = os.Stdout

Functions

func IsInvalidConfig

func IsInvalidConfig(err error) bool

func JSONFormatter

func JSONFormatter(m map[string]string) string

JSONFormatter transforms the given map into the string representation of a simple JSON object. One specificity of JSONFormatter is that it treats the value of key "stack" differently. In case a key-value pair with the key "stack" is provided, the associated value is treated like a JSON object, and not like a JSON string. This difference in behaviour is unique compared to all other key-value pairs map may carry with it.

func NewLevelFilter

func NewLevelFilter(l string) func(m map[string]string) bool

Types

type Config

type Config struct {
	Caller    func() string
	Filter    func(m map[string]string) bool
	Formatter func(m map[string]string) string
	Timer     func() string
	Writer    io.Writer
}

type Interface

type Interface interface {
	// Log prints the given key-value pairs. Additionally the given context may
	// provide information injected by the meta package.
	Log(ctx context.Context, kvs ...string)
}

Interface implementations emit messages to gather certain runtime information.

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

func New

func New(config Config) (*Logger, error)

func (*Logger) Log

func (l *Logger) Log(ctx context.Context, kvs ...string)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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