logger

package
v3.70.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: MIT Imports: 9 Imported by: 9

Documentation

Overview

Package logger provides a logger abstraction for writing log messages in configurable formats to different outputs, such as a console, plain text file, or a JSON file.

It is intended for internal use by buildkite-agent only.

Index

Constants

View Source
const (
	DateFormat = "2006-01-02 15:04:05"
)

Variables

View Source
var Discard = &ConsoleLogger{
	printer: &TextPrinter{
		Writer: io.Discard,
	},
}

Functions

func ColorsSupported

func ColorsSupported() bool

Types

type Buffer added in v3.24.0

type Buffer struct {
	Messages []string
	// contains filtered or unexported fields
}

Buffer is a Logger implementation intended for testing; messages are stored internally.

func NewBuffer added in v3.25.0

func NewBuffer() *Buffer

NewBuffer creates a new Buffer with Messages slice initialized. This makes it simpler to assert empty []string when no log messages have been sent; otherwise Messages would be nil.

func (*Buffer) Debug added in v3.24.0

func (b *Buffer) Debug(format string, v ...any)

func (*Buffer) Error added in v3.24.0

func (b *Buffer) Error(format string, v ...any)

func (*Buffer) Fatal added in v3.24.0

func (b *Buffer) Fatal(format string, v ...any)

func (*Buffer) Info added in v3.24.0

func (b *Buffer) Info(format string, v ...any)

func (*Buffer) Level added in v3.24.0

func (b *Buffer) Level() Level

func (*Buffer) Notice added in v3.24.0

func (b *Buffer) Notice(format string, v ...any)

func (*Buffer) SetLevel added in v3.24.0

func (b *Buffer) SetLevel(level Level)

func (*Buffer) Warn added in v3.24.0

func (b *Buffer) Warn(format string, v ...any)

func (*Buffer) WithFields added in v3.24.0

func (b *Buffer) WithFields(fields ...Field) Logger

type ConsoleLogger

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

func (*ConsoleLogger) Debug

func (l *ConsoleLogger) Debug(format string, v ...any)

func (*ConsoleLogger) Error

func (l *ConsoleLogger) Error(format string, v ...any)

func (*ConsoleLogger) Fatal

func (l *ConsoleLogger) Fatal(format string, v ...any)

func (*ConsoleLogger) Info

func (l *ConsoleLogger) Info(format string, v ...any)

func (*ConsoleLogger) Level

func (l *ConsoleLogger) Level() Level

func (*ConsoleLogger) Notice

func (l *ConsoleLogger) Notice(format string, v ...any)

func (*ConsoleLogger) SetLevel

func (l *ConsoleLogger) SetLevel(level Level)

SetLevel sets the level in the logger

func (*ConsoleLogger) Warn

func (l *ConsoleLogger) Warn(format string, v ...any)

func (*ConsoleLogger) WithFields

func (l *ConsoleLogger) WithFields(fields ...Field) Logger

WithFields returns a copy of the logger with the provided fields

type Field

type Field interface {
	Key() string
	String() string
}

func DurationField

func DurationField(key string, value time.Duration) Field

func IntField

func IntField(key string, value int) Field

func StringField

func StringField(key, value string) Field

type Fields

type Fields []Field

func (*Fields) Add

func (f *Fields) Add(fields ...Field)

func (*Fields) Get

func (f *Fields) Get(key string) []Field

type GenericField

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

func (GenericField) Key

func (f GenericField) Key() string

func (GenericField) String

func (f GenericField) String() string

type JSONPrinter

type JSONPrinter struct {
	Writer io.Writer
}

func NewJSONPrinter

func NewJSONPrinter(w io.Writer) *JSONPrinter

func (*JSONPrinter) Print

func (p *JSONPrinter) Print(level Level, msg string, fields Fields)

type Level

type Level int
const (
	DEBUG Level = iota
	NOTICE
	INFO
	WARN
	ERROR
	FATAL
)

func LevelFromString added in v3.36.0

func LevelFromString(s string) (Level, error)

func (Level) String

func (p Level) String() string

String returns the string representation of a logging level.

type Logger

type Logger interface {
	Debug(format string, v ...any)
	Error(format string, v ...any)
	Fatal(format string, v ...any)
	Notice(format string, v ...any)
	Warn(format string, v ...any)
	Info(format string, v ...any)

	WithFields(fields ...Field) Logger
	SetLevel(level Level)
	Level() Level
}

func NewConsoleLogger

func NewConsoleLogger(printer Printer, exitFn func(int)) Logger

type Printer

type Printer interface {
	Print(level Level, msg string, fields Fields)
}

type TextPrinter

type TextPrinter struct {
	Colors bool
	Writer io.Writer

	IsPrefixFn  func(Field) bool
	IsVisibleFn func(Field) bool
}

func NewTextPrinter

func NewTextPrinter(w io.Writer) *TextPrinter

func (*TextPrinter) Print

func (l *TextPrinter) Print(level Level, msg string, fields Fields)

Jump to

Keyboard shortcuts

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