logs

package
v0.0.0-...-57fe098 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package logs provides a simple logging interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer

type Buffer []Message

Buffer is an in-memory buffer of log messages.

func (*Buffer) Log

func (b *Buffer) Log() Log

Log is a Log implementation that appends the message to the buffer.

type Log

type Log func(Message)

Log is a function that logs a message.

var Discard Log

Discard is a log that discards all messages.

var Terse Log = func(m Message) {
	if !m.IsVerbose {
		fmt.Println(m.Text)
	}
}

Terse is a log that prints non-verbose messages to STDOUT.

var Verbose Log = func(m Message) {
	fmt.Println(m.Text)
}

Verbose is a log that prints all messages to STDOUT.

func Tee

func Tee(logs ...Log) Log

Tee returns a new log that writes to multiple other logs.

func (Log) WithPrefix

func (log Log) WithPrefix(format string, args ...any) Log

WithPrefix returns a log that prefixes all messages with the given string.

func (Log) Write

func (log Log) Write(format string, args ...any)

Write writes a log message.

func (Log) WriteVerbose

func (log Log) WriteVerbose(format string, args ...any)

WriteVerbose writes a log message that is only shown if verbose logging is enabled.

type Message

type Message struct {
	Text      string
	IsVerbose bool
}

Message is a log message.

type Writer

type Writer struct {
	// Target is the log that receives the log messages.
	Target Log
	// contains filtered or unexported fields
}

Writer is an adaptor that presents a Logger as an io.WriteCloser.

Each line of text written via Write() is logged as a separate message. Any call to write with text that does not end in line separator is buffered until a line separator is written or Close() is called. Blank lines are ignored.

Any instance of a LF, CR or CRLF is treated as a line separator. This allows usage with Unix-style or Windows-style text output, as well as console output that uses CR to overwrite the current line.

func (*Writer) Close

func (w *Writer) Close() error

Close closes the writer, producing a log message from any remaining buffered text.

func (*Writer) Write

func (w *Writer) Write(data []byte) (int, error)

Jump to

Keyboard shortcuts

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