log

package module
v0.0.0-...-fe3c858 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2017 License: MIT Imports: 4 Imported by: 9

README

log

Package log implements a simple structured logging API inspired by Logrus, designed with centralization in mind. Read more on Medium.

Badges

Build Status GoDoc


tjholowaychuk.com  ·  GitHub @tj  ·  Twitter @tjholowaychuk

Documentation

Overview

Package log implements a simple structured logging API designed with few assumptions. Designed for centralized logging solutions such as Kinesis which require encoding and decoding before fanning-out to handlers.

You may use this package with inline handlers, much like Logrus, however a centralized solution is recommended so that apps do not need to be re-deployed to add or remove logging service providers.

Example (Errors)

Errors are passed to WithError(), populating the "error" field.

package main

import (
	"errors"

	"github.com/francoishill/log"
)

func main() {
	err := errors.New("boom")
	log.WithError(err).Error("upload failed")
}
Output:

Example (MultipleFields)

Multiple fields can be set, via chaining, or WithFields().

package main

import (
	"github.com/francoishill/log"
)

func main() {
	log.WithFields(log.Fields{
		"user": "Tobi",
		"file": "sloth.png",
		"type": "image/png",
	}).Info("upload")
}
Output:

Example (Structured)

Structured logging is supported with fields, and is recommended over the formatted message variants.

package main

import (
	"github.com/francoishill/log"
)

func main() {
	log.WithField("user", "Tobo").Info("logged in")
}
Output:

Example (Trace)

Trace can be used to simplify logging of start and completion events, for example an upload which may fail.

package main

import (
	"github.com/francoishill/log"
)

func main() (err error) {
	defer log.Trace("upload").Stop(&err)
	return nil
}
Output:

Example (Unstructured)

Unstructured logging is supported, but not recommended since it is hard to query.

package main

import (
	"github.com/francoishill/log"
)

func main() {
	log.Infof("%s logged in", "Tobi")
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Alert

func Alert(msg string)

Alert level message, followed by an exit.

func Alertf

func Alertf(msg string, v ...interface{})

Alertf level formatted message, followed by an exit.

func Critical

func Critical(msg string)

Critical level message, followed by an exit.

func Criticalf

func Criticalf(msg string, v ...interface{})

Criticalf level formatted message, followed by an exit.

func Debug

func Debug(msg string)

Debug level message.

func Debugf

func Debugf(msg string, v ...interface{})

Debugf level formatted message.

func Emergency

func Emergency(msg string)

Emergency level message, followed by an exit.

func Emergencyf

func Emergencyf(msg string, v ...interface{})

Emergencyf level formatted message, followed by an exit.

func Error

func Error(msg string)

Error level message.

func Errorf

func Errorf(msg string, v ...interface{})

Errorf level formatted message.

func Info

func Info(msg string)

Info level message.

func Infof

func Infof(msg string, v ...interface{})

Infof level formatted message.

func Notice

func Notice(msg string)

Notice level message.

func Noticef

func Noticef(msg string, v ...interface{})

Noticef level formatted message.

func SetHandler

func SetHandler(h Handler)

SetHandler sets the handler. This is not thread-safe.

func SetLevel

func SetLevel(l Level)

SetLevel sets the log level. This is not thread-safe.

func Warn

func Warn(msg string)

Warn level message.

func Warnf

func Warnf(msg string, v ...interface{})

Warnf level formatted message.

Types

type Entry

type Entry struct {
	Logger    *Logger   `json:"-"`
	Fields    Fields    `json:"fields"`
	Level     Level     `json:"level"`
	Timestamp time.Time `json:"timestamp"`
	Message   string    `json:"message"`
	// contains filtered or unexported fields
}

Entry represents a single log entry.

func NewEntry

func NewEntry(log *Logger) *Entry

NewEntry returns a new entry for `log`.

func Trace

func Trace(msg string) *Entry

Trace returns a new entry with a Stop method to fire off a corresponding completion log, useful with defer.

func WithError

func WithError(err error) *Entry

WithError returns a new entry with the "error" set to `err`.

func WithField

func WithField(key string, value interface{}) *Entry

WithField returns a new entry with the `key` and `value` set.

func WithFields

func WithFields(fields Fielder) *Entry

WithFields returns a new entry with `fields` set.

func (*Entry) Alert

func (e *Entry) Alert(msg string)

Alert level message, followed by an exit.

func (*Entry) Alertf

func (e *Entry) Alertf(msg string, v ...interface{})

Alertf level formatted message, followed by an exit.

func (*Entry) Critical

func (e *Entry) Critical(msg string)

Critical level message, followed by an exit.

func (*Entry) Criticalf

func (e *Entry) Criticalf(msg string, v ...interface{})

Criticalf level formatted message, followed by an exit.

func (*Entry) Debug

func (e *Entry) Debug(msg string)

Debug level message.

func (*Entry) Debugf

func (e *Entry) Debugf(msg string, v ...interface{})

Debugf level formatted message.

func (*Entry) Emergency

func (e *Entry) Emergency(msg string)

Emergency level message, followed by an exit.

func (*Entry) Emergencyf

func (e *Entry) Emergencyf(msg string, v ...interface{})

Emergencyf level formatted message, followed by an exit.

func (*Entry) Error

func (e *Entry) Error(msg string)

Error level message.

func (*Entry) Errorf

func (e *Entry) Errorf(msg string, v ...interface{})

Errorf level formatted message.

func (*Entry) Info

func (e *Entry) Info(msg string)

Info level message.

func (*Entry) Infof

func (e *Entry) Infof(msg string, v ...interface{})

Infof level formatted message.

func (*Entry) Notice

func (e *Entry) Notice(msg string)

Notice level message.

func (*Entry) Noticef

func (e *Entry) Noticef(msg string, v ...interface{})

Noticef level formatted message.

func (*Entry) Stop

func (e *Entry) Stop(err *error)

Stop should be used with Trace, to fire off the completion message. When an `err` is passed the "error" field is set, and the log level is error.

func (*Entry) StopLevel

func (e *Entry) StopLevel(level Level, err *error)

StopLevel should be used with Trace, to fire off the completion message. When an `err` is passed the "error" field is set, and the log level is error.

func (*Entry) Trace

func (e *Entry) Trace(msg string) *Entry

Trace returns a new entry with a Stop method to fire off a corresponding completion log, useful with defer.

func (*Entry) TraceLevel

func (e *Entry) TraceLevel(level Level, msg string) *Entry

TraceLevel returns a new entry with a Stop method to fire off a corresponding completion log, useful with defer.

func (*Entry) Warn

func (e *Entry) Warn(msg string)

Warn level message.

func (*Entry) Warnf

func (e *Entry) Warnf(msg string, v ...interface{})

Warnf level formatted message.

func (*Entry) WithError

func (e *Entry) WithError(err error) *Entry

WithError returns a new entry with the "error" set to `err`.

func (*Entry) WithField

func (e *Entry) WithField(key string, value interface{}) *Entry

WithField returns a new entry with the `key` and `value` set.

func (*Entry) WithFields

func (e *Entry) WithFields(fields Fielder) *Entry

WithFields returns a new entry with `fields` set.

type Fielder

type Fielder interface {
	Fields() Fields
}

Fielder is an interface for providing fields to custom types.

type Fields

type Fields map[string]interface{}

Fields represents a map of entry level data used for structured logging.

func (Fields) Fields

func (f Fields) Fields() Fields

Fields implements Fielder.

type Handler

type Handler interface {
	HandleLog(*Entry) error
}

Handler is used to handle log events, outputting them to stdio or sending them to remote services. See the "handlers" directory for implementations.

It is left up to Handlers to implement thread-safety.

type HandlerFunc

type HandlerFunc func(*Entry) error

The HandlerFunc type is an adapter to allow the use of ordinary functions as log handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler object that calls f.

func (HandlerFunc) HandleLog

func (f HandlerFunc) HandleLog(e *Entry) error

HandleLog calls f(e).

type Interface

type Interface interface {
	WithFields(fields Fielder) *Entry
	WithField(key string, value interface{}) *Entry
	WithError(err error) *Entry

	Debug(msg string)
	Info(msg string)
	Notice(msg string)
	Warn(msg string)
	Error(msg string)
	Critical(msg string)
	Alert(msg string)
	Emergency(msg string)

	Debugf(msg string, v ...interface{})
	Infof(msg string, v ...interface{})
	Noticef(msg string, v ...interface{})
	Warnf(msg string, v ...interface{})
	Errorf(msg string, v ...interface{})
	Criticalf(msg string, v ...interface{})
	Alertf(msg string, v ...interface{})
	Emergencyf(msg string, v ...interface{})
	Trace(msg string) *Entry
}

Interface represents the API of both Logger and Entry as per RFC5424

var Log Interface = &Logger{
	Level: InfoLevel,
}

singletons ftw?

type Level

type Level int

Level of severity.

const (
	DebugLevel Level = iota
	InfoLevel
	NoticeLevel
	WarnLevel
	ErrorLevel
	CriticalLevel
	AlertLevel
	EmergencyLevel
)

Log levels.

func ParseLevel

func ParseLevel(s string) (Level, error)

ParseLevel parses level string.

func (Level) MarshalJSON

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

MarshalJSON returns the level string.

func (Level) String

func (l Level) String() string

String implements io.Stringer.

type Logger

type Logger struct {
	Handler Handler
	Level   Level
}

Logger represents a logger with configurable Level and Handler.

func (*Logger) Alert

func (l *Logger) Alert(msg string)

Alert level message, followed by an exit.

func (*Logger) Alertf

func (l *Logger) Alertf(msg string, v ...interface{})

Alertf level formatted message, followed by an exit.

func (*Logger) Critical

func (l *Logger) Critical(msg string)

Critical level message, followed by an exit.

func (*Logger) Criticalf

func (l *Logger) Criticalf(msg string, v ...interface{})

Criticalf level formatted message, followed by an exit.

func (*Logger) Debug

func (l *Logger) Debug(msg string)

Debug level message.

func (*Logger) Debugf

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

Debugf level formatted message.

func (*Logger) Emergency

func (l *Logger) Emergency(msg string)

Emergency level message, followed by an exit.

func (*Logger) Emergencyf

func (l *Logger) Emergencyf(msg string, v ...interface{})

Emergencyf level formatted message, followed by an exit.

func (*Logger) Error

func (l *Logger) Error(msg string)

Error level message.

func (*Logger) Errorf

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

Errorf level formatted message.

func (*Logger) Info

func (l *Logger) Info(msg string)

Info level message.

func (*Logger) Infof

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

Infof level formatted message.

func (*Logger) Notice

func (l *Logger) Notice(msg string)

Notice level message.

func (*Logger) Noticef

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

Noticef level formatted message.

func (*Logger) Trace

func (l *Logger) Trace(msg string) *Entry

Trace returns a new entry with a Stop method to fire off a corresponding completion log, useful with defer.

func (*Logger) Warn

func (l *Logger) Warn(msg string)

Warn level message.

func (*Logger) Warnf

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

Warnf level formatted message.

func (*Logger) WithError

func (l *Logger) WithError(err error) *Entry

WithError returns a new entry with the "error" set to `err`.

func (*Logger) WithField

func (l *Logger) WithField(key string, value interface{}) *Entry

WithField returns a new entry with the `key` and `value` set.

func (*Logger) WithFields

func (l *Logger) WithFields(fields Fielder) *Entry

WithFields returns a new entry with `fields` set.

Directories

Path Synopsis
_examples
cli
es
handlers
cli
Package cli implements a colored text handler suitable for command-line interfaces.
Package cli implements a colored text handler suitable for command-line interfaces.
discard
Package discard implements a no-op handler useful for benchmarks and tests.
Package discard implements a no-op handler useful for benchmarks and tests.
es
Package es implements an Elasticsearch batch handler.
Package es implements an Elasticsearch batch handler.
json
Package json implements a JSON handler.
Package json implements a JSON handler.
level
Package level implements a level filter handler.
Package level implements a level filter handler.
logfmt
Package logfmt implements a "logfmt" format handler.
Package logfmt implements a "logfmt" format handler.
memory
Package memory implements an in-memory handler useful for testing, as the entries can be accessed after writes.
Package memory implements an in-memory handler useful for testing, as the entries can be accessed after writes.
multi
Package multi implements a handler which invokes a number of handlers.
Package multi implements a handler which invokes a number of handlers.
text
Package text implements a development-friendly textual handler.
Package text implements a development-friendly textual handler.

Jump to

Keyboard shortcuts

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