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 Logger
- type NilLogger
- func (nl NilLogger) Debug(msg string, fields map[string]any)
- func (nl NilLogger) Error(msg string, fields map[string]any)
- func (nl NilLogger) Fatal(msg string, fields map[string]any)
- func (nl NilLogger) Info(msg string, fields map[string]any)
- func (nl NilLogger) Panic(msg string, fields map[string]any)
- func (nl NilLogger) Trace(msg string, fields map[string]any)
- func (nl NilLogger) Warning(msg string, fields map[string]any)
- type NilWriter
- type Recorder
- func (r *Recorder) ConsoleOutput() string
- func (r *Recorder) ConsoleWriter() io.Writer
- func (r *Recorder) ErrorOutput() string
- func (r *Recorder) ErrorWriter() io.Writer
- 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) Verify(w WantedRecording) (issues []string, ok bool)
- func (r *Recorder) WriteCanonicalConsole(format string, a ...any)
- func (r *Recorder) WriteCanonicalError(format string, a ...any)
- func (r *Recorder) WriteConsole(format string, a ...any)
- func (r *Recorder) WriteError(format string, a ...any)
- 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) Trace(msg string, fields map[string]any)
- func (rl *RecordingLogger) Warning(msg string, fields map[string]any)
- type WantedRecording
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bus ¶
type Bus interface { Log(Level, string, map[string]any) WriteCanonicalConsole(string, ...any) WriteConsole(string, ...any) WriteCanonicalError(string, ...any) WriteError(string, ...any) ConsoleWriter() io.Writer ErrorWriter() io.Writer IsConsoleTTY() bool IsErrorTTY() bool }
Bus defines a set of functions for writing console messages and error messages, and for providing access to the console writer, 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 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) ConsoleOutput ¶
ConsoleOutput returns the data written as console output.
func (*Recorder) ConsoleWriter ¶
ConsoleWriter returns the internal console writer.
func (*Recorder) ErrorOutput ¶
ErrorOutput returns the data written as error output.
func (*Recorder) ErrorWriter ¶
ErrorWriter returns the internal error writer.
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) Verify ¶
func (r *Recorder) Verify(w WantedRecording) (issues []string, ok bool)
Verify verifies the recorded output against the expected output and returns any differences found.
func (*Recorder) WriteCanonicalConsole ¶
WriteCanonicalConsole records data written to the console.
func (*Recorder) WriteCanonicalError ¶
WriteCanonicalError records data written as an error.
func (*Recorder) WriteConsole ¶
WriteConsole records data written to the console.
func (*Recorder) WriteError ¶
WriteError records un-edited data written as an error.
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().
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.