slog

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2022 License: MIT Imports: 16 Imported by: 0

README

slog GoDoc

slog is an opinionated structured logging implementation.

Documentation

Overview

Pulled from the github.com/oxtoacart/bpool

Index

Constants

View Source
const (
	SeverityDebug = "debug"
	SeverityInfo  = "info"
	SeverityWarn  = "warn"
	SeverityError = "error"
	SeverityPanic = "panic"
	SeverityFatal = "fatal"
)

Variables

View Source
var (
	// Writer is the writer interface which the logs will be written too.
	Writer = NewLockedWriteSyncer(os.Stdout)

	// TimeStampKey is the json key for the timestamp output.
	TimeStampKey = "ts"

	// TimeFormat will set the `slog.Time` output format if supplied. Defaults to `time.Unix()`.
	TimeFormat = ""

	// SeverityKey is the json key for the initial log type (info, warn, error, etc etc).
	SeverityKey = []byte("level")

	// TitleKey is the json key for the name of the log message.
	TitleKey = []byte("msg")

	// EnableDebug will print debug logs if true.
	EnableDebug = false

	// RequestToken is the token generator for the request middleware.
	RequestToken Token = &genericToken{}
)
View Source
var (
	// RequestHeaderKey is the key used when adding the header token.
	RequestHeaderKey = "___slog_request_token___"

	// RequestFieldKey is the key used in the Field output.
	RequestFieldKey = "reqID"

	// ResponseHeaderKey will send the token on the response as well (if set).
	ResponseHeaderKey = ""
)
View Source
var DiscardWrapper = &noSyncWrapper{ioutil.Discard}

DiscardWrapper is used for testing.

Functions

func AddGlobalFields added in v1.0.6

func AddGlobalFields(fields ...Field)

AddGlobalFields allows you to set fields that will automatically be appended to all messages.

func Debug

func Debug(message string, fields ...Field)

Debug outputs a debug message. If `EnabledDebug` is false, this turns into a noop.

func Error

func Error(message string, fields ...Field)

Error outputs an error message.

func Fatal

func Fatal(message string, fields ...Field)

Fatal outputs a fatal message and forces the application to exit with return code 1.

func Info

func Info(message string, fields ...Field)

Info outputs an info message.

func Panic

func Panic(message string, fields ...Field)

Panic outputs a panic message and also calls `panic` with the original message.

func Requestify

func Requestify(next http.Handler) http.Handler

Requestify adds a unique key to the request (header) and uses it for logging.

func SetTraceErrSeverity added in v1.1.0

func SetTraceErrSeverity(s string)

SetTraceErrSeverity allows you to change the severity type for trace errors (default is "error").

func TraceErr added in v1.0.9

func TraceErr(err error, fields ...Field) error

TraceErr outputs the error with it's trace as an error log line, but also returns the original error.

func Warning

func Warning(message string, fields ...Field)

Warning outputs a warning message.

Types

type Field

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

func Bool

func Bool(key string, val bool) Field

func Duration

func Duration(key string, val time.Duration) Field

func Err

func Err(err error) Field

func Float64

func Float64(key string, val float64) Field

func Int

func Int(key string, val int) Field

func Int64

func Int64(key string, val int64) Field

func JsonString added in v1.0.5

func JsonString(key string, val string) Field

func Jsonify added in v1.0.8

func Jsonify(key string, val interface{}) Field

func NullableString

func NullableString(key string, val string) Field

func Raw added in v1.0.4

func Raw(key string, val interface{}) Field

func RawJSON added in v1.2.0

func RawJSON(key string, val []byte) Field

func Request

func Request(r *http.Request) Field

func Skip

func Skip() Field

func String

func String(key string, val string) Field

func Time

func Time(key string, val time.Time) Field

func Uint

func Uint(key string, val uint) Field

func Uint64

func Uint64(key string, val uint64) Field

func Uintptr

func Uintptr(key string, val uintptr) Field

type LockedSyslogWriteSyncer

type LockedSyslogWriteSyncer struct {
	sync.Mutex
	// contains filtered or unexported fields
}

LockedSyslogWriteSyncer logs to syslog as well as stdout.

func (*LockedSyslogWriteSyncer) Sync

func (l *LockedSyslogWriteSyncer) Sync() error

func (*LockedSyslogWriteSyncer) Write

func (l *LockedSyslogWriteSyncer) Write(bs []byte) (int, error)

type LogFunc

type LogFunc func(message string, fields ...Field)

LogFunc is the generic interface that the level funcs conform with.

type Token

type Token interface {
	Generate() string
}

Token is an interface for generating tokens for the middleware.

type WriteSyncer

type WriteSyncer interface {
	io.Writer
	Sync() error
}

A WriteSyncer is an io.Writer that can also flush any buffered data. Note that *os.File (and thus, os.Stderr and os.Stdout) implement WriteSyncer.

func NewLockedSyslogWriteSyncer

func NewLockedSyslogWriteSyncer(network, address, tag string) WriteSyncer

NewLockedSyslogWriteSyncer creates a new write syncer for syslog.

func NewLockedWriteSyncer

func NewLockedWriteSyncer(ws WriteSyncer) WriteSyncer

Jump to

Keyboard shortcuts

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