text

package
v1.0.100 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2021 License: MIT Imports: 7 Imported by: 1

Documentation

Overview

Package text implements a text formatter which implements the Formatter.

Index

Constants

View Source
const (
	FullHeader = "{{time}} {{level}} {{file}}:{{line}} {{pkg}}.{{func}} " +
		"{{prefix}}[{{context}}] {{msg}}\n"
	CompactHeader = "{{time:time.us}} {{level}} {{file:1}}:{{line}} " +
		"{{pkg}}.{{func}} {{prefix}}[{{context}}] {{msg}}\n"
	SyslogHeader = "{{file:1}}:{{line}} {{pkg}}.{{func}} {{prefix}}" +
		"[{{context}}] {{msg}}\n"
)

All predefined headers here.

Variables

This section is empty.

Functions

This section is empty.

Types

type Color added in v0.2.0

type Color int

The Color defines the color type.

const (
	Black Color = iota + 30
	Red
	Green
	Yellow
	Blue
	Magenta
	Cyan
	White
)

Basic colors here.

const (
	BrightBlack Color = iota + 90
	BrightRed
	BrightGreen
	BrightYellow
	BrightBlue
	BrightMagenta
	BrightCyan
	BrightWhite
)

Bright colors here.

type Config added in v0.3.0

type Config struct {
	// Header is the format specifier of a text formatter.
	// It is used to specify which and how the fields of a Record to be formatted.
	// The pattern of a field specifier is {{<name>[:property][%fmtstr]}}.
	// e.g. {{level:char}}, {{line%05d}}, {{pkg:1}}, {{context:list%40s}}
	// All fields have support for the fmtstr. If the fmtstr is NOT the default
	// one of a field, it will be passed to fmt.Sprintf to format the field and
	// this affects the performance a little.
	// The supported properties vary with fields.
	// All supported fields are as the follows:
	//   name    | supported property       | defaults     | property examples
	// ----------+--------------------------+--------------+------------------------
	//   time    | <date|time>[.ms|.us|.ns] | "date.us" %s | "date.ns", "time"
	//           | layout that is supported |              | time.RFC3339Nano
	//           |   by the time package    |              | "02 Jan 06 15:04 -0700"
	//   level   | <full|char>              | "full"    %s | "full", "char"
	//   file    | <lastSegs>               | 0         %s | 0, 1, 2, ...
	//   line    |                          |           %d |
	//   pkg     | <lastSegs>               | 0         %s | 0, 1, 2, ...
	//   func    | <lastSegs>               | 0         %s | 0, 1, 2, ...
	//   prefix  |                          |           %s |
	//   context | <pair|list>              | "pair"    %s | "pair", "list"
	//   msg     |                          |           %s |
	// If Header is not specified, FullHeader is used.
	Header string
	// MinBufSize is the initial size of the internal buf of a formatter.
	// MinBufSize must NOT be negative. If it is not specified, 256 is used.
	MinBufSize int
	// ColorMap is used to remap the color of each level.
	// By default, the color of Trace, Debug and Info is Green, the color of Warn
	// is Yellow, the color of Error and Fatal is Red. The color of a marked log
	// is Magenta despite of its level.
	// The color of a level is left to be unchanged if it is not in the map.
	ColorMap map[iface.Level]Color
	// Coloring specifies whether colorization is enabled.
	Coloring bool
}

A Config is used to configure a text formatter.

func NewConfig added in v0.3.0

func NewConfig() Config

type Formatter

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

A Formatter implements the interface iface.Formatter.

All methods of a Formatter are concurrency safe. A Formatter MUST be created with New.

func New

func New(config Config) *Formatter

New creates a new Formatter with the config.

func (*Formatter) Color added in v0.4.0

func (formatter *Formatter) Color(level iface.Level) Color

Color returns the color of the level in the Formatter.

func (*Formatter) Coloring added in v0.7.0

func (formatter *Formatter) Coloring() bool

Coloring returns whether colorization is enabled in the Formatter.

func (*Formatter) DisableColoring added in v0.7.0

func (formatter *Formatter) DisableColoring()

DisableColoring disables colorization in the Formatter.

func (*Formatter) EnableColoring added in v0.7.0

func (formatter *Formatter) EnableColoring()

EnableColoring enables colorization in the Formatter.

func (*Formatter) Format

func (formatter *Formatter) Format(record *iface.Record) []byte

Format implements the interface Formatter. It formats a Record.

func (*Formatter) Header added in v0.4.0

func (formatter *Formatter) Header() string

Header returns the header of the Formatter.

func (*Formatter) MapColors added in v0.3.0

func (formatter *Formatter) MapColors(colorMap map[iface.Level]Color)

MapColors maps the color of levels in the Formatter according to the colorMap. The color of a level is left to be unchanged if it is not in the map.

func (*Formatter) MarkedColor added in v0.4.0

func (formatter *Formatter) MarkedColor() Color

MarkedColor returns the color of a log that is marked.

func (*Formatter) MinBufSize added in v0.4.0

func (formatter *Formatter) MinBufSize() int

MinBufSize returns the min buf size of the Formatter.

func (*Formatter) SetColor added in v0.2.0

func (formatter *Formatter) SetColor(level iface.Level, color Color)

SetColor sets the color of the level in the Formatter.

func (*Formatter) SetHeader

func (formatter *Formatter) SetHeader(header string)

SetHeader sets the header of the Formatter. For details of all supported fields in a header, see the comment of Config.

func (*Formatter) SetMarkedColor added in v0.3.0

func (formatter *Formatter) SetMarkedColor(color Color)

SetMarkedColor sets the color of a log that is marked.

func (*Formatter) SetMinBufSize added in v0.4.0

func (formatter *Formatter) SetMinBufSize(size int)

SetMinBufSize sets the min buf size of the Formatter. The size must NOT be negative. If the size is 0, 256 is used.

Jump to

Keyboard shortcuts

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