logftext

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2021 License: MIT Imports: 6 Imported by: 1

README

logf Appender and Encoder for colored text logs

GoDoc Build Status Go Report Status Coverage Status

Package logftext provides logf Appender and Encoder for colored text logs.

Example

The following example creates the new logf logger with logftext Encoder.

package main

import (
    "errors"
    "os"
    "runtime"

    "github.com/ssgreg/logf"
    "github.com/ssgreg/logftext"
)

func main() {
    // Create ChannelWriter with text Encoder.
    writer, writerClose := logf.NewChannelWriter(logf.ChannelWriterConfig{
        Appender: logftext.NewAppender(os.Stdout, logftext.EncoderConfig{}),
    })
    defer writerClose()

    // Create Logger with ChannelWriter.
    logger := logf.NewLogger(logf.LevelInfo, writer).WithCaller().WithName("main")

    logger.Info("got cpu info", logf.Int("count", runtime.NumCPU()))
    logger.Error("error example", logf.Error(errors.New("failed to do nothing")))
}

The output example is: Alt text

TODOs

  • Handle terminals with a light backgrounds

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NewEncoder = encoderGetter(
	func(cfg EncoderConfig) logf.Encoder {
		cfg = cfg.WithDefaults()

		return &encoder{
			cfg,
			logf.NewJSONTypeEncoderFactory(logf.JSONEncoderConfig{
				EncodeTime:     cfg.EncodeTime,
				EncodeDuration: cfg.EncodeDuration,
				EncodeError:    cfg.EncodeError,
			}),
			nil,
			logf.NewCache(100),
			0,
			EscapeSequence{*cfg.NoColor},
		}
	},
)

NewEncoder creates the new instance of the text Encoder with the given EncoderConfig. It delegates field encoding to the internal json Encoder.

It's a caller responsibility to handle colored output in a prover way. The best choice here is to use NewAppender function instead of of a creation of Encoder by Yourselves.

Functions

func CheckNoColor

func CheckNoColor() bool

CheckNoColor checks for NO_COLORS environment variable to disable color output.

All command-line software which outputs text with ANSI color added should check for the presence of a NO_COLOR environment variable that, when present (regardless of its value), prevents the addition of ANSI color.

func EnableSeqTTY

func EnableSeqTTY(f *os.File, flag bool) bool

EnableSeqTTY enables possibility to use escape sequences in TTY if possible.

func NewAppender

func NewAppender(w io.Writer, cfg EncoderConfig) logf.Appender

NewAppender returns a new logf.WriteAppender with the given Writer and EncoderConfig.

NewAppender is safe to use for colored logs.

Types

type EncoderConfig

type EncoderConfig struct {
	// NoColor enables/disables colored output.
	NoColor *bool

	DisableFieldName   bool
	DisableFieldCaller bool

	EncodeTime     logf.TimeEncoder
	EncodeDuration logf.DurationEncoder
	EncodeError    logf.ErrorEncoder
	EncodeCaller   logf.CallerEncoder
}

EncoderConfig allows to configure text Encoder.

func (EncoderConfig) WithDefaults

func (c EncoderConfig) WithDefaults() EncoderConfig

WithDefaults returns the new config in which all uninitialized fields are filled with their default values.

type EscapeCode

type EscapeCode int8

EscapeCode represents a single code in the ANSI escape sequences.

These sequences define functions that change display graphics, control cursor movement, and reassign keys.

ANSI escape sequence is a sequence of ASCII characters, the first two of which are the ASCII "Escape" character 27 (1Bh) and the left-bracket character " [ " (5Bh). The character or characters following the escape and left-bracket characters specify an alphanumeric code that controls a keyboard or display function.

const (
	EscReset EscapeCode = iota + 0
	EscBold
	EscFaint
	EscItalic
	EscUnderline
	EscSlowBlink
	EscRapidBlink
	EscReverse
	EscConseal
	EscCrossedOut
)

Text colors.

const (
	EscBlack EscapeCode = iota + 30
	EscRed
	EscGreen
	EscYellow
	EscBlue
	EscMagenta
	EscCyan
	EscWhite
)

Text colors.

const (
	EscBgBlack EscapeCode = iota + 40
	EscBgRed
	EscBgGreen
	EscBgYellow
	EscBgBlue
	EscBgMagenta
	EscBgCyan
	EscBgWhite
)

Background colors.

const (
	EscBrightBlack EscapeCode = iota + 90
	EscBrightRed
	EscBrightGreen
	EscBrightYellow
	EscBrightBlue
	EscBrightMagenta
	EscBrightCyan
	EscBrightWhite
)

Bright text colors.

const (
	EscBrightBgBlack EscapeCode = iota + 100
	EscBrightBgRed
	EscBrightBgGreen
	EscBrightBgYellow
	EscBrightBgBlue
	EscBrightBgMagenta
	EscBrightBgCyan
	EscBrightBgWhite
)

Bright background colors.

type EscapeSequence

type EscapeSequence struct {
	NoColor bool
}

EscapeSequence allows to construct escape sequences.

func (EscapeSequence) At

func (es EscapeSequence) At(buf *logf.Buffer, clr EscapeCode, fn func())

At calls the given fn, wrapped with the escape sequence, based on the given code.

func (EscapeSequence) At2

func (es EscapeSequence) At2(buf *logf.Buffer, clr1, clr2 EscapeCode, fn func())

At2 calls the given fn, wrapped with the escape sequence, based on the given codes.

func (EscapeSequence) At3

func (es EscapeSequence) At3(buf *logf.Buffer, clr1, clr2, clr3 EscapeCode, fn func())

At3 calls the given fn, wrapped with the escape sequence, based on the given codes.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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