logger

package
v0.0.0-...-61ccda9 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2022 License: AGPL-3.0 Imports: 7 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DebugPeriodicIntervals = false

DebugPeriodicIntervals enables logging of periodic-logger intervals. After each logger is run the time until next run of that periodic logger is printed, as well as the time until any other logger if non-zero.

Functions

func Escape

func Escape(b []byte) string

Escape escapes multi-line NMEA sentences for debug logging. It replaces CR, LF and NUL with \r, \n and \0, and is only slightly longer than string().

func RoundDuration

func RoundDuration(d, to time.Duration) string

RoundDuration removes excessive precission for printing.

func SiMultiple

func SiMultiple(n, multipleOf uint64, maxUnit byte) string

SiMultiple rounds n down to the nearest Kilo, Mega, Giga, ..., or Yotta, and append the letter. `multipleOf` can be 1000 or 1024 (or anything >=256 (=(2^64)^(1/8))). `maxUnit` prevents losing too much precission by using too big units.

Types

type Composer

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

Composer lets you split a long message into multiple write statements End the message by calling Finish() or Close()

func (*Composer) Close

func (c *Composer) Close()

Close releases the mutex on the logger and exits the process for `Fatal` errors.

func (*Composer) Finish

func (c *Composer) Finish(format string, args ...interface{})

Finish writes a formatted line and then closes the composer.

func (*Composer) Write

func (c *Composer) Write(format string, args ...interface{})

Write writes formatted text without a newline

func (*Composer) Writeln

func (c *Composer) Writeln(format string, args ...interface{})

Writeln writes a formatted string plus a newline. This is identical to what Logger.Log() does.

type Level

type Level uint8

Level is the importance of a logged event

const (
	Debug   Level = iota // passed through without prepending timestamp
	Fatal                // irrecoverable error
	Error                // non-fatal but permanent degradation
	Warning              // temporary degradation or transient IO error or client error
	Info                 // unimportant but noteworthy
	Ignore               // don't print
)

log message importance

type Logger

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

Logger is an utility for thread-safe and periodic logging. Use .Log() or one of its wrappers for issues that can be caught as they happen, PeriodicLogger for statistics and. Use .Compose() to make sure multi-statement messages get written as one. Should not be dereferenced or moved as it contains mutexes

func NewLogger

func NewLogger(writeTo io.WriteCloser, treshold Level) *Logger

NewLogger creates a new logger with a minimum importance level and the interval to check the periodic loggers Even though Logger implements WriteCloser, Loggers should not be nested.

func (*Logger) AddPeriodic

func (l *Logger) AddPeriodic(id string, minInterval, maxInterval time.Duration, f loggerFunc)

AddPeriodic stores a closure that will be called periodically with an interval that increases from minInterval to maxInterval exponentally.

func (*Logger) Close

func (l *Logger) Close()

Close the underlying Writer

func (*Logger) Compose

func (l *Logger) Compose(level Level) Composer

Compose allows holding the lock between multiple print

func (*Logger) Debug

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

Debug prints possibly interesting information, and is never filtered. Calls to this should probably not be committed.

func (*Logger) Error

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

Error prints a non-fatal but permanent error

func (*Logger) Fatal

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

Fatal prints an error message and exits the program

func (*Logger) FatalIf

func (l *Logger) FatalIf(cond bool, format string, args ...interface{})

FatalIf does nothing if cond is false, but otherwise prints the message and aborts the process.

func (*Logger) FatalIfErr

func (l *Logger) FatalIfErr(err error, format string, args ...interface{})

FatalIfErr does nothing if err is nil, but otherwise prints "Failed to <..>: $err.Error()" and aborts the process.

func (*Logger) Info

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

Info prints unimportant but noteworthy events or information

func (*Logger) Log

func (l *Logger) Log(level Level, format string, args ...interface{})

Log writes the message if it passes the loggers importance treshold

func (*Logger) RemovePeriodic

func (l *Logger) RemovePeriodic(id string)

RemovePeriodic removes a periodic logger so that it will never be called again. If it doesn't exist an error is printed to the logger.

func (*Logger) RunAllPeriodic

func (l *Logger) RunAllPeriodic()

RunAllPeriodic runs all the closures right now, ignoring any intervals.

func (*Logger) Warning

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

Warning prints an error that might be recovered from

func (*Logger) WriteAdapter

func (l *Logger) WriteAdapter(level Level) io.Writer

WriteAdapter returns a Writer that writes through this logger with the given level. Writes that don't end in a newline are buffered to not split messages, but Composer-written messages might get split. The adapter is not synchronized because both the standard log.Logger and other instances of this type serializes writes, and the underlying Logger is synchronized.

Jump to

Keyboard shortcuts

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