log

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2023 License: MIT Imports: 12 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 Now = time.Now

Now returns the current time.

Functions

This section is empty.

Types

type Buffered added in v0.0.5

type Buffered struct {
	Entries []*Entry
	// contains filtered or unexported fields
}

func Buffer added in v0.0.3

func Buffer() *Buffered

func (*Buffered) Debug added in v0.0.5

func (b *Buffered) Debug(args ...interface{}) error

func (*Buffered) Debugf added in v0.0.5

func (b *Buffered) Debugf(msg string, args ...interface{}) error

func (*Buffered) Error added in v0.0.5

func (b *Buffered) Error(args ...interface{}) error

func (*Buffered) Errorf added in v0.0.5

func (b *Buffered) Errorf(msg string, args ...interface{}) error

func (*Buffered) Field added in v0.0.5

func (b *Buffered) Field(key string, value interface{}) Log

func (*Buffered) Fields added in v0.0.5

func (b *Buffered) Fields(fields map[string]interface{}) Log

func (*Buffered) Info added in v0.0.5

func (b *Buffered) Info(args ...interface{}) error

func (*Buffered) Infof added in v0.0.5

func (b *Buffered) Infof(msg string, args ...interface{}) error

func (*Buffered) Log added in v0.0.5

func (h *Buffered) Log(entry *Entry) error

func (*Buffered) Notice added in v0.0.5

func (b *Buffered) Notice(args ...interface{}) error

func (*Buffered) Noticef added in v0.0.5

func (b *Buffered) Noticef(msg string, args ...interface{}) error

func (*Buffered) Warn added in v0.0.5

func (b *Buffered) Warn(args ...interface{}) error

func (*Buffered) Warnf added in v0.0.5

func (b *Buffered) Warnf(msg string, args ...interface{}) error

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)
	

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 Debugger added in v0.0.3

func Debugger() Log

Debugger log

func Default

func Default() Log

Default log

func Discard added in v0.0.3

func Discard() Log

Discard logger is a logger that discards all messages.

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)
}

func Parse added in v0.0.3

func Parse(filter string) (Log, error)

Parse the logger with a given filter

type Logger

type Logger struct {
	Handler Handler
}

func New

func New(handler Handler) *Logger

New logger

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