flogging

package
v1.3.0-rc1 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2018 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CONSOLE = iota
	JSON
)

Variables

This section is empty.

Functions

func DefaultLevel

func DefaultLevel() string

DefaultLevel returns the fallback value for loggers to use if parsing fails.

func GetModuleLevel

func GetModuleLevel(module string) string

GetModuleLevel gets the current logging level for the specified module.

func GetModuleLevels added in v1.3.0

func GetModuleLevels() map[string]zapcore.Level

GetModuleLevels takes a snapshot of the global module level information and returns it as a map. The map can then be used to restore logging levels to values in the snapshot.

func Init added in v1.3.0

func Init(config Config)

Init initializes logging with the provided config.

func InitBackend

func InitBackend(formatter logging.Formatter, output io.Writer)

InitBackend sets up the logging backend based on the provided logging formatter and I/O writer.

func InitFromSpec

func InitFromSpec(spec string) string

InitFromSpec initializes the logging based on the supplied spec. It is exposed externally so that consumers of the flogging package may parse their own logging specification. The logging specification has the following form:

[<module>[,<module>...]=]<level>[:[<module>[,<module>...]=]<level>...]

func IsValidLevel added in v1.3.0

func IsValidLevel(level string) bool

func NameToLevel added in v1.3.0

func NameToLevel(level string) zapcore.Level

NameToLevel converts a level name to a zapcore.Level. If the level name is unknown, zapcore.InfoLevel is returned.

func NewGRPCLogger added in v1.3.0

func NewGRPCLogger(l *zap.Logger) *zapgrpc.Logger

NewGRPCLogger creates a grpc.Logger that delegates to a zap.Logger.

func NewZapLogger added in v1.3.0

func NewZapLogger(core zapcore.Core, options ...zap.Option) *zap.Logger

NewZapLogger creates a zap logger around a new zap.Core. The core will use the provided encoder and sinks and a level enabler that is associated with the provided module name. The logger that is returned will be named the same as the module.

func Reset

func Reset()

Reset sets logging to the defaults defined in this package.

Used in tests and in the package init

func RestoreLevels added in v1.3.0

func RestoreLevels(levels map[string]zapcore.Level)

RestoreLevels sets the global module level information to the contents of the provided map.

func SetFormat

func SetFormat(formatSpec string) logging.Formatter

SetFormat sets the logging format.

func SetModuleLevel

func SetModuleLevel(module string, level string) error

SetModuleLevel sets the logging level for a single module.

func SetModuleLevels added in v1.3.0

func SetModuleLevels(moduleRegexp, level string) error

SetModuleLevels sets the logging level for the modules that match the supplied regular expression. Can be used to dynamically change the log level for the module.

Types

type Config added in v1.3.0

type Config struct {
	// Format is the log record format specifier for the Logging instance. If the
	// spec is the string "json", log records will be formatted as JSON. Any
	// other string will be provided to the FormatEncoder. Please see
	// fabenc.ParseFormat for details on the supported verbs.
	//
	// If Format is not provided, a default format that provides basic information will
	// be used.
	Format string

	// LogSpec determines the log levels that are enabled for the logging system. The
	// spec must be in a format that can be processed by ActivateSpec.
	//
	// If LogSpec is not provided, modules will be enabled at the INFO level.
	LogSpec string

	// Writer is the sink for encoded and formatted log records.
	//
	// If a Writer is not provided, os.Stderr will be used as the log sink.
	Writer io.Writer
}

Config is used to provide dependencies to a Logging instance.

type Core added in v1.3.0

type Core struct {
	zapcore.LevelEnabler
	Encoders map[Encoding]zapcore.Encoder
	Selector EncodingSelector
	Output   zapcore.WriteSyncer
}

Core is a custom implementation of a zapcore.Core. It's a terrible hack that only exists to work around the intersection of state associated with encoders, implementation hiding in zapcore, and implicit, ad-hoc logger initialization within fabric.

In addition to encoding log entries and fields to a buffer, zap Encoder implementations also need to maintain field state. When zapcore.Core.With is used, the associated encoder is cloned and the fields are added to the encoder. This means that encoder instances cannot be shared across cores.

In terms of implementation hiding, it's difficult for our FormatEncoder to cleanly wrap the JSON and console implementations from zap as all methods from the zapcore.ObjectEncoder would need to be implemented to delegate to the correct backend.

Finally, fabric logging can go through initialization multiple times. The first is via package initialization where hard-coded defaults are used for configuration. The second init is typically after any process configuration has been processed. This configuration can come from environment variables, command line flags, or config documents.

Since logging can be initialized multiple times, we need to be able to handle modification of the type of encoders we are using. While, for legacy reasons, a console based encoder is the default, a JSON logger is more likely to be desired in production.

This implementation works by associating multiple encoders with a core. When fields are added to the core, the fields are added to all of the encoder implementations. The core also references the logging configuration to determine the proper encoding to use, the writer to delegate to, and the enabled levels.

func (*Core) Check added in v1.3.0

func (*Core) Sync added in v1.3.0

func (c *Core) Sync() error

func (*Core) With added in v1.3.0

func (c *Core) With(fields []zapcore.Field) zapcore.Core

func (*Core) Write added in v1.3.0

func (c *Core) Write(e zapcore.Entry, fields []zapcore.Field) error

type Encoding added in v1.3.0

type Encoding int8

type EncodingSelector added in v1.3.0

type EncodingSelector interface {
	Encoding() Encoding
}

EncodingSelector is used to determine whether log records are encoded as JSON or in a human readable CONSOLE format.

type FabricLogger added in v1.3.0

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

A FabricLogger is an adapter around a zap.SugaredLogger that provides structured logging capabilities while preserving much of the legacy logging behavior.

The most significant difference between the FabricLogger and the zap.SugaredLogger is that methods without a formatting suffix (f or w) build the log entry message with fmt.Sprintln instead of fmt.Sprint. Without this change, arguments are not separated by spaces.

func MustGetLogger

func MustGetLogger(module string) *FabricLogger

MustGetLogger is used in place of `logging.MustGetLogger` to allow us to store a map of all modules and submodules that have loggers in the logging.

func NewFabricLogger added in v1.3.0

func NewFabricLogger(l *zap.Logger, options ...zap.Option) *FabricLogger

NewFabricLogger creates a logger that delegates to the zap.SugaredLogger.

func (*FabricLogger) Critical added in v1.3.0

func (f *FabricLogger) Critical(args ...interface{})

for backwards compatibility

func (*FabricLogger) Criticalf added in v1.3.0

func (f *FabricLogger) Criticalf(template string, args ...interface{})

func (*FabricLogger) DPanic added in v1.3.0

func (f *FabricLogger) DPanic(args ...interface{})

func (*FabricLogger) DPanicf added in v1.3.0

func (f *FabricLogger) DPanicf(template string, args ...interface{})

func (*FabricLogger) DPanicw added in v1.3.0

func (f *FabricLogger) DPanicw(msg string, kvPairs ...interface{})

func (*FabricLogger) Debug added in v1.3.0

func (f *FabricLogger) Debug(args ...interface{})

func (*FabricLogger) Debugf added in v1.3.0

func (f *FabricLogger) Debugf(template string, args ...interface{})

func (*FabricLogger) Debugw added in v1.3.0

func (f *FabricLogger) Debugw(msg string, kvPairs ...interface{})

func (*FabricLogger) Error added in v1.3.0

func (f *FabricLogger) Error(args ...interface{})

func (*FabricLogger) Errorf added in v1.3.0

func (f *FabricLogger) Errorf(template string, args ...interface{})

func (*FabricLogger) Errorw added in v1.3.0

func (f *FabricLogger) Errorw(msg string, kvPairs ...interface{})

func (*FabricLogger) Fatal added in v1.3.0

func (f *FabricLogger) Fatal(args ...interface{})

func (*FabricLogger) Fatalf added in v1.3.0

func (f *FabricLogger) Fatalf(template string, args ...interface{})

func (*FabricLogger) Fatalw added in v1.3.0

func (f *FabricLogger) Fatalw(msg string, kvPairs ...interface{})

func (*FabricLogger) Info added in v1.3.0

func (f *FabricLogger) Info(args ...interface{})

func (*FabricLogger) Infof added in v1.3.0

func (f *FabricLogger) Infof(template string, args ...interface{})

func (*FabricLogger) Infow added in v1.3.0

func (f *FabricLogger) Infow(msg string, kvPairs ...interface{})

func (*FabricLogger) IsEnabledFor added in v1.3.0

func (f *FabricLogger) IsEnabledFor(level zapcore.Level) bool

func (*FabricLogger) Named added in v1.3.0

func (f *FabricLogger) Named(name string) *FabricLogger

func (*FabricLogger) Notice added in v1.3.0

func (f *FabricLogger) Notice(args ...interface{})

func (*FabricLogger) Noticef added in v1.3.0

func (f *FabricLogger) Noticef(template string, args ...interface{})

func (*FabricLogger) Panic added in v1.3.0

func (f *FabricLogger) Panic(args ...interface{})

func (*FabricLogger) Panicf added in v1.3.0

func (f *FabricLogger) Panicf(template string, args ...interface{})

func (*FabricLogger) Panicw added in v1.3.0

func (f *FabricLogger) Panicw(msg string, kvPairs ...interface{})

func (*FabricLogger) Sync added in v1.3.0

func (f *FabricLogger) Sync() error

func (*FabricLogger) Warn added in v1.3.0

func (f *FabricLogger) Warn(args ...interface{})

func (*FabricLogger) Warnf added in v1.3.0

func (f *FabricLogger) Warnf(template string, args ...interface{})

func (*FabricLogger) Warning added in v1.3.0

func (f *FabricLogger) Warning(args ...interface{})

func (*FabricLogger) Warningf added in v1.3.0

func (f *FabricLogger) Warningf(template string, args ...interface{})

func (*FabricLogger) Warnw added in v1.3.0

func (f *FabricLogger) Warnw(msg string, kvPairs ...interface{})

func (*FabricLogger) With added in v1.3.0

func (f *FabricLogger) With(args ...interface{}) *FabricLogger

func (*FabricLogger) WithOptions added in v1.3.0

func (f *FabricLogger) WithOptions(opts ...zap.Option) *FabricLogger

type Logging added in v1.3.0

type Logging struct {
	*ModuleLevels
	// contains filtered or unexported fields
}

Logging maintains the state associated with the fabric logging system. It is intended to bridge between the legacy logging infrastructure built around go-logging and the structured, level logging provided by zap.

var Global *Logging

func New added in v1.3.0

func New(c Config) (*Logging, error)

New creates a new logging system and initializes it with the provided configuration.

func (*Logging) Apply added in v1.3.0

func (s *Logging) Apply(c Config) error

Apply applies the provided configuration to the logging system.

func (*Logging) Encoding added in v1.3.0

func (s *Logging) Encoding() Encoding

Encoding satisfies the Encoding interface. It determines whether the JSON or CONSOLE encoder should be used by the Core when log records are written.

func (*Logging) Logger added in v1.3.0

func (s *Logging) Logger(module string) *FabricLogger

Logger instantiates a new FabricLogger for the specified module. The module becomes the name of the logger and is used to determine which log levels are enabled.

func (*Logging) SetFormat added in v1.3.0

func (s *Logging) SetFormat(format string) error

SetFormat updates how log records are formatted and encoded. Log entries created after this method has completed will use the new format.

An error is returned if the log format specification cannot be parsed.

func (*Logging) SetWriter added in v1.3.0

func (s *Logging) SetWriter(w io.Writer)

SetWriter controls which writer formatted log records are written to. Writers, with the exception of an *os.File, need to be safe for concurrent use by multiple go routines.

func (*Logging) Sync added in v1.3.0

func (s *Logging) Sync() error

Sync satisfies the zapcore.WriteSyncer interface. It is used by the Core to flush log records before terminating the process.

func (*Logging) Write added in v1.3.0

func (s *Logging) Write(b []byte) (int, error)

Write satisfies the io.Write contract. It delegates to the writer argument of SetWriter or the Writer field of Config. The Core uses this when encoding log records.

func (*Logging) ZapLogger added in v1.3.0

func (s *Logging) ZapLogger(module string) *zap.Logger

ZapLogger instantiates a new zap.Logger for the specified module. The module becomes the name of the logger and is used to determine which log levels are enabled.

type ModuleLevels added in v1.3.0

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

ModuleLevels tracks the logging level of logging modules.

func (*ModuleLevels) ActivateSpec added in v1.3.0

func (m *ModuleLevels) ActivateSpec(spec string) error

ActivateSpec is used to modify module logging levels.

The logging specification has the following form:

[<module>[,<module>...]=]<level>[:[<module>[,<module>...]=]<level>...]

func (*ModuleLevels) DefaultLevel added in v1.3.0

func (m *ModuleLevels) DefaultLevel() zapcore.Level

DefaultLevel returns the default logging level for modules that do not have an explicit level set.

func (*ModuleLevels) Level added in v1.3.0

func (m *ModuleLevels) Level(module string) zapcore.Level

Level returns the effective logging level for a module. If a level has not been explicitly set for the module, the default logging level will be returned.

func (*ModuleLevels) LevelEnabler added in v1.3.0

func (m *ModuleLevels) LevelEnabler(module string) zapcore.LevelEnabler

LevelEnabler adapts ModuleLevels for use with zap as a zapcore.LevelEnabler.

func (*ModuleLevels) Levels added in v1.3.0

func (m *ModuleLevels) Levels() map[string]zapcore.Level

Levels returns a copy of current log levels.

func (*ModuleLevels) ResetLevels added in v1.3.0

func (m *ModuleLevels) ResetLevels()

ResetLevels discards level information about all modules and restores the default logging level to zapcore.InfoLevel.

func (*ModuleLevels) RestoreLevels added in v1.3.0

func (m *ModuleLevels) RestoreLevels(levels map[string]zapcore.Level)

RestoreLevels replaces the log levels with values previously acquired from Levels.

func (*ModuleLevels) SetDefaultLevel added in v1.3.0

func (m *ModuleLevels) SetDefaultLevel(l zapcore.Level)

SetDefaultLevel sets the default logging level for modules that do not have an explicit level set.

func (*ModuleLevels) SetLevel added in v1.3.0

func (m *ModuleLevels) SetLevel(module string, l zapcore.Level)

SetLevel sets the logging level for a single logging module.

func (*ModuleLevels) SetLevels added in v1.3.0

func (m *ModuleLevels) SetLevels(re *regexp.Regexp, l zapcore.Level)

SetLevels sets the logging level for all logging modules that match the provided regular expression.

Directories

Path Synopsis
Code generated by counterfeiter.
Code generated by counterfeiter.

Jump to

Keyboard shortcuts

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