logger

package
v0.6.4-rc3 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2020 License: ISC Imports: 12 Imported by: 9

Documentation

Overview

Package logger defines an interface and default implementation for subsystem logging.

Log level verbosity may be modified at runtime for each individual subsystem logger.

The default implementation in this package must be created by the Backend type. Backends can write to any io.Writer, including multi-writers created by io.MultiWriter. Multi-writers allow log output to be written to many writers, including standard output and log files.

Optional logging behavior can be specified by using the LOGFLAGS environment variable and overridden per-Backend by using the WithFlags call option. Multiple LOGFLAGS options can be specified, separated by commas. The following options are recognized:

longfile: Include the full filepath and line number in all log messages

shortfile: Include the filename and line number in all log messages.
Overrides longfile.

Index

Constants

View Source
const (
	// Llongfile modifies the logger output to include full path and line number
	// of the logging callsite, e.g. /a/b/c/main.go:123.
	Llongfile uint32 = 1 << iota

	// Lshortfile modifies the logger output to include filename and line number
	// of the logging callsite, e.g. main.go:123. Overrides Llongfile.
	Lshortfile
)

Flags to modify Backend's behavior.

Variables

View Source
var (
	// BackendLog is the logging backend used to create all subsystem loggers.
	BackendLog = NewBackend()
)

Loggers per subsystem. A single backend logger is created and all subsytem loggers created from it will write to the backend. When adding new subsystems, add the subsystem logger variable here and to the subsystemLoggers map.

Loggers can not be used before the log rotator has been initialized with a log file. This must be performed early during application startup by calling InitLog.

View Source
var SubsystemTags = struct {
	ADXR,
	AMGR,
	CMGR,
	KSDB,
	KASD,
	BDAG,
	CNFG,
	DISC,
	INDX,
	MINR,
	PEER,
	RPCS,
	SCRP,
	SRVR,
	SYNC,
	TXMP,
	UTIL,
	PROF,
	PROT,
	MUXX,
	GRPC,
	P2PS,
	NTAR,
	DNSS,
	SNVR,
	IBDS string
}{
	ADXR: "ADXR",
	AMGR: "AMGR",
	CMGR: "CMGR",
	KSDB: "KSDB",
	KASD: "KASD",
	BDAG: "BDAG",
	CNFG: "CNFG",
	DISC: "DISC",
	INDX: "INDX",
	MINR: "MINR",
	PEER: "PEER",
	RPCS: "RPCS",
	SCRP: "SCRP",
	SRVR: "SRVR",
	SYNC: "SYNC",
	TXMP: "TXMP",
	UTIL: "UTIL",
	PROF: "PROF",
	PROT: "PROT",
	MUXX: "MUXX",
	GRPC: "GRPC",
	P2PS: "P2PS",
	NTAR: "NTAR",
	DNSS: "DNSS",
	SNVR: "SNVR",
	IBDS: "IBDS",
}

SubsystemTags is an enum of all sub system tags

Functions

func DirectionString

func DirectionString(inbound bool) string

DirectionString is a helper function that returns a string that represents the direction of a connection (inbound or outbound).

func InitLog

func InitLog(logFile, errLogFile string)

InitLog attaches log file and error log file to the backend log.

func ParseAndSetDebugLevels

func ParseAndSetDebugLevels(debugLevel string) error

ParseAndSetDebugLevels attempts to parse the specified debug level and set the levels accordingly. An appropriate error is returned if anything is invalid.

func PickNoun

func PickNoun(n uint64, singular, plural string) string

PickNoun returns the singular or plural form of a noun depending on the count n.

func SetLogLevel

func SetLogLevel(subsystemID string, logLevel string)

SetLogLevel sets the logging level for provided subsystem. Invalid subsystems are ignored. Uninitialized subsystems are dynamically created as needed.

func SetLogLevels

func SetLogLevels(logLevel string)

SetLogLevels sets the log level for all subsystem loggers to the passed level. It also dynamically creates the subsystem loggers as needed, so it can be used to initialize the logging system.

func SupportedSubsystems

func SupportedSubsystems() []string

SupportedSubsystems returns a sorted slice of the supported subsystems for logging purposes.

Types

type Backend

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

Backend is a logging backend. Subsystems created from the backend write to the backend's Writer. Backend provides atomic writes to the Writer from all subsystems.

func NewBackend

func NewBackend(opts ...BackendOption) *Backend

NewBackend creates a new logger backend.

func (*Backend) AddLogFile

func (b *Backend) AddLogFile(logFile string, logLevel Level) error

AddLogFile adds a file which the log will write into on a certain log level with the default log rotation settings. It'll create the file if it doesn't exist.

func (*Backend) AddLogFileWithCustomRotator

func (b *Backend) AddLogFileWithCustomRotator(logFile string, logLevel Level, thresholdKB int64, maxRolls int) error

AddLogFileWithCustomRotator adds a file which the log will write into on a certain log level, with the specified log rotation settings. It'll create the file if it doesn't exist.

func (*Backend) Close

func (b *Backend) Close()

Close finalizes all log rotators for this backend

func (*Backend) Logger

func (b *Backend) Logger(subsystemTag string) *Logger

Logger returns a new logger for a particular subsystem that writes to the Backend b. A tag describes the subsystem and is included in all log messages. The logger uses the info verbosity level by default.

type BackendOption

type BackendOption func(b *Backend)

BackendOption is a function used to modify the behavior of a Backend.

func WithFlags

func WithFlags(flags uint32) BackendOption

WithFlags configures a Backend to use the specified flags rather than using the package's defaults as determined through the LOGFLAGS environment variable.

type Level

type Level uint32

Level is the level at which a logger is configured. All messages sent to a level which is below the current level are filtered.

const (
	LevelTrace Level = iota
	LevelDebug
	LevelInfo
	LevelWarn
	LevelError
	LevelCritical
	LevelOff
)

Level constants.

func LevelFromString

func LevelFromString(s string) (l Level, ok bool)

LevelFromString returns a level based on the input string s. If the input can't be interpreted as a valid log level, the info level and false is returned.

func (Level) String

func (l Level) String() string

String returns the tag of the logger used in log messages, or "OFF" if the level will not produce any log output.

type LogClosure

type LogClosure func() string

LogClosure is a closure that can be printed with %s to be used to generate expensive-to-create data for a detailed log level and avoid doing the work if the data isn't printed.

func NewLogClosure

func NewLogClosure(c func() string) LogClosure

NewLogClosure casts a function to a LogClosure. See LogClosure for details.

func (LogClosure) String

func (c LogClosure) String() string

type Logger

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

Logger is a subsystem logger for a Backend.

func Get

func Get(tag string) (logger *Logger, ok bool)

Get returns a logger of a specific sub system

func (*Logger) Backend

func (l *Logger) Backend() *Backend

Backend returns the log backend

func (*Logger) Critical

func (l *Logger) Critical(args ...interface{})

Critical formats message using the default formats for its operands, prepends the prefix as necessary, and writes to log with LevelCritical.

func (*Logger) Criticalf

func (l *Logger) Criticalf(format string, args ...interface{})

Criticalf formats message according to format specifier, prepends the prefix as necessary, and writes to log with LevelCritical.

func (*Logger) Debug

func (l *Logger) Debug(args ...interface{})

Debug formats message using the default formats for its operands, prepends the prefix as necessary, and writes to log with LevelDebug.

func (*Logger) Debugf

func (l *Logger) Debugf(format string, args ...interface{})

Debugf formats message according to format specifier, prepends the prefix as necessary, and writes to log with LevelDebug.

func (*Logger) Error

func (l *Logger) Error(args ...interface{})

Error formats message using the default formats for its operands, prepends the prefix as necessary, and writes to log with LevelError.

func (*Logger) Errorf

func (l *Logger) Errorf(format string, args ...interface{})

Errorf formats message according to format specifier, prepends the prefix as necessary, and writes to log with LevelError.

func (*Logger) Info

func (l *Logger) Info(args ...interface{})

Info formats message using the default formats for its operands, prepends the prefix as necessary, and writes to log with LevelInfo.

func (*Logger) Infof

func (l *Logger) Infof(format string, args ...interface{})

Infof formats message according to format specifier, prepends the prefix as necessary, and writes to log with LevelInfo.

func (*Logger) Level

func (l *Logger) Level() Level

Level returns the current logging level

func (*Logger) SetLevel

func (l *Logger) SetLevel(level Level)

SetLevel changes the logging level to the passed level.

func (*Logger) Trace

func (l *Logger) Trace(args ...interface{})

Trace formats message using the default formats for its operands, prepends the prefix as necessary, and writes to log with LevelTrace.

func (*Logger) Tracef

func (l *Logger) Tracef(format string, args ...interface{})

Tracef formats message according to format specifier, prepends the prefix as necessary, and writes to log with LevelTrace.

func (*Logger) Warn

func (l *Logger) Warn(args ...interface{})

Warn formats message using the default formats for its operands, prepends the prefix as necessary, and writes to log with LevelWarn.

func (*Logger) Warnf

func (l *Logger) Warnf(format string, args ...interface{})

Warnf formats message according to format specifier, prepends the prefix as necessary, and writes to log with LevelWarn.

func (*Logger) Write

func (l *Logger) Write(logLevel Level, args ...interface{})

Write formats message using the default formats for its operands, prepends the prefix as necessary, and writes to log with the given logLevel.

func (*Logger) Writef

func (l *Logger) Writef(logLevel Level, format string, args ...interface{})

Writef formats message according to format specifier, prepends the prefix as necessary, and writes to log with the given logLevel.

Jump to

Keyboard shortcuts

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