logger

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2021 License: MIT Imports: 7 Imported by: 34

README

Logo

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

logger is a fast Go logging package made to be simple but effective.

Overview

Install with:

go get github.com/hamba/logger/v2
Formatters
  • JSON
  • Logfmt
  • Console
Writers
  • SyncWriter Write synchronised to a Writer

Examples

log := logger.New(os.Stdout, logger.LogfmtFormat(), logger.Info)

// Logger can have scoped context
log = log.With(ctx.Str("env", "prod"))

// All messages can have a context
log.Warn("connection error", ctx.Str("redis", "dsn_1"), ctx.Int("timeout", conn.Timeout()))

Will log the message

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

More examples can be found in the godocs.

Documentation

Overview

Package logger implements a logging package.

Example usage:

log := logger.New(os.Stdout, logger.LogfmtFormat(), logger.Info)

// Logger can have scoped context
log = log.With(ctx.Str("env", "prod"))

// All messages can have a context
log.Error("connection error", ctx.Str("redis", conn.Name()), ctx.Int("timeout", conn.Timeout()))

Index

Examples

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 Event

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

Event is a log event.

func (*Event) AppendBool

func (e *Event) AppendBool(k string, b bool)

AppendBool appends a bool to the event.

func (*Event) AppendBytes

func (e *Event) AppendBytes(k string, p []byte)

AppendBytes appends bytes to the event.

func (*Event) AppendDuration

func (e *Event) AppendDuration(k string, d time.Duration)

AppendDuration appends a duration to the event.

func (*Event) AppendFloat

func (e *Event) AppendFloat(k string, f float64)

AppendFloat appends a float to the event.

func (*Event) AppendInt

func (e *Event) AppendInt(k string, i int64)

AppendInt appends an int to the event.

func (*Event) AppendInterface

func (e *Event) AppendInterface(k string, v interface{})

AppendInterface appends a interface to the event.

func (*Event) AppendInts

func (e *Event) AppendInts(k string, a []int)

AppendInts appends ints to the event.

func (*Event) AppendString

func (e *Event) AppendString(k, s string)

AppendString appends a string to the event.

func (*Event) AppendStrings

func (e *Event) AppendStrings(k string, s []string)

AppendStrings appends strings to the event.

func (*Event) AppendTime

func (e *Event) AppendTime(k string, d time.Time)

AppendTime appends a time to the event.

func (*Event) AppendUint

func (e *Event) AppendUint(k string, i uint64)

AppendUint appends a uint to the event.

type Field

type Field func(*Event)

Field is a context field.

type Formatter

type Formatter interface {
	WriteMessage(buf *bytes.Buffer, time int64, lvl Level, msg string)
	AppendEndMarker(buf *bytes.Buffer)
	AppendArrayStart(buf *bytes.Buffer)
	AppendArraySep(buf *bytes.Buffer)
	AppendArrayEnd(buf *bytes.Buffer)
	AppendKey(buf *bytes.Buffer, key string)
	AppendString(buf *bytes.Buffer, s string)
	AppendBool(buf *bytes.Buffer, b bool)
	AppendInt(buf *bytes.Buffer, i int64)
	AppendUint(buf *bytes.Buffer, i uint64)
	AppendFloat(buf *bytes.Buffer, f float64)
	AppendTime(buf *bytes.Buffer, t time.Time)
	AppendDuration(buf *bytes.Buffer, d time.Duration)
	AppendInterface(buf *bytes.Buffer, v interface{})
}

Formatter represents a log message formatter.

func ConsoleFormat

func ConsoleFormat() Formatter

ConsoleFormat formats a log line in a console format.

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 Level

type Level int

Level represents the predefined log level.

const (
	Disabled Level = iota
	Crit
	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 {
	// With returns a logger with context.
	With(ctx ...Field) Logger
	// Debug logs a debug message.
	Debug(msg string, ctx ...Field)
	// Info logs an informational message.
	Info(msg string, ctx ...Field)
	// Warn logs a warning message.
	Warn(msg string, ctx ...Field)
	// Error logs an error message.
	Error(msg string, ctx ...Field)
	// Crit logs a critical message.
	Crit(msg string, ctx ...Field)
}

Logger represents a log writer.

func New

func New(w io.Writer, fmtr Formatter, lvl Level) Logger

New creates a new Logger.

Example
log := logger.New(os.Stdout, logger.LogfmtFormat(), logger.Info).With(ctx.Str("env", "prod"))

log.Info("redis connection", ctx.Str("redis", "some redis name"), ctx.Int("timeout", 10))

type SyncWriter

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

SyncWriter implements a writer that is synchronised with a lock.

Example
log := logger.New(logger.NewSyncWriter(os.Stdout), logger.LogfmtFormat(), logger.Info).With(ctx.Str("env", "prod"))

log.Info("redis connection", ctx.Str("redis", "some redis name"), ctx.Int("timeout", 10))

func NewSyncWriter

func NewSyncWriter(w io.Writer) *SyncWriter

NewSyncWriter returns a synchronised writer.

func (*SyncWriter) Write

func (w *SyncWriter) Write(p []byte) (n int, err error)

Write writes to the writer.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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