Code Monkey home page Code Monkey logo

go-log's Introduction

go-log

GoDoc

The logging library used by go-ipfs

go-log wraps zap to provide a logging facade. go-log manages logging instances and allows for their levels to be controlled individually.

Install

go get github.com/ipfs/go-log

Usage

Once the package 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 in plain printf-style messages at seven standard levels:

Levels may be set for all loggers:

lvl, err := logging.LevelFromString("error")
if err != nil {
	panic(err)
}
logging.SetAllLoggers(lvl)

or individually:

err := logging.SetLogLevel("net:pubsub", "info")
if err != nil {
	panic(err)
}

or by regular expression:

err := logging.SetLogLevelRegex("net:.*", "info")
if err != nil {
	panic(err)
}

Environment Variables

This package can be configured through various environment variables.

GOLOG_LOG_LEVEL

Specifies the log-level, both globally and on a per-subsystem basis.

For example, the following will set the global minimum log level to error, but reduce the minimum log level for subsystem1 to info and reduce the minimum log level for subsystem2 to debug.

export GOLOG_LOG_LEVEL="error,subsystem1=info,subsystem2=debug"

IPFS_LOGGING is a deprecated alias for this environment variable.

GOLOG_FILE

Specifies that logs should be written to the specified file. If this option is not specified, logs are written to standard error.

export GOLOG_FILE="/path/to/my/file.log"

GOLOG_OUTPUT

Specifies where logging output should be written. Can take one or more of the following values, combined with +:

  • stdout -- write logs to standard out.
  • stderr -- write logs to standard error.
  • file -- write logs to the file specified by GOLOG_FILE

For example, if you want to log to both a file and standard error:

export GOLOG_FILE="/path/to/my/file.log"
export GOLOG_OUTPUT="stderr+file"

Setting only GOLOG_FILE will prevent logs from being written to standard error.

GOLOG_LOG_FMT

Specifies the log message format. It supports the following values:

  • color -- human readable, colorized (ANSI) output
  • nocolor -- human readable, plain-text output.
  • json -- structured JSON.

For example, to log structured JSON (for easier parsing):

export GOLOG_LOG_FMT="json"

The logging format defaults to color when the output is a terminal, and nocolor otherwise.

IPFS_LOGGING_FMT is a deprecated alias for this environment variable.

GOLOG_LOG_LABELS

Specifies a set of labels that should be added to all log messages as comma-separated key-value pairs. For example, the following add {"app": "example_app", "dc": "sjc-1"} to every log entry.

export GOLOG_LOG_LABELS="app=example_app,dc=sjc-1"

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.