log

package module
v2.1.2-alpha.6 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2020 License: MIT Imports: 17 Imported by: 0

README

go-log

Fork of ipfs/go-log used by BDWare projects.

standard-readme compliant go.dev reference Build Status

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/bdware/go-log

Add the following line to your go.mod:

replace github.com/ipfs/go-log/v2 => github.com/bdware/go-log/v2 {{VERSION}}

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:

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

Contribute

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

License

MIT

Copyright for portions of this fork are held by [Juan Batiz-Benet, 2014] as part of the original go-log project.

All other copyright for this fork are held by [The BDWare Authors, 2020].

All rights reserved.

Documentation

Overview

Package log is the logging library used by IPFS & libp2p (https://github.com/ipfs/go-ipfs).

Index

Constants

This section is empty.

Variables

View Source
var (
	LevelDebug  = LogLevel(zapcore.DebugLevel)
	LevelInfo   = LogLevel(zapcore.InfoLevel)
	LevelWarn   = LogLevel(zapcore.WarnLevel)
	LevelError  = LogLevel(zapcore.ErrorLevel)
	LevelDPanic = LogLevel(zapcore.DPanicLevel)
	LevelPanic  = LogLevel(zapcore.PanicLevel)
	LevelFatal  = LogLevel(zapcore.FatalLevel)
)
View Source
var ErrNoSuchLogger = errors.New("Error: No such logger")

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

Functions

func CompactColorLevelEncoder

func CompactColorLevelEncoder(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder)

CompactColorLevelEncoder serializes a Level to a compact string and adds coloring. For example, InfoLevel is serialized to "I" and colored blue.

func CompactLevelEncoder

func CompactLevelEncoder(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder)

CompactLevelEncoder serializes a Level to a compact string. For example, InfoLevel is serialized to "I".

func FormatRFC3339

func FormatRFC3339(t time.Time) string

FormatRFC3339 returns the given time in UTC with RFC3999Nano format.

func GetSubsystems

func GetSubsystems() []string

GetSubsystems returns a slice containing the names of the current loggers

func NewCompactEncoder

func NewCompactEncoder(cfg zapcore.EncoderConfig) zapcore.Encoder

NewCompactEncoder creates an encoder whose output is designed for human - rather than machine - consumption. It serializes the core log entry data (message, level, timestamp, etc.) in a compact plain-text format and leaves the structured context as JSON.

Note that although the compact encoder doesn't use the keys specified in the encoder configuration, it will omit any element whose key is set to the empty string.

func SetAllLoggers

func SetAllLoggers(lvl LogLevel)

SetAllLoggers changes the logging level of all loggers to lvl

func SetDebugLogging

func SetDebugLogging()

SetDebugLogging calls SetAllLoggers with logging.DEBUG

func SetLogLevel

func SetLogLevel(name, level string) error

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

func SetLogLevelRegex

func SetLogLevelRegex(e, l string) error

SetLogLevelRegex sets all loggers to level `l` that match expression `e`. An error is returned if `e` fails to compile.

func SetupLogging

func SetupLogging(cfg Config)

SetupLogging will initialize the logger backend and set the flags. TODO calling this in `init` pushes all configuration to env variables - move it out of `init`? then we need to change all the code (js-ipfs, go-ipfs) to call this explicitly - have it look for a config file? need to define what that is

Types

type Config

type Config struct {
	// Format overrides the format of the log output. Defaults to ColorizedOutput
	Format LogFormat

	// Level is the minimum enabled logging level.
	Level LogLevel

	// Stderr indicates whether logs should be written to stderr.
	Stderr bool

	// Stdout indicates whether logs should be written to stdout.
	Stdout bool

	// File is a path to a file that logs will be written to.
	File string

	// URL with schema supported by zap. Use zap.RegisterSink
	URL string

	// AutoStd indicates whether to force enabling stderr and stdout if:
	// (File is not set or not correct) and (URL is not set).
	AutoStd bool

	// Log sampling config.
	Sampling *zap.SamplingConfig

	// Lumberjack config, disabled if nil. Lumberjack.Filename will be ignored and set to File.
	Lumberjack *lumberjack.Logger
}

type EventLogger

type EventLogger interface {
	StandardLogger
}

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

type LogFormat

type LogFormat int
const (
	ColorizedOutput LogFormat = iota
	PlaintextOutput
	JSONOutput
	ColorizedCompactOutput
	CompactOutput
)

type LogLevel

type LogLevel zapcore.Level

LogLevel represents a log severity level. Use the package variables as an enum.

func LevelFromString

func LevelFromString(level string) (LogLevel, error)

LevelFromString parses a string-based level and returns the corresponding LogLevel.

Supported strings are: DEBUG, INFO, WARN, ERROR, DPANIC, PANIC, FATAL, and their lower-case forms.

The returned LogLevel must be discarded if error is not nil.

type PipeReader

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

A PipeReader is a reader that reads from the logger. It is synchronous so blocking on read will affect logging performance.

func NewPipeReader

func NewPipeReader(opts ...PipeReaderOption) *PipeReader

NewPipeReader creates a new in-memory reader that reads from all loggers The caller must call Close on the returned reader when done.

By default, it:

  1. Logs JSON. This can be changed by passing the PipeFormat option.
  2. Logs everything that would otherwise be logged to the "primary" log output. That is, everything enabled by SetLogLevel. The minimum log level can be increased by passing the PipeLevel option.

func (*PipeReader) Close

func (p *PipeReader) Close() error

Close unregisters the reader from the logger.

func (*PipeReader) Read

func (p *PipeReader) Read(data []byte) (int, error)

Read implements the standard Read interface

type PipeReaderOption

type PipeReaderOption interface {
	// contains filtered or unexported methods
}

func PipeFormat

func PipeFormat(format LogFormat) PipeReaderOption

PipeFormat sets the output format of the pipe reader

func PipeLevel

func PipeLevel(level LogLevel) PipeReaderOption

PipeLevel sets the log level of logs sent to the pipe reader.

type StandardLogger

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{})
	Warn(args ...interface{})
	Warnf(format string, args ...interface{})
}

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

type ZapEventLogger

type ZapEventLogger struct {
	zap.SugaredLogger
	// contains filtered or unexported fields
}

ZapEventLogger implements the EventLogger and wraps a go-logging Logger

func Logger

func Logger(system string) *ZapEventLogger

Logger retrieves an event logger by name

func (*ZapEventLogger) Warning

func (logger *ZapEventLogger) Warning(args ...interface{})

Warning is for compatibility Deprecated: use Warn(args ...interface{}) instead

func (*ZapEventLogger) Warningf

func (logger *ZapEventLogger) Warningf(format string, args ...interface{})

Warningf is for compatibility Deprecated: use Warnf(format string, args ...interface{}) instead

Directories

Path Synopsis
zap_private
_internal/bufferpool
https://github.com/uber-go/zap/blob/cada3b3e434a735ead0d5e818f6e31d7d94af517/internal/bufferpool/bufferpool.go Package bufferpool houses zap's shared internal buffer pool.
https://github.com/uber-go/zap/blob/cada3b3e434a735ead0d5e818f6e31d7d94af517/internal/bufferpool/bufferpool.go Package bufferpool houses zap's shared internal buffer pool.
_internal/color
https://github.com/uber-go/zap/blob/cada3b3e434a735ead0d5e818f6e31d7d94af517/internal/color/color.go Package color adds coloring functionality for TTY output.
https://github.com/uber-go/zap/blob/cada3b3e434a735ead0d5e818f6e31d7d94af517/internal/color/color.go Package color adds coloring functionality for TTY output.
zapcore
https://github.com/uber-go/zap/blob/cada3b3e434a735ead0d5e818f6e31d7d94af517/zapcore/field.go https://github.com/uber-go/zap/blob/cada3b3e434a735ead0d5e818f6e31d7d94af517/zapcore/json_encoder.go https://github.com/uber-go/zap/blob/cada3b3e434a735ead0d5e818f6e31d7d94af517/zapcore/level_strings.go
https://github.com/uber-go/zap/blob/cada3b3e434a735ead0d5e818f6e31d7d94af517/zapcore/field.go https://github.com/uber-go/zap/blob/cada3b3e434a735ead0d5e818f6e31d7d94af517/zapcore/json_encoder.go https://github.com/uber-go/zap/blob/cada3b3e434a735ead0d5e818f6e31d7d94af517/zapcore/level_strings.go

Jump to

Keyboard shortcuts

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