plain

package module
v1.1.6 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2026 License: GPL-3.0 Imports: 14 Imported by: 0

README

plain

A tiny, super fast logger for Go with a couple of handy input helpers.

Install
go get -u github.com/coalaura/plain
Quick start
import (
	"github.com/coalaura/plain"
)

func main() {
	pl := plain.New()

	pl.Debugln("Hello from Debug")
	pl.Println("Hello from Print")
	pl.Warnln("Hello from Warn")
	pl.Errorln("Hello from Error")

	confirmed, _ := pl.Confirm("Confirm", true)

	if confirmed {
		pl.Println("You confirmed")
	} else {
		pl.Println("You declined")
	}

	input, _ := pl.Read("Input: ", 64)

	pl.Printf("You entered '%s'\n", input)

	options := []string{"Red", "Green", "Blue", "Yellow"}

	index, _ := pl.Select("Select: ", options)

	pl.Printf("You selected '%s'\n", options[index])

	// optional, prevents leftover un-reset colors
	pl.WaitForInterrupt(true)

	// cleanup code on exit...
}

Documentation

Index

Constants

View Source
const (
	// RFC3339Local is an RFC3339-like time format without timezone information
	RFC3339Local = "2006-01-02T15:04:05"

	// Reset resets ANSI styling to default
	Reset = "\x1b[0m"
)
View Source
const (
	ModeNone = iota - 1
	ModeSome
	Mode8Bit
	ModeFull
)

Variables

This section is empty.

Functions

func AsStrings

func AsStrings[T any](input []T) []string

AsStrings converts a slice/array of any type to a slice of strings

func WithDate

func WithDate(format string) option

WithDate sets the timestamp format used in log headers (empty disables timestamps)

func WithTarget

func WithTarget(out io.Writer) option

WithTarget sets the output writer used by the logger

Types

type Plain

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

Plain is a small, allocation-conscious logger with optional ANSI color output

func New

func New(opts ...option) *Plain

New creates a Plain logger configured by the provided options

func (*Plain) Close

func (p *Plain) Close() error

Close runs any registered closers and closes the underlying writer when supported

func (*Plain) Confirm

func (p *Plain) Confirm(prompt string, defaultYes bool) (bool, error)

Confirm displays a prompt aligned with the logger's format and reads y/n confirmation from stdin.

func (*Plain) Debug

func (p *Plain) Debug(a ...any)

Debug formats using the default formats for its operands and writes to the target output as a debug log.

func (*Plain) Debugf

func (p *Plain) Debugf(format string, a ...any)

Debugf formats according to a format specifier and writes to the target output as a debug log.

func (*Plain) Debugln

func (p *Plain) Debugln(a ...any)

Debugln formats using the default formats for its operands and writes to the target output as a debug log with a trailing newline.

func (*Plain) Error

func (p *Plain) Error(a ...any)

Error formats using the default formats for its operands and writes to the target output as an error.

func (*Plain) Errorf

func (p *Plain) Errorf(format string, a ...any)

Errorf formats according to a format specifier and writes to the target output as an error.

func (*Plain) Errorln

func (p *Plain) Errorln(a ...any)

Errorln formats using the default formats for its operands and writes to the target output as an error with a trailing newline.

func (*Plain) LogRequest

func (p *Plain) LogRequest(request *http.Request, metrics *httpsnoop.Metrics)

LogRequest writes a single formatted access log line for request using the provided metrics

func (*Plain) Middleware

func (p *Plain) Middleware() func(http.Handler) http.Handler

Middleware returns an http middleware that logs each request after it has been handled

func (*Plain) MustExit added in v1.1.5

func (p *Plain) MustExit(err error)

MustExit logs the error and exits with code 1 if the error is not nil.

func (*Plain) MustFail

func (p *Plain) MustFail(err error)

MustFail panics if err is not nil

func (*Plain) Print

func (p *Plain) Print(a ...any)

Print formats using the default formats for its operands and writes to the target output.

func (*Plain) Printf

func (p *Plain) Printf(format string, a ...any)

Printf formats according to a format specifier and writes to the target output.

func (*Plain) Println

func (p *Plain) Println(a ...any)

Println formats using the default formats for its operands and writes to the target output with a trailing newline.

func (*Plain) Read

func (p *Plain) Read(prompt string, max int) (string, error)

Read displays a prompt aligned with the logger's format and reads max bytes from stdin.

func (*Plain) ReadOne

func (p *Plain) ReadOne(prompt string) (rune, error)

ReadOne displays a prompt aligned with the logger's format and reads 1 byte from stdin.

func (*Plain) Select

func (p *Plain) Select(prompt string, options []string) (int, error)

Select displays a cyclic selector aligned with the logger's format.

func (*Plain) WaitForInterrupt added in v1.1.6

func (p *Plain) WaitForInterrupt(close bool) error

WaitForInterrupt blocks until SIGINT or SIGTERM is received and optionally closes the logger

func (*Plain) Warn

func (p *Plain) Warn(a ...any)

Warn formats using the default formats for its operands and writes to the target output as a warning.

func (*Plain) Warnf

func (p *Plain) Warnf(format string, a ...any)

Warnf formats according to a format specifier and writes to the target output as a warning.

func (*Plain) Warnln

func (p *Plain) Warnln(a ...any)

Warnln formats using the default formats for its operands and writes to the target output as a warning with a trailing newline.

func (*Plain) Write

func (p *Plain) Write(code, msg string, reset, nl bool)

Write writes a formatted log line with an optional reset code and newline

type Theme

type Theme struct {
	Success   string
	Highlight string
	Input     string

	Dimmed string
	Warn   string
	Error  string
}

Theme defines the ANSI color sequences used by the logger

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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