logged

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2024 License: MIT Imports: 7 Imported by: 2

README

logged

Go Report Card Build Status Coverage Status GoDoc GitHub release GitHub license

A fast logger for Go.

Overview

Install with:

go get github.com/msales/logged

Examples

// Composable handlers
h := logged.LevelFilterHandler(
    logged.Info,
    logged.StreamHandler(os.Stdout, logged.LogfmtFormat()),
)

// The logger can have an initial context
l := logged.New(h, "env", "prod")

// All messages can have a context
l.Warn("connection error", "redis", conn.Name(), "timeout", conn.Timeout())

Will log the message

lvl=warn msg="connection error" redis=dsn_1 timeout=0.500

License

MIT-License. As is. No warranties whatsoever. Mileage may vary. Batteries not included.

Documentation

Index

Constants

View Source
const (
	//LevelKey is the key used for message levels.
	LevelKey = "lvl"
	// MessageKey is the key used for message descriptions.
	MessageKey = "msg"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type FilterFunc

type FilterFunc func(msg string, lvl Level, ctx []interface{}) bool

FilterFunc represents a function that can filter messages.

type Format added in v0.3.0

type Format int

Format represents the predefined log format.

const (
	JSON Format = iota
	Logfmt
)

List of predefined log Formats

func (Format) String added in v0.3.0

func (f Format) String() string

String returns the string representation of the level.

type Formatter

type Formatter interface {
	// Format formats a log message.
	Format(msg string, lvl Level, ctx []interface{}) []byte
}

Formatter represents a log message formatter.

func FormatterFromString added in v0.3.0

func FormatterFromString(format string) (Formatter, error)

FormatterFromString returns a formatter instance appropriate for the given format name.

func JSONFormat

func JSONFormat() Formatter

JSONFormat formats a log line in json format.

func LogfmtFormat

func LogfmtFormat() Formatter

LogfmtFormat formats a log line in logfmt format.

type FormatterFunc

type FormatterFunc func(msg string, lvl Level, ctx []interface{}) []byte

FormatterFunc is a function formatter.

func (FormatterFunc) Format

func (f FormatterFunc) Format(msg string, lvl Level, ctx []interface{}) []byte

Format formats a log message.

type Handler

type Handler interface {
	// Log write the log message.
	Log(msg string, lvl Level, ctx []interface{})
}

Handler represents a log handler.

func BufferedStreamHandler

func BufferedStreamHandler(w io.Writer, flushBytes int, flushInterval time.Duration, fmtr Formatter) Handler

BufferedStreamHandler writes buffered log messages to an io.Writer with the given format.

func DiscardHandler

func DiscardHandler() Handler

DiscardHandler does nothing, discarding all log messages.

func FilterHandler

func FilterHandler(fn FilterFunc, h Handler) Handler

FilterHandler returns a handler that only writes messages to the wrapped handler if the given function evaluates true.

func LevelFilterHandler

func LevelFilterHandler(maxLvl Level, h Handler) Handler

LevelFilterHandler returns a handler that

func StreamHandler

func StreamHandler(w io.Writer, fmtr Formatter) Handler

StreamHandler writes log messages to an io.Writer with the given format.

type HandlerFunc

type HandlerFunc func(msg string, lvl Level, ctx []interface{})

HandlerFunc is a function handler.

func (HandlerFunc) Log

func (h HandlerFunc) Log(msg string, lvl Level, ctx []interface{})

Log write the log message.

type Level

type Level int

Level represents the predefined log level.

const (
	Crit Level = iota
	Error
	Warn
	Info
	Debug
)

List of predefined log Levels

func LevelFromString

func LevelFromString(lvl string) (Level, error)

LevelFromString converts a string to Level.

func (Level) String

func (l Level) String() string

String returns the string representation of the level.

type Logger

type Logger interface {
	// Debug logs a debug message.
	Debug(msg string, ctx ...interface{})
	// Info logs an informational message.
	Info(msg string, ctx ...interface{})
	// Warn logs a warning message.
	Warn(msg string, ctx ...interface{})
	// Error logs an error message.
	Error(msg string, ctx ...interface{})
	// Crit logs a critical message.
	Crit(msg string, ctx ...interface{})

	// Close closes the logger.
	Close() error
}

Logger represents a log writer.

func New

func New(h Handler, ctx ...interface{}) Logger

New creates a new Logger.

Jump to

Keyboard shortcuts

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