termlog

package module
v0.0.0-...-f866977 Latest Latest
Warning

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

Go to latest
Published: May 23, 2025 License: MIT Imports: 10 Imported by: 21 modules, 70 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

GoDoc Travis Build Status Build status

termlog: Logging for interactive terminals

screenshot

Basic usage

l := termlog.NewLog()
l.Say("Log")
l.Notice("Notice!")
l.Warn("Warn!")
l.Shout("Error!")

Each log entry gets a timestamp.

Groups

Groups collect entries together under a single timestamp, with subsequent lines indented:

g = l.Group()
g.Say("This line gets a timestamp")
g.Say("This line will be indented with no timestamp")
g.Done()

Groups produce no output until the .Done() method is called - a good use for defer. Termlog ensures that all grouped entries appear together in output.

Streams

Streams associate log entries with a header. New stream log entries only get a header if another log source (i.e. a different stream, group, or plain log) has produced output in the meantime. Each stream log entry gets its own timestamp.

g = l.Stream("This is the header")
g.Say("The header will be printed before this line")
g.Say("But not before this one")
g.Done()

Named logs

Log entries can be named using the *As methods:

l := termlog.NewLog()
l.Say("hello")
l.SayAs("debug", "Some debugging info")

Named entries are always silenced, unless they've been enabled specifically, like so:

l.Enable("debug")

Specified colors

The package is compatible with the color specifications from github.com/fatih/color, which means that colors can be composed like this:

l.Say("Here are some composed colors...")
l.Say(
	"%s %s %s",
	color.RedString("red"),
	color.GreenString("green"),
	color.BlueString("blue"),
)

Documentation

Overview

Package termlog provides facilities for logging to a terminal geared towards interactive use.

Index

Constants

This section is empty.

Variables

View Source
var DefaultPalette = Palette{
	Say:       color.New(),
	Notice:    color.New(color.FgBlue),
	Warn:      color.New(color.FgYellow),
	Shout:     color.New(color.FgRed),
	Timestamp: color.New(color.FgCyan),
	Header:    color.New(color.FgBlue),
}

DefaultPalette is a sensbile default palette, with the following foreground colours:

Say: Terminal default
Notice: Blue
Warn: Yellow
Shout: Red
Timestamp: Cyan

Functions

func NewContext

func NewContext(ctx context.Context, logger Logger) context.Context

NewContext creates a new context with an included Logger

func SetOutput

func SetOutput(w io.Writer)

SetOutput sets the output writer for termlog (stdout by default).

Types

type Group

type Group interface {
	Logger
	Done()
	Quiet()
}

Group is a collected group of log entries. Logs are only displayed once the Done method is called.

type Log

type Log struct {
	Palette *Palette
	TimeFmt string
	// contains filtered or unexported fields
}

Log is the top-level log structure

func NewLog

func NewLog() *Log

NewLog creates a new Log instance and initialises it with a set of defaults.

func (*Log) Color

func (*Log) Color(state bool)

Color sets the state of colour output - true to turn on, false to disable.

func (*Log) Enable

func (l *Log) Enable(name string)

Enable logging for a specified name

func (*Log) Group

func (l *Log) Group() Group

Group creates a new log group

func (*Log) Notice

func (l *Log) Notice(format string, args ...interface{})

Notice logs a line with the Notice color

func (*Log) NoticeAs

func (l *Log) NoticeAs(name string, format string, args ...interface{})

NoticeAs logs a line with the Notice color

func (*Log) Quiet

func (l *Log) Quiet()

Quiet disables all output

func (*Log) Say

func (l *Log) Say(format string, args ...interface{})

Say logs a line

func (*Log) SayAs

func (l *Log) SayAs(name string, format string, args ...interface{})

SayAs logs a line

func (*Log) Shout

func (l *Log) Shout(format string, args ...interface{})

Shout logs a line with the Shout color

func (*Log) ShoutAs

func (l *Log) ShoutAs(name string, format string, args ...interface{})

ShoutAs logs a line with the Shout color

func (*Log) Stream

func (l *Log) Stream(header string) Stream

Stream creates a new log group

func (*Log) Warn

func (l *Log) Warn(format string, args ...interface{})

Warn logs a line with the Warn color

func (*Log) WarnAs

func (l *Log) WarnAs(name string, format string, args ...interface{})

WarnAs logs a line with the Warn color

type LogTest

type LogTest struct {
	Log *Log
	// contains filtered or unexported fields
}

LogTest lets test suites work with log output

func NewLogTest

func NewLogTest() *LogTest

NewLogTest creates a new LogTest

func (*LogTest) String

func (lt *LogTest) String() string

type Logger

type Logger interface {
	Say(format string, args ...interface{})
	Notice(format string, args ...interface{})
	Warn(format string, args ...interface{})
	Shout(format string, args ...interface{})

	SayAs(name string, format string, args ...interface{})
	NoticeAs(name string, format string, args ...interface{})
	WarnAs(name string, format string, args ...interface{})
	ShoutAs(name string, format string, args ...interface{})
}

Logger logs things

func FromContext

func FromContext(ctx context.Context) Logger

FromContext retrieves a Logger from a context. If no logger is present, we return a new silenced logger that will produce no output.

type Palette

type Palette struct {
	Timestamp *color.Color
	Say       *color.Color
	Notice    *color.Color
	Warn      *color.Color
	Shout     *color.Color
	Header    *color.Color
}

Palette defines the colour of output

type Stream

type Stream interface {
	Logger
	Quiet()
	Header()
}

Stream is a stream of log entries with a header

type TermLog

type TermLog interface {
	Logger
	Group() Group
	Stream(header string) Stream
	Quiet()
}

TermLog is the top-level termlog interface

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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