Documentation
¶
Overview ¶
Package log 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/kumose-go/clog"
)
func main() {
err := errors.New("boom")
clog.WithError(err).Error("upload failed")
}
Example (Structured) ¶
Structured logging is supported with fields, and is recommended over the formatted message variants.
package main
import (
"github.com/kumose-go/clog"
)
func main() {
clog.WithField("user", "Tobo").Info("logged in")
}
Example (Unstructured) ¶
Unstructured logging is supported, but not recommended since it is hard to query.
package main
import (
"github.com/kumose-go/clog"
)
func main() {
clog.Infof("%s logged in", "Tobi")
}
Index ¶
- Variables
- func Debug(msg string)
- func Debugf(msg string, v ...any)
- func DecreasePadding()
- func Error(msg string)
- func Errorf(msg string, v ...any)
- func Fatal(msg string)
- func Fatalf(msg string, v ...any)
- func IncreasePadding()
- func Info(msg string)
- func Infof(msg string, v ...any)
- 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 ...any)
- type Entry
- func (e *Entry) Debug(msg string)
- func (e *Entry) Debugf(msg string, v ...any)
- func (e *Entry) DecreasePadding()
- func (e *Entry) Error(msg string)
- func (e *Entry) Errorf(msg string, v ...any)
- func (e *Entry) Fatal(msg string)
- func (e *Entry) Fatalf(msg string, v ...any)
- func (e *Entry) IncreasePadding()
- func (e *Entry) Info(msg string)
- func (e *Entry) Infof(msg string, v ...any)
- func (e *Entry) ResetPadding()
- func (e *Entry) Warn(msg string)
- func (e *Entry) Warnf(msg string, v ...any)
- func (e *Entry) WithError(err error) *Entry
- func (e *Entry) WithField(key string, value any) *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 ...any)
- func (l *Logger) DecreasePadding()
- func (l *Logger) Error(msg string)
- func (l *Logger) Errorf(msg string, v ...any)
- func (l *Logger) Fatal(msg string)
- func (l *Logger) Fatalf(msg string, v ...any)
- func (l *Logger) IncreasePadding()
- func (l *Logger) Info(msg string)
- func (l *Logger) Infof(msg string, v ...any)
- func (l *Logger) ResetPadding()
- func (l *Logger) Warn(msg string)
- func (l *Logger) Warnf(msg string, v ...any)
- func (l *Logger) WithError(err error) *Entry
- func (l *Logger) WithField(key string, value any) *Entry
- func (l *Logger) WithoutPadding() *Entry
- type Style
- func (s Style) Background(style pterm.RGB) Style
- func (s Style) Bold(f bool) Style
- func (s Style) Foreground(style pterm.RGB) Style
- func (s Style) PaddingLeft(n int) Style
- func (s Style) PaddingRight(n int) Style
- func (s Style) Render(strs ...string) string
- func (s Style) SetString(strs ...string) Style
- func (s Style) String() string
- type Theme
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( FgWhith = pterm.NewRGBStyle(pterm.NewRGB(255, 255, 255)) NoneColorTheme = Theme{ DebugLevel: NewStyle(FgWhith), InfoLevel: NewStyle(FgWhith), WarnLevel: NewStyle(FgWhith), ErrorLevel: NewStyle(FgWhith), FatalLevel: NewStyle(FgWhith), } DefaultTheme = Theme{ DebugLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(94, 101, 85))).Bold(true), InfoLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(4, 124, 140))).Bold(true), WarnLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(194, 156, 5))).Bold(true), ErrorLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(141, 35, 6))).Bold(true), FatalLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(246, 60, 9))).Bold(true), } CoolTheme = Theme{ DebugLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(120, 144, 156))), InfoLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(38, 198, 218))), WarnLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(255, 179, 0))), ErrorLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(244, 67, 54))), FatalLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(211, 47, 47))), } WarmTheme = Theme{ DebugLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(189, 189, 189))), InfoLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(255, 202, 40))), WarnLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(255, 112, 67))), ErrorLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(211, 47, 47))), FatalLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(183, 28, 28))), } DarkContrastTheme = Theme{ DebugLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(130, 130, 130))), InfoLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(0, 188, 212))), WarnLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(255, 235, 59))), ErrorLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(255, 87, 34))), FatalLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(244, 67, 54))), } VividTheme = Theme{ DebugLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(102, 187, 106))), InfoLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(3, 169, 244))), WarnLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(255, 193, 7))), ErrorLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(244, 67, 54))), FatalLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(156, 39, 176))), } BusinessTheme = Theme{ DebugLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(102, 102, 153))), InfoLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(51, 102, 204))), WarnLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(153, 102, 255))), ErrorLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(204, 51, 102))), FatalLevel: NewStyle(pterm.NewRGBStyle(pterm.NewRGB(102, 0, 153))), } )
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 = DefaultTheme
Styles mapping.
Functions ¶
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 ¶
Entry represents a single log entry.
func WithoutPadding ¶
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 ¶
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 clog.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 ¶
WithoutPadding returns a new entry with padding set to default.
type Style ¶ added in v0.6.0
type Style struct {
// contains filtered or unexported fields
}
