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 ¶
- Variables
- func Debug(msg string)
- func Debugf(msg string, v ...interface{})
- func DecreasePadding()
- func Error(msg string)
- func Errorf(msg string, v ...interface{})
- func Fatal(msg string)
- func Fatalf(msg string, v ...interface{})
- func IncreasePadding()
- func Info(msg string)
- func Infof(msg string, v ...interface{})
- func NewContext(ctx context.Context, v Interface) context.Context
- func ResetPadding()
- func SetLevel(l Level)
- func SetLevelFromString(s string)
- func Warn(msg string)
- func Warnf(msg string, v ...interface{})
- type Entry
- func (e *Entry) Debug(msg string)
- func (e *Entry) Debugf(msg string, v ...interface{})
- func (e *Entry) DecreasePadding()
- func (e *Entry) Error(msg string)
- func (e *Entry) Errorf(msg string, v ...interface{})
- func (e *Entry) Fatal(msg string)
- func (e *Entry) Fatalf(msg string, v ...interface{})
- func (e *Entry) IncreasePadding()
- func (e *Entry) Info(msg string)
- func (e *Entry) Infof(msg string, v ...interface{})
- func (e *Entry) ResetPadding()
- func (e *Entry) Warn(msg string)
- func (e *Entry) Warnf(msg string, v ...interface{})
- func (e *Entry) WithError(err error) *Entry
- func (e *Entry) WithField(key string, value interface{}) *Entry
- func (e *Entry) WithoutPadding() *Entry
- type Interface
- type Level
- type Logger
- func (l *Logger) Debug(msg string)
- func (l *Logger) Debugf(msg string, v ...interface{})
- func (l *Logger) DecreasePadding()
- func (l *Logger) Error(msg string)
- func (l *Logger) Errorf(msg string, v ...interface{})
- func (l *Logger) Fatal(msg string)
- func (l *Logger) Fatalf(msg string, v ...interface{})
- func (l *Logger) IncreasePadding()
- func (l *Logger) Info(msg string)
- func (l *Logger) Infof(msg string, v ...interface{})
- func (l *Logger) ResetPadding()
- func (l *Logger) Warn(msg string)
- func (l *Logger) Warnf(msg string, v ...interface{})
- func (l *Logger) WithError(err error) *Entry
- func (l *Logger) WithField(key string, value interface{}) *Entry
- func (l *Logger) WithoutPadding() *Entry
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidLevel = errors.New("invalid level")
ErrInvalidLevel is returned if the severity level is invalid.
var Now = time.Now
Now returns the current time.
var Strings = [...]string{
DebugLevel: "•",
InfoLevel: "•",
WarnLevel: "•",
ErrorLevel: "⨯",
FatalLevel: "⨯",
}
Strings mapping.
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 Fatalf ¶
func Fatalf(msg string, v ...interface{})
Fatalf level formatted message, followed by an exit.
func NewContext ¶
NewContext returns a new context with logger.
func SetLevelFromString ¶
func SetLevelFromString(s string)
SetLevelFromString sets the log level from a string, panicing when invalid. This is not thread-safe.
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 WithoutPadding ¶ added in v0.2.0
func WithoutPadding() *Entry
WithoutPadding returns a new entry with padding set to default.
func (*Entry) DecreasePadding ¶
func (e *Entry) DecreasePadding()
DecreasePadding decreases the padding 1 times.
func (*Entry) IncreasePadding ¶
func (e *Entry) IncreasePadding()
IncreasePadding increases the padding 1 times.
func (*Entry) ResetPadding ¶
func (e *Entry) ResetPadding()
ResetPadding resets the padding to default.
func (*Entry) WithError ¶
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) WithoutPadding ¶ added in v0.2.0
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.
func FromContext ¶
FromContext returns the logger from context, or log.Log.
type Level ¶
type Level int
Level of severity.
Log levels.
func MustParseLevel ¶
MustParseLevel parses level string or panics.
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 (*Logger) DecreasePadding ¶
func (l *Logger) DecreasePadding()
DecreasePadding decreases the padding 1 times.
func (*Logger) IncreasePadding ¶
func (l *Logger) IncreasePadding()
IncreasePadding increases the padding 1 times.
func (*Logger) ResetPadding ¶
func (l *Logger) ResetPadding()
ResetPadding resets the padding to default.
func (*Logger) WithField ¶
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
WithoutPadding returns a new entry with padding set to default.