log

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

This package defines a logger writer and reader that is capible of logging arbitrary values. This is extremely useful when debugging an application.

Index

Constants

View Source
const (
	// The separator character that is used to delineate different values in the
	// log
	LogPartSeparator string = "|"

	Error LogStatus = iota
	Warning
	Deprecation
	Info
	Debug
	Invalid
)

Variables

View Source
var (
	LogFileNotSpecified = errors.New("The log file was not specified.")
	LogFileMalformed    = errors.New("Log line malformed.")
	NoLogStatus         = errors.New("No log status present.")
	NoLogTime           = errors.New("No log time present.")
	NoLogObj            = errors.New("No object present.")
	InvalidLogStatus    = errors.New("Invalid log status.")
)

Functions

func JoinLogByTimeInc

func JoinLogByTimeInc[T any, U any](left LogEntry[T], right LogEntry[U]) bool

A function that can be passed to iter.Join to allow it to sort the values from two logs, providing each log statement in the order that they were written.

func NewOptions

func NewOptions() *options

Returns a new options struct initialized with the default values that can be passed to the other functions in this package that require options.

Types

type LogEntry

type LogEntry[T any] struct {
	Status  LogStatus
	Time    time.Time
	Message string
	Val     T
}

type LogStatus

type LogStatus int

func LogStatusFromString

func LogStatusFromString(s string) (LogStatus, error)

func (LogStatus) String

func (l LogStatus) String() string

type ValueLogger

type ValueLogger[T any] struct {
	Log func(val T, fmt string, vals ...any)
	// contains filtered or unexported fields
}

Represents a logger that outputs log files in the format shown below.

<log status> | <date time> | <message> | <JSON formatted object>

The message is set on a per-log-call basis. The object that is logged is formatted in JSON to allow for marshalling and un-marshalling. This logger should only be used when concerned with a value, or a small set of values. Generic logging statements with this logger will not perform optimally. For that consider using the std libs logging library.

func NewBlankLog

func NewBlankLog[T any]() ValueLogger[T]

Creates a new logger object that is initialized such that it performs no action. The idea behind this is to allow for debugging logging to be conditionally turned off in production code, hopefully allowing to compiler to optimize away the logger entirely.

func NewValueLogger

func NewValueLogger[T any](
	status LogStatus,
	file string,
	opts *options,
) (ValueLogger[T], error)

Returns a new log, initialized with the supplied options. If an error occurs it will be returned and the logger will be in an invalid state and should not be used.

func (*ValueLogger[T]) Clear

func (l *ValueLogger[T]) Clear() error

Clears all statements in the log and it's associated file. This will maintain the state of the log file, meaning if it was closed before it will be closed after the operation; same thing with open.

func (*ValueLogger[T]) Close

func (l *ValueLogger[T]) Close()

Closes the logger and its associated file. Any writes to the Log method after calling this function will result in no action being performed.

func (*ValueLogger[T]) LogElems

func (l *ValueLogger[T]) LogElems() iter.Iter[LogEntry[T]]

Retrieves the elements from the log, returning them as a stream of LogEntry structs. Any objects that were encoded in the log will be un-marshaled into each LogEntry struct allowing values to be retrieved from the log. The datetime format will be determined from the options that the [Logger] struct was initialized with.

func (*ValueLogger[T]) SetStatus

func (l *ValueLogger[T]) SetStatus(s LogStatus)

Sets the loggers status to the supplied log status.

Jump to

Keyboard shortcuts

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