oms-log-analytics-firehose-.../vendor/code.cloudfoundry.org/lager
Liz Zhang 166e1e4761 Use lager as logger 2016-12-23 16:49:03 +08:00
..
LICENSE Use lager as logger 2016-12-23 16:49:03 +08:00
NOTICE Use lager as logger 2016-12-23 16:49:03 +08:00
README.md Use lager as logger 2016-12-23 16:49:03 +08:00
logger.go Use lager as logger 2016-12-23 16:49:03 +08:00
models.go Use lager as logger 2016-12-23 16:49:03 +08:00
reconfigurable_sink.go Use lager as logger 2016-12-23 16:49:03 +08:00
writer_sink.go Use lager as logger 2016-12-23 16:49:03 +08:00

README.md

lager

Note: This repository should be imported as code.cloudfoundry.org/lager.

Lager is a logging library for go.

Usage

Instantiate a logger with the name of your component.

import (
  "code.cloudfoundry.org/lager"
)

logger := lager.NewLogger("my-app")

Sinks

Lager can write logs to a variety of destinations. You can specify the destinations using Lager sinks:

To write to an arbitrary Writer object:

logger.RegisterSink(lager.NewWriterSink(myWriter, lager.INFO))

Emitting logs

Lager supports the usual level-based logging, with an optional argument for arbitrary key-value data.

logger.Info("doing-stuff", lager.Data{
  "informative": true,
})

output:

{ "source": "my-app", "message": "doing-stuff", "data": { "informative": true }, "timestamp": 1232345, "log_level": 1 }

Error messages also take an Error object:

logger.Error("failed-to-do-stuff", errors.New("Something went wrong"))

output:

{ "source": "my-app", "message": "failed-to-do-stuff", "data": { "error": "Something went wrong" }, "timestamp": 1232345, "log_level": 1 }

Sessions

You can avoid repetition of contextual data using 'Sessions':


contextualLogger := logger.Session("my-task", lager.Data{
  "request-id": 5,
})

contextualLogger.Info("my-action")

output:

{ "source": "my-app", "message": "my-task.my-action", "data": { "request-id": 5 }, "timestamp": 1232345, "log_level": 1 }

License

Lager is Apache 2.0 licensed.