internal

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LogKeyMessage    = "msg"
	LogKeyName       = "logger"
	LogKeyTimestamp  = "time"
	LogKeyCaller     = "caller"
	LogKeyLevel      = "level"
	LogKeyContext    = "ctx"
	LogKeyStacktrace = "stacktrace"
)
View Source
const (
	Default = Color(iota)

	Black
	Red
	Green
	Yellow
	Blue
	Magenta
	Cyan
	Gray

	BoldBlack
	BoldRed
	BoldGreen
	BoldYellow
	BoldBlue
	BoldMagenta
	BoldCyan
	BoldGray
)

Variables

View Source
var (
	TmplColorFuncMap        template.FuncMap
	TmplColorFuncMapNonTerm template.FuncMap
)
View Source
var (
	FgColors   []string
	BgColors   []string
	ResetColor = "\x1b[39;49;22m"
)
View Source
var (
	TmplFuncMap = template.FuncMap{
		"cap":   Capped,
		"pad":   Padding,
		"lvl":   MakeLevelFunc(true),
		"join":  Join,
		"trace": Trace,
	}
	TmplFuncMapNonTerm = template.FuncMap{
		"cap":   Capped,
		"pad":   Padding,
		"lvl":   MakeLevelFunc(false),
		"join":  Join,
		"trace": Trace,
	}
)

Note: https://pkg.go.dev/text/template#hdr-Pipelines chainable argument should be the last parameter of any function

View Source
var ColorNames = map[string]Color{
	"black":   Black,
	"red":     Red,
	"green":   Green,
	"yellow":  Yellow,
	"blue":    Blue,
	"magenta": Magenta,
	"cyan":    Cyan,
	"gray":    Gray,

	"black_b":   BoldBlack,
	"red_b":     BoldRed,
	"green_b":   BoldGreen,
	"yellow_b":  BoldYellow,
	"blue_b":    BoldBlue,
	"magenta_b": BoldMagenta,
	"cyan_b":    BoldCyan,
	"gray_b":    BoldGray,
}

ColorNames names can be used for generic color function. quick foreground color function with same name is also availalbe in template

Functions

func Capped

func Capped(cap int, v interface{}) string

Capped truncate given value to specified length if cap > 0: with tailing "..." if truncated if cap < 0: with middle "..." if truncated

func Colored

func Colored(s interface{}, colorNames ...string) string

Colored takes 0, 1, or 2 color names when present, they should be in order of fgName, bgName

func ColoredWithCode

func ColoredWithCode(s interface{}, fg, bg Color) string

func ColoredWithName

func ColoredWithName(s interface{}, fgName, bgName string) string

func DebugShowcase

func DebugShowcase()

func IsTerminal

func IsTerminal(w io.Writer) bool

IsTerminal returns true if w writes to a terminal. Implementations adopted from github.com/go-kit/log/term

func Join

func Join(sep string, values ...interface{}) string

func MakeKVFunc

func MakeKVFunc(ignored utils.StringSet) func(Fields) string

func MakeLevelFunc

func MakeLevelFunc(term bool) func(padding int, kvs Fields) string

func MakeLevelPaddingFunc

func MakeLevelPaddingFunc(v interface{}) func(int) string

func MakeQuickColorFunc

func MakeQuickColorFunc(fg Color) func(s interface{}) string

func NewZapFormattedEncoder

func NewZapFormattedEncoder(cfg zapcore.EncoderConfig, formatter TextFormatter, isTerm bool) zapcore.Encoder

func NewZapWriterWrapper

func NewZapWriterWrapper(w io.Writer) zapcore.WriteSyncer

NewZapWriterWrapper similar to zapcore.AddSync with exported type

func NoopColored

func NoopColored(v interface{}, _ ...string) interface{}

func NoopQuickColor

func NoopQuickColor(v interface{}) interface{}

func Padding

func Padding(padding int, v interface{}) string

Padding example: `{{padding -6 value}}` "{{padding 10 value}}"

func Sprint

func Sprint(v interface{}) string

func Trace

func Trace(tid, sid, pid interface{}) string

Trace generate shortest possible tracing info string:

  • if trace ID is not available, return empty string
  • if span ID is same as trace ID, we assume parent ID is 0 and only returns traceID
  • if span ID is different from trace ID and parent ID is same as trace ID, we only returns trace ID and span ID

Types

type Color

type Color uint8

type Fields

type Fields map[string]interface{}

type KitTextLoggerAdapter

type KitTextLoggerAdapter struct {
	Formatter  TextFormatter
	Writer     io.Writer
	IsTerminal bool
}

KitTextLoggerAdapter implmenets go-kit's log.Logger with custom Formatter

func NewKitTextLoggerAdapter

func NewKitTextLoggerAdapter(writer io.Writer, formatter TextFormatter) *KitTextLoggerAdapter

func (*KitTextLoggerAdapter) Log

func (l *KitTextLoggerAdapter) Log(keyvals ...interface{}) error

type SliceArrayEncoder

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

SliceArrayEncoder implementing zapcore.PrimitiveArrayEncoder. It's used to apply zapcore's entry encoders like zapcore.NameEncoder

func (*SliceArrayEncoder) AppendBool

func (s *SliceArrayEncoder) AppendBool(v bool)

func (*SliceArrayEncoder) AppendByteString

func (s *SliceArrayEncoder) AppendByteString(v []byte)

func (*SliceArrayEncoder) AppendComplex128

func (s *SliceArrayEncoder) AppendComplex128(v complex128)

func (*SliceArrayEncoder) AppendComplex64

func (s *SliceArrayEncoder) AppendComplex64(v complex64)

func (*SliceArrayEncoder) AppendDuration

func (s *SliceArrayEncoder) AppendDuration(v time.Duration)

func (*SliceArrayEncoder) AppendFloat32

func (s *SliceArrayEncoder) AppendFloat32(v float32)

func (*SliceArrayEncoder) AppendFloat64

func (s *SliceArrayEncoder) AppendFloat64(v float64)

func (*SliceArrayEncoder) AppendInt

func (s *SliceArrayEncoder) AppendInt(v int)

func (*SliceArrayEncoder) AppendInt16

func (s *SliceArrayEncoder) AppendInt16(v int16)

func (*SliceArrayEncoder) AppendInt32

func (s *SliceArrayEncoder) AppendInt32(v int32)

func (*SliceArrayEncoder) AppendInt64

func (s *SliceArrayEncoder) AppendInt64(v int64)

func (*SliceArrayEncoder) AppendInt8

func (s *SliceArrayEncoder) AppendInt8(v int8)

func (*SliceArrayEncoder) AppendString

func (s *SliceArrayEncoder) AppendString(v string)

func (*SliceArrayEncoder) AppendTime

func (s *SliceArrayEncoder) AppendTime(v time.Time)

func (*SliceArrayEncoder) AppendUint

func (s *SliceArrayEncoder) AppendUint(v uint)

func (*SliceArrayEncoder) AppendUint16

func (s *SliceArrayEncoder) AppendUint16(v uint16)

func (*SliceArrayEncoder) AppendUint32

func (s *SliceArrayEncoder) AppendUint32(v uint32)

func (*SliceArrayEncoder) AppendUint64

func (s *SliceArrayEncoder) AppendUint64(v uint64)

func (*SliceArrayEncoder) AppendUint8

func (s *SliceArrayEncoder) AppendUint8(v uint8)

func (*SliceArrayEncoder) AppendUintptr

func (s *SliceArrayEncoder) AppendUintptr(v uintptr)

func (*SliceArrayEncoder) Latest

func (s *SliceArrayEncoder) Latest() interface{}

type TemplatedFormatter

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

func NewTemplatedFormatter

func NewTemplatedFormatter(tmpl string, fixedFields utils.StringSet, isTerm bool) *TemplatedFormatter

func (*TemplatedFormatter) Format

func (f *TemplatedFormatter) Format(kvs Fields, w io.Writer) error

type TerminalAware

type TerminalAware interface {
	IsTerminal() bool
}

type TextFormatter

type TextFormatter interface {
	Format(kvs Fields, w io.Writer) error
}

type ZapFormattedEncoder

type ZapFormattedEncoder struct {
	*zapcore.MapObjectEncoder
	Formatter  TextFormatter
	Config     *zapcore.EncoderConfig
	IsTerminal bool
}

ZapFormattedEncoder implements zapcore.Encoder. This encoder leverage go template system for render user defined log. Note: Unlike zapcore's JSONEncoder and ConsoleEncoder, this encoder focus on flexibility rather than performance.

When performance is crucial, JSON format of log should be used.

func (*ZapFormattedEncoder) Clone

func (enc *ZapFormattedEncoder) Clone() zapcore.Encoder

func (*ZapFormattedEncoder) EncodeEntry

func (enc *ZapFormattedEncoder) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error)

EncodeEntry implements zapcore.Encoder We use map and slice based encoders. Working with map and slice is necessary with go template based formatter. Map and slice operations is not the most performant approach, but it wouldn't be the bottleneck comparing to go template rendering

type ZapTerminalCore

type ZapTerminalCore struct {
	zapcore.Core
}

ZapTerminalCore implements TerminalAware and always returns true

func (ZapTerminalCore) IsTerminal

func (s ZapTerminalCore) IsTerminal() bool

type ZapWriterWrapper

type ZapWriterWrapper struct {
	io.Writer
}

ZapWriterWrapper implements zapcore.WriteSyncer and TerminalAware

func (ZapWriterWrapper) IsTerminal

func (s ZapWriterWrapper) IsTerminal() bool

func (ZapWriterWrapper) Sync

func (ZapWriterWrapper) Sync() error

Jump to

Keyboard shortcuts

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