Categorygithub.com/daotl/go-log/v2
modulepackage
2.3.1
Repository: https://github.com/daotl/go-log.git
Documentation: pkg.go.dev

# README

go-log

DAOT Labs' fork of ipfs/go-log.

Go Reference

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

Additional features of this fork

Added formats

  • CompactOutput: a compact format which looks like:
D[2021-05-13T17:49:52.413+0800]	example/log.go:52	for Debug
I[2021-05-13T17:49:52.413+0800]	example/log.go:52	for Info
W[2021-05-13T17:49:52.413+0800]	example/log.go:52	for Warn
E[2021-05-13T17:49:52.413+0800]	example/log.go:52	for Error
p[2021-05-13T17:49:52.413+0800]	example/log.go:52	for DPanic
P[2021-05-13T17:49:52.413+0800]	example/log.go:52	for Panic
F[2021-05-13T17:49:52.413+0800]	example/log.go:52	for Fatal
  • ColorizedCompactOutput: same as CompactOutput but colorized

Added config options

  • AutoColor: automatically switches between formats and their colorized counterparts (ColorizedOutput <-> PlaintextOutput, ColorizedCompactOutput <-> CompactOutput), if the current program is run from a terminal it switches to the colorized version, vice versa
  • AutoStdout: automatically enables stdout output if the current program is run from a terminal, or ((File is not set or not correct) and (URL is not set))
  • Sampling: configs log sampling
  • Lumberjack: configs log rolling using Lumberjack

Install

go get github.com/daotl/go-log/v2

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"

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.
  • compactcolor -- human readable, compact colorized (ANSI) output.
  • compactnocolor -- human readable, compact output.

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.

GOLOG_AUTO_COLOR

See AutoColor in Added config options.

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!

License

MIT

# Packages

Modified from: https://github.com/uber-go/zap/blob/7b21229fb3f063275f4f169f8a79ad30aa001c51/zapcore/console_encoder.go.
No description provided by the author

# Functions

FormatRFC3339 returns the given time in UTC with RFC3999Nano format.
GetSubsystems returns a slice containing the names of the current loggers.
LevelFromString parses a string-based level and returns the corresponding LogLevel.
Logger retrieves an event logger by name.
NewPipeReader creates a new in-memory reader that reads from all loggers The caller must call Close on the returned reader when done.
NopLogger returns a no-op Logger.
PipeFormat sets the output format of the pipe reader.
PipeLevel sets the log level of logs sent to the pipe reader.
SetAllLoggers changes the logging level of all loggers to lvl.
SetDebugLogging calls SetAllLoggers with logging.DEBUG.
SetLogLevel changes the log level of a specific subsystem name=="*" changes all subsystems.
SetLogLevelRegex sets all loggers to level `l` that match expression `e`.
SetPrimaryCore changes the primary logging core.
SetupLogging will initialize the logger backend and set the flags.
TestingLogger returns a Logger which writes to STDOUT if test(s) are being run with the verbose (-v) flag, NopLogger otherwise.
No description provided by the author

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

ErrNoSuchLogger is returned when the util pkg is asked for a non existant logger.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
A PipeReader is a reader that reads from the logger.
ZapEventLogger implements the EventLogger and wraps a go-logging Logger.

# Interfaces

EventLogger extends the StandardLogger interface to allow for log items containing structured metadata.
No description provided by the author
StandardLogger provides API compatibility with standard printf loggers eg.

# Type aliases

No description provided by the author
LogLevel represents a log severity level.