slog

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2019 License: MIT Imports: 3 Imported by: 1

Documentation

Overview

Package slog is used to log from the other sebase packages. The purpose of this is to not depend on a particular log package since different users might want to use different packages. Plog will install itself here if used, but the default values simply creates a string and calls log.Printf.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func KVsMap

func KVsMap(kvs ...interface{}) map[string]interface{}

KVsMap converts a set of key-value pairs into a map. Each argument read is type checked. If it's a string, the next argument is used as a value. If it's a map[string]interface{} then the contents is copied to the return value. Same thing if it implements the ToMap interface, then the map contents is copied. If none of the types match, fmt.Sprint is used to convert to a string and the next argument is used as value. Sometimes this happens if you forget to add ... to expand the array when calling this function Error interface values are special handled, converted to the error message.

Types

type DefaultLogger

type DefaultLogger struct {
	Logf func(format string, v ...interface{})
}

DefaultLogger is used as a default logger, wrapping a Printf like function.

func (DefaultLogger) LogMsg

func (d DefaultLogger) LogMsg(msg string, kvs ...interface{})

LogMsg creates a string and calls the log.Printf like function contained if not nil. This is not a printf-like function, although the signature matches. Instead kvs is expected to contain alternating keys and values as documented in the KVsMap function.

type KV

type KV map[string]interface{}

KV can be used instead of a key-value pair in KVsMap function and any function that use it, e.g. DefaultLogger.LogMsg.

func (KV) ToMap

func (kvs KV) ToMap() map[string]interface{}

ToMap returns kvs, it implements the ToMap interface.

type Logger

type Logger func(msg string, kvs ...interface{})

Logger is the function type used to log.

The first argument is a human readable message. The rest are alternating keys and values as parsed by the KVsMap function. This is not a printf-like function, despite the signature. Typically you give key-value pairs with string keys and any type values as the kvs.

var (
	Critical Logger = DefaultLogger{log.Printf}.LogMsg
	Error    Logger = DefaultLogger{log.Printf}.LogMsg
	Warning  Logger = DefaultLogger{log.Printf}.LogMsg
	Info     Logger = DefaultLogger{log.Printf}.LogMsg
	Debug    Logger = DefaultLogger{nil}.LogMsg
)

func (*Logger) Disabe

func (logger *Logger) Disabe()

Disable sets the receiver to a value discaring the logs.

func (*Logger) SetLogPrintf

func (logger *Logger) SetLogPrintf()

SetLogPrintf sets the receiver to log to log.Printf via the DefaultLogger type. It's a convenience function. For example you can enable debug logging with slog.Debug.SetLogPrintf()

type ToMap

type ToMap interface {
	ToMap() map[string]interface{}
}

ToMap is used to check arguments passed to KVsMap. If this function is implemented the keys and values returned are added to the KVsMap return value.

Jump to

Keyboard shortcuts

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