log

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2023 License: MIT Imports: 16 Imported by: 1

README

Log

Go Reference

Simple logger for Go.

log

Features

  • Pretty console handler for terminals
  • Adds a level filter handler
  • json and logfmt handlers
  • Adds a multi-logger

Install

go get github.com/livebud/log

Example

log := log.Multi(
  log.Filter(log.LevelInfo, log.Console(color.Ignore(), os.Stderr)),
  log.Json(os.Stderr),
)
log.Debug("world", "args", 10)
log.Info("hello", "planet", "world", "args", 10)
log.Warn("hello", "planet", "world", "args", 10)
log.Error("hello world", "planet", "world", "args", 10)

Contributors

License

MIT

Documentation

Overview

Package log is inspired by apex/log.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// Debug message is written to the console
	Debug = stderr.Debug
	// Debugf message is written to the console
	Debugf = stderr.Debugf
	// Info message is written to the console
	Info = stderr.Info
	// Infof message is written to the console
	Infof = stderr.Infof
	// Notice message is written to the console
	Notice = stderr.Notice
	// Noticef message is written to the console
	Noticef = stderr.Noticef
	// Warn message is written to the console
	Warn = stderr.Warn
	// Warnf message is written to the console
	Warnf = stderr.Warnf
	// Error message is written to the console
	Error = stderr.Error
	// Errorf message is written to the console
	Errorf = stderr.Errorf
)
View Source
var ErrNotInContext = fmt.Errorf("log: not in context")

ErrNotInContext is returned when a log is not in the context

View Source
var Now = time.Now

Now returns the current time.

Functions

func Buffer added in v0.0.3

func Buffer() *buffer

func Middleware added in v0.0.7

func Middleware(log Log, next http.Handler, options ...func(*middlewareOption)) http.Handler

Middleware uses the logger to log requests and responses

func WithRequestId added in v0.0.7

func WithRequestId(fn func(r *http.Request) string) func(*middlewareOption)

WithRequestId sets the request id function for generating a unique request id for each request

Types

type Entry added in v0.0.3

type Entry struct {
	Time    time.Time `json:"ts,omitempty"`
	Level   Level     `json:"lvl,omitempty"`
	Message string    `json:"msg,omitempty"`
	Fields  Fields    `json:"fields,omitempty"`
}

func (*Entry) MarshalJSON added in v0.0.3

func (e *Entry) MarshalJSON() ([]byte, error)

type Fields added in v0.0.3

type Fields map[string]interface{}

func (Fields) Get added in v0.0.3

func (f Fields) Get(key string) interface{}

func (Fields) Keys added in v0.0.3

func (f Fields) Keys() []string

type Handler

type Handler interface {
	Log(log *Entry) error
}

func Console

func Console(color color.Writer, writer io.Writer) Handler

Console handler

Example
package main

import (
	"os"

	"github.com/livebud/color"
	"github.com/livebud/log"
)

func main() {
	log := log.New(log.Console(color.Ignore(), os.Stdout))
	log.Debug("world", "args", 10)
	log.Info("hello", "planet", "world", "args", 10)
	log.Warn("hello", "planet", "world", "args", 10)
	// log.Error("hello world", slog.String("planet", "world"), "args", 10)
	
Output:

func Filter

func Filter(level Level, handler Handler) Handler

Filter logs by level

func Json added in v0.0.3

func Json(w io.Writer) Handler

type Level

type Level uint8

Level of the logger

const (
	LevelDebug Level = iota + 1
	LevelInfo
	LevelNotice
	LevelWarn
	LevelError
)

Log level

func ParseLevel

func ParseLevel(level string) (Level, error)

func (Level) String added in v0.0.3

func (level Level) String() string

type Log added in v0.0.3

type Log interface {
	Field(key string, value interface{}) Log
	Fields(fields map[string]interface{}) Log
	Debug(args ...interface{}) error
	Debugf(msg string, args ...interface{}) error
	Info(args ...interface{}) error
	Infof(msg string, args ...interface{}) error
	Notice(args ...interface{}) error
	Noticef(msg string, args ...interface{}) error
	Warn(args ...interface{}) error
	Warnf(msg string, args ...interface{}) error
	Error(args ...interface{}) error
	Errorf(msg string, args ...interface{}) error
}

func Discard added in v0.0.3

func Discard() Log

Discard logger is a logger that discards all messages.

func From added in v0.0.6

func From(ctx context.Context) (Log, error)

From gets the log from the context. If the logger isn't in the middleware, we warn and discards the logs

func Multi

func Multi(handlers ...Handler) Log

Multi logs to multiple places

Example
package main

import (
	"os"

	"github.com/livebud/color"
	"github.com/livebud/log"
)

func main() {
	log := log.Multi(
		log.Filter(log.LevelDebug, log.Console(color.Default(), os.Stderr)),
		log.Json(os.Stderr),
	)
	log.Debug("world", "args", 10)
	log.Field("planet", "world").Field("args", 10).Info("hello")
	log.Warnf("hello %s", "world")
	log.Error("hello world", "planet", "world", "args", 10)
}
Output:

func MustFrom added in v0.0.7

func MustFrom(ctx context.Context) Log

MustFrom gets the log from the context or panics

type Logger

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

func Debugger added in v0.0.3

func Debugger() *Logger

Debugger log

func Default

func Default() *Logger

Default log

func New

func New(handler Handler) *Logger

New logger

func Parse added in v0.0.3

func Parse(filter string) (*Logger, error)

Parse the logger with a given filter

func (*Logger) Debug added in v0.0.3

func (l *Logger) Debug(args ...interface{}) error

func (*Logger) Debugf added in v0.0.3

func (l *Logger) Debugf(msg string, args ...interface{}) error

func (*Logger) Error added in v0.0.3

func (l *Logger) Error(args ...interface{}) error

func (*Logger) Errorf added in v0.0.3

func (l *Logger) Errorf(msg string, args ...interface{}) error

func (*Logger) Field added in v0.0.3

func (l *Logger) Field(key string, value interface{}) Log

func (*Logger) Fields added in v0.0.3

func (l *Logger) Fields(fields map[string]interface{}) Log

func (*Logger) Info added in v0.0.3

func (l *Logger) Info(args ...interface{}) error

func (*Logger) Infof added in v0.0.3

func (l *Logger) Infof(msg string, args ...interface{}) error

func (*Logger) Notice added in v0.0.3

func (l *Logger) Notice(args ...interface{}) error

func (*Logger) Noticef added in v0.0.3

func (l *Logger) Noticef(msg string, args ...interface{}) error

func (*Logger) Warn added in v0.0.3

func (l *Logger) Warn(args ...interface{}) error

func (*Logger) Warnf added in v0.0.3

func (l *Logger) Warnf(msg string, args ...interface{}) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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