logutils

package
v3.8.0-0.dev+incompatible Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2019 License: Apache-2.0 Imports: 12 Imported by: 172

Documentation

Index

Constants

View Source
const (
	// FieldForceFlush is a field name used to signal to the BackgroundHook that it should flush the log after this
	// message.  It can be used as follows: logrus.WithField(FieldForceFlush, true).Info("...")
	FieldForceFlush = "__flush__"
)

Variables

This section is empty.

Functions

func FilterLevels

func FilterLevels(maxLevel log.Level) []log.Level

FilterLevels returns all the logrus.Level values <= maxLevel.

func FormatForSyslog

func FormatForSyslog(entry *log.Entry) string

FormatForSyslog formats logs in a way tailored for syslog. It avoids logging information that is already included in the syslog metadata such as timestamp and PID. The log level _is_ included because syslog doesn't seem to output it by default and it's very useful.

INFO endpoint_mgr.go 434: Skipping configuration of interface because it is oper down.
ifaceName="cali1234"

func SafeParseLogLevel

func SafeParseLogLevel(logLevel string) log.Level

safeParseLogLevel parses a string version of a logrus log level, defaulting to logrus.PanicLevel on failure.

Types

type BackgroundHook

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

BackgroundHook is a logrus Hook that (synchronously) formats each log and sends it to one or more Destinations for writing ona background thread. It supports filtering destinations on individual log levels. We write logs from background threads so that blocking of the output stream doesn't block the mainline code. Up to a point, we queue logs for writing, then we start dropping logs.

func NewBackgroundHook

func NewBackgroundHook(
	levels []log.Level,
	syslogLevel log.Level,
	destinations []*Destination,
	counter prometheus.Counter,
) *BackgroundHook

func (*BackgroundHook) Fire

func (h *BackgroundHook) Fire(entry *log.Entry) (err error)

func (*BackgroundHook) Levels

func (h *BackgroundHook) Levels() []log.Level

func (*BackgroundHook) Start

func (h *BackgroundHook) Start()

type ContextHook

type ContextHook struct {
}

func (ContextHook) Fire

func (hook ContextHook) Fire(entry *log.Entry) error

func (ContextHook) Levels

func (hook ContextHook) Levels() []log.Level

type Destination

type Destination struct {
	// Level is the minimum level that a log must have to be logged to this destination.
	Level log.Level
	// Channel is the channel used to queue logs to the background worker thread.  Public for
	// test purposes.
	Channel chan QueuedLog
	// contains filtered or unexported fields
}

func NewStreamDestination

func NewStreamDestination(
	level log.Level,
	writer io.Writer,
	c chan QueuedLog,
	disableLogDropping bool,
	counter prometheus.Counter,
) *Destination

func NewSyslogDestination

func NewSyslogDestination(
	level log.Level,
	writer syslogWriter,
	c chan QueuedLog,
	disableLogDropping bool,
	counter prometheus.Counter,
) *Destination

func (*Destination) Close

func (d *Destination) Close()

Close closes the channel to the background goroutine. This is only safe to call if you know that the destination is no longer in use by any thread; in tests, for example.

func (*Destination) LoopWritingLogs

func (d *Destination) LoopWritingLogs()

LoopWritingLogs is intended to be used as a background go-routine. It processes the logs from the channel.

func (*Destination) Send

func (d *Destination) Send(ql QueuedLog) (ok bool)

Send sends a log to the background thread. It returns true on success or false if the channel is blocked.

type Formatter

type Formatter struct{}

Formatter is our custom log formatter designed to balance ease of machine processing with human readability. Logs include:

  • A sortable millisecond timestamp, for scanning and correlating logs
  • The log level, near the beginning of the line, to aid in visual scanning
  • The PID of the process to make it easier to spot log discontinuities (If you are looking at two disjoint chunks of log, were they written by the same process? Was there a restart in-between?)
  • The file name and line number, as essential context
  • The message!
  • Log fields appended in sorted order

Example:

2017-01-05 09:17:48.238 [INFO][85386] endpoint_mgr.go 434: Skipping configuration of
interface because it is oper down. ifaceName="cali1234"

func (*Formatter) Format

func (f *Formatter) Format(entry *log.Entry) ([]byte, error)

type NullWriter

type NullWriter struct{}

NullWriter is a dummy writer that always succeeds and does nothing.

func (*NullWriter) Write

func (w *NullWriter) Write(p []byte) (int, error)

type QueuedLog

type QueuedLog struct {
	Level         log.Level
	Message       []byte
	SyslogMessage string
	WaitGroup     *sync.WaitGroup

	// NumSkippedLogs contains the number of logs that were skipped before this log (due to the
	// queue being blocked).
	NumSkippedLogs uint
}

func (QueuedLog) OnLogDone

func (ql QueuedLog) OnLogDone()

Jump to

Keyboard shortcuts

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