Documentation ¶
Overview ¶
Package output provides an easy way for command-line oriented programs to handle console and error writing and logging, as well as a simple way to verify what is written to those channels.
Installation
go get github.com/majohn-r/output
Index ¶
- type Bus
- type Level
- type ListDecorator
- type Logger
- type NilLogger
- func (nl NilLogger) Debug(_ string, _ map[string]any)
- func (nl NilLogger) Error(_ string, _ map[string]any)
- func (nl NilLogger) Fatal(_ string, _ map[string]any)
- func (nl NilLogger) Info(_ string, _ map[string]any)
- func (nl NilLogger) Panic(_ string, _ map[string]any)
- func (nl NilLogger) Trace(_ string, _ map[string]any)
- func (nl NilLogger) Warning(_ string, _ map[string]any)
- type NilWriter
- type Recorder
- func (r *Recorder) BeginConsoleList(numeric bool)
- func (r *Recorder) BeginErrorList(numeric bool)
- func (r *Recorder) ConsoleListDecorator() *ListDecorator
- func (r *Recorder) ConsoleOutput() string
- func (r *Recorder) ConsolePrintf(format string, args ...any)
- func (r *Recorder) ConsolePrintln(msg string)
- func (r *Recorder) ConsoleWriter() io.Writer
- func (r *Recorder) DecrementTab(t uint8)
- func (r *Recorder) EndConsoleList()
- func (r *Recorder) EndErrorList()
- func (r *Recorder) ErrorListDecorator() *ListDecorator
- func (r *Recorder) ErrorOutput() string
- func (r *Recorder) ErrorPrintf(format string, args ...any)
- func (r *Recorder) ErrorPrintln(msg string)
- func (r *Recorder) ErrorWriter() io.Writer
- func (r *Recorder) IncrementTab(t uint8)
- func (r *Recorder) IsConsoleTTY() bool
- func (r *Recorder) IsErrorTTY() bool
- func (r *Recorder) Log(l Level, msg string, fields map[string]any)
- func (r *Recorder) LogOutput() string
- func (r *Recorder) Report(t TestingReporter, header string, w WantedRecording)
- func (r *Recorder) Tab() uint8
- func (r *Recorder) Verify(w WantedRecording) (differences []string, verified bool)
- type RecordingLogger
- func (rl *RecordingLogger) Debug(msg string, fields map[string]any)
- func (rl *RecordingLogger) Error(msg string, fields map[string]any)
- func (rl *RecordingLogger) Fatal(msg string, fields map[string]any)
- func (rl *RecordingLogger) Info(msg string, fields map[string]any)
- func (rl *RecordingLogger) Panic(msg string, fields map[string]any)
- func (rl *RecordingLogger) String() string
- func (rl *RecordingLogger) Trace(msg string, fields map[string]any)
- func (rl *RecordingLogger) Warning(msg string, fields map[string]any)
- type TestingReporter
- type WantedRecording
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bus ¶
type Bus interface { // Log logs a message and map of fields at a specified log level. Log(Level, string, map[string]any) // ConsolePrintf prints a message with arguments to the error channel ConsolePrintf(string, ...any) // ConsolePrintln prints a message to the error channel, terminated by a newline ConsolePrintln(string) // ErrorPrintf prints a message with arguments to the error channel ErrorPrintf(string, ...any) // ErrorPrintln prints a message to the error channel, terminated by a newline ErrorPrintln(string) // ConsoleWriter returns a writer for console output. ConsoleWriter() io.Writer // ErrorWriter returns a writer for error output. ErrorWriter() io.Writer // IsConsoleTTY returns whether the console writer is a TTY IsConsoleTTY() bool // IsErrorTTY returns whether the error writer is a TTY IsErrorTTY() bool // Tab returns the current tab setting (number of spaces) Tab() uint8 // IncrementTab increases the current tab setting up to the max uint8 value IncrementTab(uint8) // DecrementTab decreases the current tab setting; will not go below 0 DecrementTab(uint8) // BeginConsoleList initiates console listing BeginConsoleList(bool) // EndConsoleList terminates console listing EndConsoleList() // ConsoleListDecorator makes the console list decorator available ConsoleListDecorator() *ListDecorator // BeginErrorList initiates error listing BeginErrorList(bool) // EndErrorList terminates error listing EndErrorList() // ErrorListDecorator makes the error list decorator available ErrorListDecorator() *ListDecorator }
Bus defines a set of functions for writing console messages and error messages, and for providing access to the console writer and the error writer, and a Logger instance; its primary use is to simplify how application code handles console, error, and logged output, and its secondary use is to make it easy to test output writing.
func NewCustomBus ¶
NewCustomBus returns an implementation of Bus that lets the caller specify the console and error writers and the Logger.
func NewDefaultBus ¶
NewDefaultBus returns an implementation of Bus that writes console messages to stdout and error messages to stderr.
type ListDecorator ¶ added in v0.7.0
type ListDecorator struct {
// contains filtered or unexported fields
}
ListDecorator contains the data needed for creating list decorations
func (*ListDecorator) Decorator ¶ added in v0.7.0
func (ld *ListDecorator) Decorator() string
Decorator generates the appropriate decoration for lists (and typically, this is the empty string)
type Logger ¶
type Logger interface { Trace(msg string, fields map[string]any) Debug(msg string, fields map[string]any) Info(msg string, fields map[string]any) Warning(msg string, fields map[string]any) Error(msg string, fields map[string]any) Panic(msg string, fields map[string]any) Fatal(msg string, fields map[string]any) }
Logger defines a set of functions for writing to a log at various log levels
type NilLogger ¶
type NilLogger struct{}
NilLogger is a logger that does nothing at all; its intended use is for test code where the side effect of logging is of no interest whatsoever.
type NilWriter ¶
type NilWriter struct{}
NilWriter is a writer that does nothing at all; its intended use is for test code where the side effect of writing to the console or writing error output is of no interest whatsoever.
type Recorder ¶
type Recorder struct {
// contains filtered or unexported fields
}
Recorder is an implementation of Bus that simply records its inputs; it's intended for unit tests, where you can provide the code under test (that needs a Bus) with an instance of Recorder and then verify that the code produces the expected console, error, and log output.
func NewRecorder ¶
func NewRecorder() *Recorder
NewRecorder returns a recording implementation of Bus.
func (*Recorder) BeginConsoleList ¶ added in v0.7.0
BeginConsoleList initiates console listing
func (*Recorder) BeginErrorList ¶ added in v0.7.0
BeginErrorList initiates error listing
func (*Recorder) ConsoleListDecorator ¶ added in v0.7.0
func (r *Recorder) ConsoleListDecorator() *ListDecorator
ConsoleListDecorator makes the console list decorator available
func (*Recorder) ConsoleOutput ¶
ConsoleOutput returns the data written as console output.
func (*Recorder) ConsolePrintf ¶ added in v0.8.0
ConsolePrintf prints a message with arguments to the error channel
func (*Recorder) ConsolePrintln ¶ added in v0.8.0
ConsolePrintln prints a message to the error channel, terminated by a newline
func (*Recorder) ConsoleWriter ¶
ConsoleWriter returns the internal console writer.
func (*Recorder) DecrementTab ¶ added in v0.5.0
DecrementTab decrements the tab setting by the specified number of spaces
func (*Recorder) EndConsoleList ¶ added in v0.7.0
func (r *Recorder) EndConsoleList()
EndConsoleList terminates console listing
func (*Recorder) EndErrorList ¶ added in v0.7.0
func (r *Recorder) EndErrorList()
EndErrorList terminates error listing
func (*Recorder) ErrorListDecorator ¶ added in v0.7.0
func (r *Recorder) ErrorListDecorator() *ListDecorator
ErrorListDecorator makes the error list decorator available
func (*Recorder) ErrorOutput ¶
ErrorOutput returns the data written as error output.
func (*Recorder) ErrorPrintf ¶ added in v0.8.0
ErrorPrintf prints a message with arguments to the error channel
func (*Recorder) ErrorPrintln ¶ added in v0.8.0
ErrorPrintln prints a message to the error channel, terminated by a newline
func (*Recorder) ErrorWriter ¶
ErrorWriter returns the internal error writer.
func (*Recorder) IncrementTab ¶ added in v0.5.0
IncrementTab increments the tab setting by the specified number of spaces
func (*Recorder) IsConsoleTTY ¶ added in v0.3.0
IsConsoleTTY returns whether the console writer is a TTY
func (*Recorder) IsErrorTTY ¶ added in v0.3.0
IsErrorTTY returns whether the error writer is a TTY
func (*Recorder) Report ¶ added in v0.4.0
func (r *Recorder) Report(t TestingReporter, header string, w WantedRecording)
Report handles the common use case for using a Recorder: detecting whether any differences were recorded, and reporting them if there were any differences.
type RecordingLogger ¶
type RecordingLogger struct {
// contains filtered or unexported fields
}
RecordingLogger is a simple logger intended for use in unit tests; it records the output given to it.
Caveats:
Your production log may not actually do anything with some calls into it - for instance, many logging frameworks allow you to limit the severity of what is logged, e.g., only warnings or worse; RecordingLogger will record every call made into it.
The output recorded cannot be guaranteed to match exactly what your logging code records - but it will include the log level, the message, and all field-value pairs.
The RecordingLogger will probably behave differently to a logging mechanism that supports panic and fatal logs, in that a production logger will probably call panic in processing a panic log, and will probably exit the program on a fatal log. RecordingLogger does neither of those.
func NewRecordingLogger ¶
func NewRecordingLogger() *RecordingLogger
NewRecordingLogger returns a recording implementation of Logger.
func (*RecordingLogger) Debug ¶
func (rl *RecordingLogger) Debug(msg string, fields map[string]any)
Debug records a debug log message.
func (*RecordingLogger) Error ¶
func (rl *RecordingLogger) Error(msg string, fields map[string]any)
Error records an error log message.
func (*RecordingLogger) Fatal ¶
func (rl *RecordingLogger) Fatal(msg string, fields map[string]any)
Fatal records a fatal log message and does not terminate the program.
func (*RecordingLogger) Info ¶
func (rl *RecordingLogger) Info(msg string, fields map[string]any)
Info records an info log message.
func (*RecordingLogger) Panic ¶
func (rl *RecordingLogger) Panic(msg string, fields map[string]any)
Panic records a panic log message and does not call panic().
func (*RecordingLogger) String ¶ added in v0.5.3
func (rl *RecordingLogger) String() string
type TestingReporter ¶ added in v0.4.0
TestingReporter is an interface that requires the one *testing.T function that we care about: Errorf
type WantedRecording ¶
WantedRecording is intended to be used in unit tests as part of the test structure; it allows the test writer to capture what the test wants the console, error, and log output to contain.