Help us shape the future of Go! Take the 2026 Go Developer Survey

log

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2018 License: MIT Imports: 13 Imported by: 0

README

go-log

standard-readme compliant GoDoc Build Status

The logging library used by go-ipfs

It currently uses a modified version of go-logging to implement the standard printf-style log output.

Install

go get github.com/ipfs/go-log

Usage

Once the pacakge is imported under the name logging, an instance of EventLogger can be created like so:

var log = logging.Logger("subsystem name")

It can then be used to emit log messages, either plain printf-style messages at six standard levels or structured messages using Event, EventBegin and EventBeginInContext methods.

Examples

Event

log.Event(ctx, "event name", logging.LoggableMap{"metaKey": metaValue})

EventBegin

In a method with named returns

func (s *Session) GetBlock(ctx context.Context, c *cid.Cid) (blk blocks.Block, err error) {
  eip := log.EventBegin(ctx, "Session.GetBlock", c)
  defer func() {
    if err != nil {
      eip.SetError(err)
    }
    eip.Done()
  }()
  ...
}

As a one liner

defer log.EventBegin(ctx, "bootstrapDial", ph.ID(), p.ID).Done()

EventBeginInContext

When an event spans more than one function call Start and event in the context

func (s *blockService) GetBlocks(ctx context.Context, ks []*cid.Cid) <-chan blocks.Block {
  ctx = log.EventBeginInContext(ctx, "BlockService.GetBlocks")
  return getBlocks(ctx, ks, s.blockstore, s.exchange)
}

Finish the event later

func getBlocks(ctx context.Context, ks []*cid.Cid, bs blockstore.Blockstore, f exchange.Fetcher) <-chan blocks.Block {
  ...
  go func() {
    defer logging.MaybeFinishEvent(ctx)
    ...
    select {
    case out <- hit:
    case <-ctx.Done():
      return
    }
  }

Contribute

Feel free to join in. All welcome. Open an issue!

This repository falls under the IPFS Code of Conduct.

Want to hack on IPFS?

License

MIT

Documentation

Overview

Package log is the logging library used by IPFS (https://github.com/ipfs/go-ipfs). It uses a modified version of https://godoc.org/github.com/whyrusleeping/go-logging .

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNoSuchLogger = errors.New("Error: No such logger")

ErrNoSuchLogger is returned when the util pkg is asked for a non existant logger

View Source
var LdJSONFormatter = func() {
	logging.SetFormatter(&PoliteJSONFormatter{})
}

LdJSONFormatter Option formats the event log as line-delimited JSON

View Source
var LevelDebug = func() {
	logging.SetLevel(logging.DEBUG, "")
}

LevelDebug Option sets the log level to debug

View Source
var LevelError = func() {
	logging.SetLevel(logging.ERROR, "")
}

LevelError Option sets the log level to error

View Source
var LevelInfo = func() {
	logging.SetLevel(logging.INFO, "")
}

LevelInfo Option sets the log level to info

View Source
var LogFormats = map[string]string{
	"nocolor": "%{time:2006-01-02 15:04:05.000000} %{level} %{module} %{shortfile}: %{message}",
	"color": ansiGray + "%{time:15:04:05.000} %{color}%{level:5.5s} " + ansiBlue +
		"%{module:10.10s}: %{color:reset}%{message} " + ansiGray + "%{shortfile}%{color:reset}",
}

LogFormats defines formats for logging (i.e. "color")

View Source
var MaxWriterBuffer = 512 * 1024

MaxWriterBuffer specifies how big the writer buffer can get before killing the writer.

View Source
var TextFormatter = func() {
	logging.SetFormatter(logging.DefaultFormatter)
}

TextFormatter Option formats the event log as human-readable plain-text

View Source
var WriterGroup = NewMirrorWriter()

WriterGroup is the global writer group for logs to output to

Functions

func Configure added in v1.1.0

func Configure(options ...Option)

Configure applies the provided options sequentially from left to right

func ContextWithLoggable added in v1.1.0

func ContextWithLoggable(ctx context.Context, l Loggable) context.Context

ContextWithLoggable returns a derived context which contains the provided Loggable. Any Events logged with the derived context will include the provided Loggable.

func FormatRFC3339 added in v1.1.0

func FormatRFC3339(t time.Time) string

FormatRFC3339 returns the given time in UTC with RFC3999Nano format.

func GetSubsystems added in v1.1.0

func GetSubsystems() []string

GetSubsystems returns a slice containing the names of the current loggers

func MaybeFinishEvent added in v1.3.0

func MaybeFinishEvent(ctx context.Context)

Will complete an event if there is one in ctx

func SetAllLoggers added in v1.1.0

func SetAllLoggers(lvl logging.Level)

SetAllLoggers changes the logging.Level of all loggers to lvl

func SetDebugLogging added in v1.1.0

func SetDebugLogging()

SetDebugLogging calls SetAllLoggers with logging.DEBUG

func SetLogLevel added in v1.1.0

func SetLogLevel(name, level string) error

SetLogLevel changes the log level of a specific subsystem name=="*" changes all subsystems

func SetupLogging added in v1.1.0

func SetupLogging()

SetupLogging will initialize the logger backend and set the flags.

Types

type EventInProgress added in v1.1.0

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

EventInProgress represent and event which is happening

func (*EventInProgress) Append added in v1.1.0

func (eip *EventInProgress) Append(l Loggable)

Append adds loggables to be included in the call to Done

func (*EventInProgress) Close added in v1.1.0

func (eip *EventInProgress) Close() error

Close is an alias for done

func (*EventInProgress) Done added in v1.1.0

func (eip *EventInProgress) Done()

Done creates a new Event entry that includes the duration and appended loggables.

func (*EventInProgress) DoneWithErr added in v1.4.0

func (eip *EventInProgress) DoneWithErr(err error)

DoneWithErr creates a new Event entry that includes the duration and appended loggables. DoneWithErr accepts an error, if err is non-nil, it is set on the EventInProgress. Otherwise the logic is the same as the `Done()` method

func (*EventInProgress) SetError added in v1.1.0

func (eip *EventInProgress) SetError(err error)

SetError includes the provided error

type EventLogger added in v1.1.0

type EventLogger interface {
	StandardLogger

	// Event merges structured data from the provided inputs into a single
	// machine-readable log event.
	//
	// If the context contains metadata, a copy of this is used as the base
	// metadata accumulator.
	//
	// If one or more loggable objects are provided, these are deep-merged into base blob.
	//
	// Next, the event name is added to the blob under the key "event". If
	// the key "event" already exists, it will be over-written.
	//
	// Finally the timestamp and package name are added to the accumulator and
	// the metadata is logged.
	Event(ctx context.Context, event string, m ...Loggable)

	EventBegin(ctx context.Context, event string, m ...Loggable) *EventInProgress

	EventBeginInContext(ctx context.Context, event string, m ...Loggable) context.Context
}

EventLogger extends the StandardLogger interface to allow for log items containing structured metadata

Example
{
	log := EventLogger(nil)
	e := log.EventBegin(context.Background(), "dial")
	e.Done()
}
{
	log := EventLogger(nil)
	e := log.EventBegin(context.Background(), "dial")
	_ = e.Close() // implements io.Closer for convenience
}

func Logger added in v1.1.0

func Logger(system string) EventLogger

Logger retrieves an event logger by name

type Loggable added in v1.1.0

type Loggable interface {
	Loggable() map[string]interface{}
}

Loggable describes objects that can be marshalled into Metadata for logging

func Deferred added in v1.1.0

func Deferred(key string, f func() string) Loggable

Deferred returns a LoggableF where the execution of the provided function is deferred.

func Pair added in v1.1.0

func Pair(key string, l Loggable) Loggable

Pair returns a Loggable where key is paired to Loggable.

type LoggableF added in v1.1.0

type LoggableF func() map[string]interface{}

LoggableF converts a func into a Loggable

func (LoggableF) Loggable added in v1.1.0

func (l LoggableF) Loggable() map[string]interface{}

Loggable implements the Loggable interface by running the LoggableF function.

type LoggableMap added in v1.1.0

type LoggableMap map[string]interface{}

LoggableMap is just a generic map keyed by string. It implements the Loggable interface.

func (LoggableMap) Loggable added in v1.1.0

func (l LoggableMap) Loggable() map[string]interface{}

Loggable implements the Loggable interface for LoggableMap

type Metadata added in v1.1.0

type Metadata map[string]interface{}

Metadata is a convenience type for generic maps

func DeepMerge added in v1.1.0

func DeepMerge(b, a Metadata) Metadata

DeepMerge merges the second Metadata parameter into the first. Nested Metadata are merged recursively. Primitives are over-written.

func MetadataFromContext added in v1.1.0

func MetadataFromContext(ctx context.Context) (Metadata, error)

MetadataFromContext extracts Matadata from a given context's value.

func Metadatify added in v1.1.0

func Metadatify(i interface{}) (Metadata, error)

Metadatify converts maps into Metadata.

func (Metadata) JsonString added in v1.1.0

func (m Metadata) JsonString() (string, error)

JsonString returns the marshaled JSON string for the metadata.

func (Metadata) Loggable added in v1.1.0

func (m Metadata) Loggable() map[string]interface{}

Loggable implements the Loggable interface.

type MirrorWriter added in v1.1.0

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

MirrorWriter implements a WriteCloser which syncs incoming bytes to multiple [buffered] WriteClosers. They can be added with AddWriter().

func NewMirrorWriter added in v1.1.0

func NewMirrorWriter() *MirrorWriter

NewMirrorWriter initializes and returns a MirrorWriter.

func (*MirrorWriter) Active added in v1.1.0

func (mw *MirrorWriter) Active() (active bool)

Active returns if there is at least one Writer attached to this MirrorWriter

func (*MirrorWriter) AddWriter added in v1.1.0

func (mw *MirrorWriter) AddWriter(w io.WriteCloser)

AddWriter attaches a new WriteCloser to this MirrorWriter. The new writer will start getting any bytes written to the mirror.

func (*MirrorWriter) Close added in v1.1.0

func (mw *MirrorWriter) Close() error

Close closes the MirrorWriter

func (*MirrorWriter) Write added in v1.1.0

func (mw *MirrorWriter) Write(b []byte) (int, error)

Write broadcasts the written bytes to all Writers.

type Option added in v1.1.0

type Option func()

Option is a generic function

func Output added in v1.1.0

func Output(w io.Writer) Option

Output returns an option which sets the the given writer as the new logging backend

type PoliteJSONFormatter added in v1.1.0

type PoliteJSONFormatter struct{}

PoliteJSONFormatter marshals entries into JSON encoded slices (without overwriting user-provided keys). How polite of it!

func (*PoliteJSONFormatter) Format added in v1.1.0

func (f *PoliteJSONFormatter) Format(calldepth int, r *logging.Record, w io.Writer) error

Format encodes a logging.Record in JSON and writes it to Writer.

type StandardLogger added in v1.1.0

type StandardLogger interface {
	Debug(args ...interface{})
	Debugf(format string, args ...interface{})
	Error(args ...interface{})
	Errorf(format string, args ...interface{})
	Fatal(args ...interface{})
	Fatalf(format string, args ...interface{})
	Info(args ...interface{})
	Infof(format string, args ...interface{})
	Panic(args ...interface{})
	Panicf(format string, args ...interface{})
	Warning(args ...interface{})
	Warningf(format string, args ...interface{})
}

StandardLogger provides API compatibility with standard printf loggers eg. go-logging

Jump to

Keyboard shortcuts

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