clog

package
v1.30.4 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2022 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package clog provides rich error types and logging helpers for coder-cli.

clog encourages returning error types rather than logging them and failing with os.Exit as they happen. Error, Fatal, and Warn allow downstream functions to return errors with rich formatting information while preserving the original, single-line error chain.

Index

Constants

View Source
const BlankLine = ""

BlankLine is an empty string meant to be used in CLIMessage and CLIError construction.

Variables

This section is empty.

Functions

func Bold

func Bold(a string) string

Bold provides a convenience wrapper around color.New for brevity when logging.

func Causef

func Causef(format string, a ...interface{}) string

Causef formats according to the given format specifier and prepends a bolded "cause: " header.

func Hintf

func Hintf(format string, a ...interface{}) string

Hintf formats according to the given format specifier and prepends a bolded "hint: " header.

func Log

func Log(err error)

Log logs the given error to stderr, defaulting to "fatal" if the error is not a CLIError. If the error is a CLIError, the plain error chain is ignored and the CLIError is logged on its own.

func LogInfo

func LogInfo(header string, lines ...string)

LogInfo prints the given info message to stderr.

func LogSuccess

func LogSuccess(header string, lines ...string)

LogSuccess prints the given info message to stderr.

func LogWarn

func LogWarn(header string, lines ...string)

LogWarn prints the given warn message to stderr.

func SetOutput added in v1.17.0

func SetOutput(w io.Writer)

SetOutput sets the package-level writer target for log functions.

func Tipf

func Tipf(format string, a ...interface{}) string

Tipf formats according to the given format specifier and prepends a bolded "tip: " header.

Types

type CLIError

type CLIError struct {
	CLIMessage
	// contains filtered or unexported fields
}

CLIError wraps a CLIMessage and allows consumers to treat it as a normal error.

func Error

func Error(header string, lines ...string) CLIError

Error creates an error with the level "error".

func Fatal

func Fatal(header string, lines ...string) CLIError

Fatal creates an error with the level "fatal".

type CLIMessage

type CLIMessage struct {
	Level  string
	Color  color.Attribute
	Header string
	Lines  []string
}

CLIMessage provides a human-readable message for CLI errors and messages.

func (CLIMessage) String

func (m CLIMessage) String() string

String formats the CLI message for consumption by a human.

type ErrGroup

type ErrGroup interface {
	Go(f func() error)
	Wait() error
}

ErrGroup wraps the /x/sync/errgroup.(Group) and adds clog logging and rich error propagation.

Take for example, a case in which we are concurrently stopping a slice of workspaces. In this case, we want to log errors as they happen, not pass them through the callstack as errors. When the operations complete, we want to log how many, if any, failed. The caller is still expected to handle success and info logging.

func LoggedErrGroup

func LoggedErrGroup() ErrGroup

LoggedErrGroup gives an error group with error logging and error propagation handled automatically.

Jump to

Keyboard shortcuts

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