log

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2020 License: MIT Imports: 12 Imported by: 0

README

boldlygo/log - a simple leveled, structured log

boldlygo/log provides a simple leveled, structured log.

Example

package main

import (
	"context"
	"net"
	"net/http"
	"os"

	"gitlab.com/boldlygo/log"
)

type Handler struct {
	log.Logger
}

func (h Handler) handle(w http.ResponseWriter, r *http.Request) {
	// Log with the fields in r.Context() and level debug.
	h.Log(r.Context(), log.LevelDebug)
}

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// Include the request URI in any log entries.
	ctx := log.Context(r.Context(), log.NewField("uri", r.RequestURI))
	r = r.WithContext(ctx)
	h.handle(w, r)
}

func main() {
	logger := log.New(os.Stderr)

	svr := &http.Server{
		Addr: ":http",
		Handler: Handler{
			Logger: logger,
		},
		ErrorLog:    logger.Logger,
		BaseContext: func(net.Listener) context.Context {
			return log.Context(context.Background(), log.NewField("name", "value"))
		},
	}

	svr.ListenAndServe()
}

Documentation

Overview

Package log defines the Logger interface and a basic implementation.

Index

Constants

View Source
const (
	FieldKeyError   = "error"
	FieldKeyLevel   = "level"
	FieldKeyMessage = "msg"
	FieldKeyTime    = "time"
)

Common field keys.

Variables

This section is empty.

Functions

func NewContext added in v0.1.3

func NewContext(ctx context.Context, field ...Field) context.Context

NewContext returns a copy of ctx in which the provided Fields, if any, are added to the set of Fields stored in ctx.

Types

type Field

type Field interface {
	Key() string
	Get() interface{}
}

Field is the interface implemented by types that store structured data for logging.

func FromContext

func FromContext(ctx context.Context) []Field

FromContext returns the fields stored in ctx.

func Wrap added in v0.1.3

func Wrap(key string, value interface{}) Field

Wrap returns a Field for the provided key, value pair.

type Formatter added in v0.2.0

type Formatter interface {
	Format(entry) ([]byte, error)
}

Formatter is the interface implemented by types that can format a Log entry.

type JSONFormat added in v0.2.0

type JSONFormat struct{}

JSONFormat formats log entries as JSON.

func (JSONFormat) Format added in v0.2.0

func (JSONFormat) Format(e entry) ([]byte, error)

Format implements the Formatter interface.

type Level

type Level int8

Level is the log severity level.

const (
	LevelTrace Level = iota - 2
	LevelDebug
	LevelInfo
	LevelWarning
	LevelError
	LevelFatal // Logger implementations may call os.Exit(1).
	LevelPanic // Logger implementations may panic.
)

Common log severity levels.

func ParseLevel added in v0.1.3

func ParseLevel(s string) (Level, error)

ParseLevel parses s into a Level.

func (Level) Get added in v0.1.3

func (l Level) Get() interface{}

Get implements the Field and flag.Getter interfaces.

func (Level) Key added in v0.1.3

func (Level) Key() string

Key implements the Field interface.

func (Level) MarshalText

func (l Level) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*Level) Set added in v0.1.3

func (l *Level) Set(s string) error

Set implements the flag.Getter interface.

func (Level) String

func (l Level) String() string

func (*Level) UnmarshalText added in v0.1.3

func (l *Level) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Log

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

Log is a basic leveled, structured log.

func New

func New(w io.Writer, lvl Level, f Formatter) Log

New returns a new Log that writes to w.

func (Log) Log

func (l Log) Log(ctx context.Context, fields ...Field)

Log writes a new log entry with any fields stored in ctx and the given field(s).

func (Log) StdLog added in v0.2.0

func (l Log) StdLog(ctx context.Context, fields ...Field) *log.Logger

StdLog returns a Go standard library Logger that writes to the io.Writer from l.Writer(ctx, fields...).

func (Log) Writer added in v0.2.0

func (l Log) Writer(ctx context.Context, fields ...Field) io.Writer

Writer returns a Writer that logs on each call to Write.

type Logger

type Logger interface {
	Log(context.Context, ...Field)
}

Logger is the interface implemented by types that can write to a log.

type Msg

type Msg string

Msg is a log message.

func Msgf added in v0.2.0

func Msgf(format string, a ...interface{}) Msg

Msgf formats according to a format specifier and returns the resulting Msg.

func (Msg) Get added in v0.1.3

func (m Msg) Get() interface{}

Get implements the Field interface.

func (Msg) Key added in v0.1.3

func (Msg) Key() string

Key implements the Field interface.

type TextFormat added in v0.2.0

type TextFormat struct{}

TextFormat formats log entries as plain text.

func (TextFormat) Format added in v0.2.0

func (TextFormat) Format(e entry) ([]byte, error)

Format implements the Formatter interface.

Jump to

Keyboard shortcuts

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