go-ethereum/vendor/github.com/ipfs/go-log/README.md
2018-04-21 13:24:21 +02:00

2.9 KiB

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