log

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2023 License: MIT Imports: 10 Imported by: 2

README

tieredlog

Simple console logger that is very heavily based on caarlos0/log.

Used heavily on ECSDeployer.

Usage

go get github.com/webdestroya/go-log

Major Differences

  • Adds a TRACE level
  • Allows more adjustment of indent levels and indent amount

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultTier int = 0

	TierPadding int = 2

	ErrorKey = "error"

	// how far into the line should keypairs be placed
	KeyPairPosition = 50
)
View Source
var ErrInvalidLevel = errors.New("invalid level")

ErrInvalidLevel is returned if the severity level is invalid.

View Source
var Now = time.Now

Now returns the current time.

View Source
var Strings = [...]string{
	FatalLevel: "⨯",
	ErrorLevel: "⨯",
	WarnLevel:  "•",
	InfoLevel:  "•",
	DebugLevel: "•",
	TraceLevel: "?",
}

Strings mapping.

View Source
var Styles = [...]lipgloss.Style{
	FatalLevel: lipgloss.NewStyle().Foreground(lipgloss.Color("9")).Bold(true),
	ErrorLevel: lipgloss.NewStyle().Foreground(lipgloss.Color("9")).Bold(true),
	WarnLevel:  lipgloss.NewStyle().Foreground(lipgloss.Color("11")).Bold(true),
	InfoLevel:  lipgloss.NewStyle().Foreground(lipgloss.Color("12")).Bold(true),
	DebugLevel: lipgloss.NewStyle().Foreground(lipgloss.Color("15")).Bold(true),
	TraceLevel: lipgloss.NewStyle().Foreground(lipgloss.Color("8")).Bold(false),
}

Styles mapping.

Functions

func Debug

func Debug(msg string)

Debug level message.

func Debugf

func Debugf(msg string, v ...interface{})

Debugf level formatted message.

func DecreasePadding

func DecreasePadding()

DecreasePadding decreases the padding 1 times.

func Error

func Error(msg string)

Error level message.

func Errorf

func Errorf(msg string, v ...interface{})

Errorf level formatted message.

func Fatal

func Fatal(msg string)

Fatal level message, followed by an exit.

func Fatalf

func Fatalf(msg string, v ...interface{})

Fatalf level formatted message, followed by an exit.

func IncreasePadding

func IncreasePadding()

IncreasePadding increases the padding 1 times.

func Info

func Info(msg string)

Info level message.

func Infof

func Infof(msg string, v ...interface{})

Infof level formatted message.

func NewContext

func NewContext(ctx context.Context, v Interface) context.Context

NewContext returns a new context with logger.

func ResetPadding

func ResetPadding()

ResetPadding resets the padding to default.

func SetLevel

func SetLevel(l Level)

SetLevel sets the log level. This is not thread-safe.

func SetLevelFromString

func SetLevelFromString(s string)

SetLevelFromString sets the log level from a string, panicing when invalid. This is not thread-safe.

func Trace

func Trace(msg string)

Trace level message.

func Tracef

func Tracef(msg string, v ...interface{})

Tracef level formatted message.

func Warn

func Warn(msg string)

Warn level message.

func Warnf

func Warnf(msg string, v ...interface{})

Warnf level formatted message.

Types

type Entry

type Entry struct {
	Logger  *Logger `json:"-"`
	Fields  Fields  `json:"fields"`
	Level   Level   `json:"level"`
	Message string  `json:"message"`
	Tier    int
	// contains filtered or unexported fields
}

Entry represents a single log entry.

func NewEntry

func NewEntry(log *Logger) *Entry

NewEntry returns a new entry for `log`.

func WithError

func WithError(err error) *Entry

WithError returns a new entry with the "error" set to `err`.

func WithField

func WithField(key string, value interface{}) *Entry

WithField returns a new entry with the `key` and `value` set.

func WithFields

func WithFields(fields Fielder) *Entry

WithFields returns a new entry with `fields` set.

func WithoutPadding

func WithoutPadding() *Entry

WithoutPadding returns a new entry with padding set to default.

func (*Entry) Debug

func (e *Entry) Debug(msg string)

Debug level message.

func (*Entry) Debugf

func (e *Entry) Debugf(msg string, v ...interface{})

Debugf level formatted message.

func (*Entry) DecreasePadding

func (e *Entry) DecreasePadding()

DecreasePadding decreases the padding 1 times.

func (*Entry) Error

func (e *Entry) Error(msg string)

Error level message.

func (*Entry) Errorf

func (e *Entry) Errorf(msg string, v ...interface{})

Errorf level formatted message.

func (*Entry) Fatal

func (e *Entry) Fatal(msg string)

Fatal level message, followed by an exit.

func (*Entry) Fatalf

func (e *Entry) Fatalf(msg string, v ...interface{})

Fatalf level formatted message, followed by an exit.

func (*Entry) IncreasePadding

func (e *Entry) IncreasePadding()

IncreasePadding increases the padding 1 times.

func (*Entry) Info

func (e *Entry) Info(msg string)

Info level message.

func (*Entry) Infof

func (e *Entry) Infof(msg string, v ...interface{})

Infof level formatted message.

func (*Entry) ResetPadding

func (e *Entry) ResetPadding()

ResetPadding resets the padding to default.

func (*Entry) Trace

func (e *Entry) Trace(msg string)

Trace level message.

func (*Entry) Tracef

func (e *Entry) Tracef(msg string, v ...interface{})

Tracef level formatted message.

func (*Entry) Warn

func (e *Entry) Warn(msg string)

Warn level message.

func (*Entry) Warnf

func (e *Entry) Warnf(msg string, v ...interface{})

Warnf level formatted message.

func (*Entry) WithError

func (e *Entry) WithError(err error) *Entry

WithError returns a new entry with the "error" set to `err`.

The given error may implement .Fielder, if it does the method will add all its `.Fields()` into the returned entry.

func (*Entry) WithField

func (e *Entry) WithField(key string, value interface{}) *Entry

WithField returns a new entry with the `key` and `value` set.

func (*Entry) WithFields

func (e *Entry) WithFields(fields Fielder) *Entry

WithFields returns a new entry with `fields` set.

func (*Entry) WithoutPadding

func (e *Entry) WithoutPadding() *Entry

WithoutPadding returns a new entry with padding set to default.

type Fielder

type Fielder interface {
	Fields() Fields
}

Fielder is an interface for providing fields to custom types.

type Fields

type Fields map[string]interface{}

Fields represents a map of entry level data used for structured logging.

func (Fields) Fields

func (f Fields) Fields() Fields

Fields implements Fielder.

func (Fields) Get

func (f Fields) Get(name string) interface{}

Get field value by name.

func (Fields) Names

func (f Fields) Names() (v []string)

Names returns field names sorted.

type Interface

type Interface interface {
	WithFields(Fielder) *Entry
	WithField(string, interface{}) *Entry
	WithError(error) *Entry
	WithoutPadding() *Entry
	Trace(string)
	Debug(string)
	Info(string)
	Warn(string)
	Error(string)
	Fatal(string)
	Tracef(string, ...interface{})
	Debugf(string, ...interface{})
	Infof(string, ...interface{})
	Warnf(string, ...interface{})
	Errorf(string, ...interface{})
	Fatalf(string, ...interface{})
	ResetPadding()
	IncreasePadding()
	DecreasePadding()
}

Interface represents the API of both Logger and Entry.

var Log Interface = New(os.Stderr)

singletons ftw?

func FromContext

func FromContext(ctx context.Context) Interface

FromContext returns the logger from context, or log.Log.

type Level

type Level int8

Level of severity.

const (
	InvalidLevel Level = iota - 1
	FatalLevel
	ErrorLevel
	WarnLevel
	InfoLevel
	DebugLevel
	TraceLevel
)

Log levels.

func MustParseLevel

func MustParseLevel(s string) Level

MustParseLevel parses level string or panics.

func ParseLevel

func ParseLevel(s string) (Level, error)

ParseLevel parses level string.

func (Level) String

func (l Level) String() string

String implementation.

type Logger

type Logger struct {
	Writer io.Writer
	Level  Level
	Tier   int
	// contains filtered or unexported fields
}

Logger represents a logger with configurable Level and Handler.

func New

func New(w io.Writer) *Logger

New creates a new logger.

func (*Logger) Debug

func (l *Logger) Debug(msg string)

Debug level message.

func (*Logger) Debugf

func (l *Logger) Debugf(msg string, v ...interface{})

Debugf level formatted message.

func (*Logger) DecreasePadding

func (l *Logger) DecreasePadding()

DecreasePadding decreases the padding 1 times.

func (*Logger) Error

func (l *Logger) Error(msg string)

Error level message.

func (*Logger) Errorf

func (l *Logger) Errorf(msg string, v ...interface{})

Errorf level formatted message.

func (*Logger) Fatal

func (l *Logger) Fatal(msg string)

Fatal level message, followed by an exit.

func (*Logger) Fatalf

func (l *Logger) Fatalf(msg string, v ...interface{})

Fatalf level formatted message, followed by an exit.

func (*Logger) IncreasePadding

func (l *Logger) IncreasePadding()

IncreasePadding increases the padding 1 times.

func (*Logger) Info

func (l *Logger) Info(msg string)

Info level message.

func (*Logger) Infof

func (l *Logger) Infof(msg string, v ...interface{})

Infof level formatted message.

func (*Logger) ResetPadding

func (l *Logger) ResetPadding()

ResetPadding resets the padding to default.

func (*Logger) Trace

func (l *Logger) Trace(msg string)

Trace level message.

func (*Logger) Tracef

func (l *Logger) Tracef(msg string, v ...interface{})

Tracef level formatted message.

func (*Logger) Warn

func (l *Logger) Warn(msg string)

Warn level message.

func (*Logger) Warnf

func (l *Logger) Warnf(msg string, v ...interface{})

Warnf level formatted message.

func (*Logger) WithError

func (l *Logger) WithError(err error) *Entry

WithError returns a new entry with the "error" set to `err`.

func (*Logger) WithField

func (l *Logger) WithField(key string, value interface{}) *Entry

WithField returns a new entry with the `key` and `value` set.

Note that the `key` should not have spaces in it - use camel case or underscores

func (*Logger) WithFields

func (l *Logger) WithFields(fields Fielder) *Entry

WithFields returns a new entry with `fields` set.

func (*Logger) WithoutPadding

func (l *Logger) WithoutPadding() *Entry

WithoutPadding returns a new entry with padding set to default.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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