lalog

package
v0.0.0-...-2db35d6 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2024 License: MPL-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MaxLogMessageLen is the maximum length memorised for each of the latest log entries.
	MaxLogMessageLen = 2048
)

Variables

View Source
var (
	// MaxLogMessagePerSec is the maximum number of messages each logger will be able to print out.
	// Any additional log messages will be dropped.
	MaxLogMessagePerSec = runtime.NumCPU() * 300

	// LatestWarnings are a small number of the most recent log messages
	// (warnings and info messages) kept in memory for retrieval and inspection.
	LatestLogs = datastruct.NewRingBuffer(1 * 1048576 / MaxLogMessageLen)

	// LatestWarnings are a small number of the most recent warning log messages kept in memory for retrieval and inspection.
	LatestWarnings = datastruct.NewRingBuffer(1 * 1048576 / MaxLogMessageLen)

	// LatestWarningActors is a small number of identifiers (actors) from recent
	// warning messages, they are used to de-duplicate these messages at regular
	// intervals to reduce spamming.
	LatestWarningActors = datastruct.NewLeastRecentlyUsedBuffer(1 * 1048576 / MaxLogMessageLen)

	// LatestWarningActors is a small number of message contents from recent log
	// messages of all types , they are used to de-duplicate these messages at
	// regular intervals to reduce spamming.
	LatestLogMessageContent = datastruct.NewLeastRecentlyUsedBuffer(1 * 1048576 / MaxLogMessageLen)

	// LogWarningCallback is invoked in a separate goroutine after any logger has processed a warning message.
	// The function must avoid generating a warning log message of itself, to avoid an infinite recursion.
	GlobalLogWarningCallback LogWarningCallbackFunc = nil

	// NumDropped is the number of de-duplicated log messages that are not
	// printed to stderr.
	NumDropped = new(atomic.Int64)
)
View Source
var DefaultLogger = &Logger{ComponentName: "default", ComponentID: []LoggerIDField{{"PID", os.Getpid()}}}

DefaultLogger must be used when it is not possible to acquire a reference to a more dedicated logger.

View Source
var DiscardCloser = discardCloser{}

DiscardCloser implements io.WriteCloser.

Functions

func ByteArrayLogString

func ByteArrayLogString(data []byte) string

ByteArrayLogString returns a human-readable string for the input byte array. The returned string is only suitable for log messages.

func ClearDedupBuffers

func ClearDedupBuffers()

Clear the global LRU buffers used for de-duplicating log messages.

func LintString

func LintString(in string, maxLength int) string

LintString returns a copy of the input string with unusual characters (such as non-printable characters and record separators) replaced by an underscore. Consequently, printable characters such as CJK languages are also replaced. Additionally the string return value is capped to the maximum specified length.

func TruncateString

func TruncateString(in string, maxLength int) string

TruncateString returns the input string as-is if it is less or equal to the desired length. Otherwise, it removes text from the middle of string to fit to the desired length, and substitutes the removed portion with text "...(truncated)..." and then returns.

Types

type ByteLogWriter

type ByteLogWriter struct {
	io.WriteCloser
	MaxBytes int // MaxBytes is the number of latest data bytes to keep.
	// contains filtered or unexported fields
}

ByteLogWriter forwards verbatim bytes to destination writer, and keeps designated number of latest output bytes in internal buffers for later retrieval. It implements io.Writer interface.

func NewByteLogWriter

func NewByteLogWriter(destination io.Writer, maxBytes int) *ByteLogWriter

NewByteLogWriter initialises a new ByteLogBuffer and returns it.

func (*ByteLogWriter) Close

func (writer *ByteLogWriter) Close() error

Close does nothing and always returns nil.

func (*ByteLogWriter) Retrieve

func (writer *ByteLogWriter) Retrieve(asciiOnly bool) (ret []byte)

Retrieve returns a copy of the latest bytes written.

func (*ByteLogWriter) Write

func (writer *ByteLogWriter) Write(p []byte) (n int, err error)

Write implements io.Writer to forward the data to destination writer.

type LogWarningCallbackFunc

type LogWarningCallbackFunc func(componentName, componentID, funcName string, actorName interface{}, err error, msg string)

type Logger

type Logger struct {
	ComponentName string          // ComponentName is similar to a class name, or a category name.
	ComponentID   []LoggerIDField // ComponentID comprises key-value pairs that give log entry a clue as to its origin.
	// contains filtered or unexported fields
}

Help to write log messages in a regular format.

func (*Logger) Abort

func (logger *Logger) Abort(actorName interface{}, err error, template string, values ...interface{})

func (*Logger) Format

func (logger *Logger) Format(functionName string, actorName interface{}, err error, template string, values ...interface{}) string

Format a log message and return, but do not print it.

func (*Logger) Info

func (logger *Logger) Info(actorName interface{}, err error, template string, values ...interface{})

Print a log message and keep the message in latest log buffer. If there is an error, also keep the message in warnings buffer.

func (*Logger) MaybeMinorError

func (logger *Logger) MaybeMinorError(err error)

MaybeMinorError logs the input error, which by convention is minor in nature, in an info log message. As a special case, if the error indicates the closure of a network connection, or includes the keyword "broken", then no log message will be written.

func (*Logger) Panic

func (logger *Logger) Panic(actorName interface{}, err error, template string, values ...interface{})

func (*Logger) Warning

func (logger *Logger) Warning(actorName interface{}, err error, template string, values ...interface{})

Print a log message and keep the message in warnings buffer.

type LoggerIDField

type LoggerIDField struct {
	Key   string      // Key is an arbitrary string key
	Value interface{} // Value is an arbitrary value that will be converted to string upon printing a log entry.
}

LoggerIDField is a field of Logger's ComponentID, all fields that make up a ComponentID offer log entry a clue as to which component instance generated the log message.

type RateLimit

type RateLimit struct {
	UnitSecs int64
	MaxCount int
	Logger   *Logger
	// contains filtered or unexported fields
}

RateLimit tracks number of hits performed by each source ("actor") to determine whether a source has exceeded specified rate limit. Instead of being a rolling counter, the tracking data is reset to empty at regular interval. Remember to call Initialise() before use!

func NewRateLimit

func NewRateLimit(unitSecs int64, maxCount int, logger *Logger) (limit *RateLimit)

NewRateLimit constructs a new rate limiter.

func (*RateLimit) Add

func (limit *RateLimit) Add(actor string, logIfLimitHit bool) bool

Add increases the current counter by one for the actor name/ID if the max count per time interval has not been exceeded, and returns true. Otherwise, the actor's current counter stays until the interval passes, and the function will return false.

Jump to

Keyboard shortcuts

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