term

package
v0.0.0-...-79843c9 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2022 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package term provides tools for logging to a terminal.

Deprecated: Use github.com/go-kit/log/term instead.

Index

Examples

Constants

View Source
const (
	Default = term.Default

	Black       = term.Black
	DarkRed     = term.DarkRed
	DarkGreen   = term.DarkGreen
	Brown       = term.Brown
	DarkBlue    = term.DarkBlue
	DarkMagenta = term.DarkMagenta
	DarkCyan    = term.DarkCyan
	Gray        = term.Gray

	DarkGray = term.DarkGray
	Red      = term.Red
	Green    = term.Green
	Yellow   = term.Yellow
	Blue     = term.Blue
	Magenta  = term.Magenta
	Cyan     = term.Cyan
	White    = term.White
)

ANSI colors.

Variables

This section is empty.

Functions

func IsTerminal

func IsTerminal(w io.Writer) bool

IsTerminal returns true if w writes to a terminal.

func NewColorLogger

func NewColorLogger(w io.Writer, newLogger func(io.Writer) log.Logger, color func(keyvals ...interface{}) FgBgColor) log.Logger

NewColorLogger returns a Logger which writes colored logs to w. ANSI color codes for the colors returned by color are added to the formatted output from the Logger returned by newLogger and the combined result written to w.

func NewColorWriter

func NewColorWriter(w io.Writer) io.Writer

NewColorWriter returns an io.Writer that writes to w and provides cross platform support for ANSI color codes. If w is not a terminal it is returned unmodified.

func NewLogger

func NewLogger(w io.Writer, newLogger func(io.Writer) log.Logger, color func(keyvals ...interface{}) FgBgColor) log.Logger

NewLogger returns a Logger that takes advantage of terminal features if possible. Log events are formatted by the Logger returned by newLogger. If w is a terminal each log event is colored according to the color function.

Example (LevelColors)
package main

import (
	"os"

	"github.com/heiyhia/gokit/log"
	"github.com/heiyhia/gokit/log/term"
)

func main() {
	// Color by level value
	colorFn := func(keyvals ...interface{}) term.FgBgColor {
		for i := 0; i < len(keyvals)-1; i += 2 {
			if keyvals[i] != "level" {
				continue
			}
			switch keyvals[i+1] {
			case "debug":
				return term.FgBgColor{Fg: term.DarkGray}
			case "info":
				return term.FgBgColor{Fg: term.Gray}
			case "warn":
				return term.FgBgColor{Fg: term.Yellow}
			case "error":
				return term.FgBgColor{Fg: term.Red}
			case "crit":
				return term.FgBgColor{Fg: term.Gray, Bg: term.DarkRed}
			default:
				return term.FgBgColor{}
			}
		}
		return term.FgBgColor{}
	}

	logger := term.NewLogger(os.Stdout, log.NewJSONLogger, colorFn)

	logger.Log("level", "warn", "msg", "yellow")
	logger.Log("level", "debug", "msg", "dark gray")
}
Output:

Example (RedErrors)
package main

import (
	"errors"
	"os"

	"github.com/heiyhia/gokit/log"
	"github.com/heiyhia/gokit/log/term"
)

func main() {
	// Color errors red
	colorFn := func(keyvals ...interface{}) term.FgBgColor {
		for i := 1; i < len(keyvals); i += 2 {
			if _, ok := keyvals[i].(error); ok {
				return term.FgBgColor{Fg: term.White, Bg: term.Red}
			}
		}
		return term.FgBgColor{}
	}

	logger := term.NewLogger(os.Stdout, log.NewLogfmtLogger, colorFn)

	logger.Log("msg", "default color", "err", nil)
	logger.Log("msg", "colored because of error", "err", errors.New("coloring error"))
}
Output:

Types

type Color

type Color = term.Color

Color represents an ANSI color. The zero value is Default.

type FgBgColor

type FgBgColor = term.FgBgColor

FgBgColor represents a foreground and background color.

Jump to

Keyboard shortcuts

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