termlog

package
v0.0.0-...-eecc6eb Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2015 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package termlog provides facilities for logging to a terminal geared towards interactive use. Basic usage looks like this:

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

 Each log entry gets a timestamp. Entries can be grouped together under one
 timestamp, with subsequent lines indented like so:

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

Groups must be marked as .Done() before output is produced - a good use for defer.

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

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

The *As logging functions tag a line with an identity. All tagged lines are silenced unless explicitly enabled on the Logger with the .Enable() method.

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),
}

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

Types

type Group

type Group struct {
	// contains filtered or unexported fields
}

Group is a group of lines that constitue a single log entry that won't be split. Lines in a group are indented.

func (*Group) Done

func (g *Group) Done()

Done outputs the group to screen

func (*Group) Group

func (g *Group) Group() Logger

Group of a group is just same group

func (*Group) Notice

func (g *Group) Notice(format string, args ...interface{})

Notice logs a line with the Notice color

func (*Group) NoticeAs

func (g *Group) NoticeAs(name string, format string, args ...interface{})

NoticeAs logs a line with the Notice color

func (*Group) Quiet

func (g *Group) Quiet()

Quiet disables output for this subgroup

func (*Group) Say

func (g *Group) Say(format string, args ...interface{})

Say logs a line

func (*Group) SayAs

func (g *Group) SayAs(name string, format string, args ...interface{})

SayAs logs a line

func (*Group) Shout

func (g *Group) Shout(format string, args ...interface{})

Shout logs a line with the Shout color

func (*Group) ShoutAs

func (g *Group) ShoutAs(name string, format string, args ...interface{})

ShoutAs logs a line with the Shout color

func (*Group) Warn

func (g *Group) Warn(format string, args ...interface{})

Warn logs a line with the Warn color

func (*Group) WarnAs

func (g *Group) WarnAs(name string, format string, args ...interface{})

WarnAs logs a line with the Warn color

type Log

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

Log has a level cutoff

func NewLog

func NewLog() *Log

NewLog creates a new Log instance

func (*Log) Done

func (l *Log) Done()

Done is just a stub to comply with the Logger interface

func (*Log) Enable

func (l *Log) Enable(name string)

Enable logging for a specified name

func (*Log) Group

func (l *Log) Group() Logger

Group creates a new log group

func (*Log) NoColor

func (*Log) NoColor()

NoColor disables colour output

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) 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 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{})

	Done()
	Quiet()
	Group() Logger
}

Logger logs things

func FromContext

func FromContext(ctx context.Context) Logger

FromContext retrieves a logger from a context

type Palette

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

Palette defines the colour of output

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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