log

package module
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2023 License: MIT Imports: 10 Imported by: 37

README

log

A Colorful CLI logger.

Build Status Coverage Status

Acknowledgements

This package is a very refactored version of the great apex/log.

Changes are mostly related to reducing the number of dependencies and removing things I don't use, focusing in using it as CLI pretty logger, mainly for GoReleaser.

Make sure to check out the original work by tj!

Documentation

Overview

Package logger implements a simple and colorful CLI logger that can be used to pretty-print output without too much effort.

It has some structured logging functionality and simple "indent" utilities to better organize output, and that's about it.

Example (Errors)

Errors are passed to WithError(), populating the "error" field.

package main

import (
	"errors"

	"github.com/caarlos0/log"
)

func main() {
	err := errors.New("boom")
	log.WithError(err).Error("upload failed")
}
Output:

Example (Structured)

Structured logging is supported with fields, and is recommended over the formatted message variants.

package main

import (
	"github.com/caarlos0/log"
)

func main() {
	log.WithField("user", "Tobo").Info("logged in")
}
Output:

Example (Unstructured)

Unstructured logging is supported, but not recommended since it is hard to query.

package main

import (
	"github.com/caarlos0/log"
)

func main() {
	log.Infof("%s logged in", "Tobi")
}
Output:

Index

Examples

Constants

This section is empty.

Variables

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{
	DebugLevel: "•",
	InfoLevel:  "•",
	WarnLevel:  "•",
	ErrorLevel: "⨯",
	FatalLevel: "⨯",
}

Strings mapping.

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

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 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
	Level   Level
	Message string
	Padding int
	Fields  *orderedmap.OrderedMap[string, any]
}

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 WithoutPadding added in v0.2.0

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) 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) WithoutPadding added in v0.2.0

func (e *Entry) WithoutPadding() *Entry

WithoutPadding returns a new entry with padding set to default.

type Interface

type Interface interface {
	WithField(string, interface{}) *Entry
	WithError(error) *Entry
	WithoutPadding() *Entry
	Debug(string)
	Info(string)
	Warn(string)
	Error(string)
	Fatal(string)
	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 int

Level of severity.

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

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
	Padding 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) 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) WithoutPadding added in v0.2.0

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