Documentation
¶
Overview ¶
Package basiclogger provides a simple interface for colourised, level-based logging
Basic logging is provided with no configuration required:
basiclogger.Debug("Debug message")
basiclogger.Info("Information message")
basiclogger.Warn("Warning message")
basiclogger.Error("Error message")
basiclogger.Fatal("Error message and exit with code 42", 42)
The *E variants of the above functions accept an [error] instead of a string:
err := errors.New("womp womp :(")
basiclogger.WarnE(err)
Some limited configuration options are supported.
Prepend a timestamp to all log messages:
basiclogger.SetTimestamps(true)
Use RFC3339 timestamps:
basiclogger.SetTimestampLayout(time.RFC3339)
Change the formatting of timestamps
basiclogger.SetTimestampFormat("[%s]")
Colours can be changed for a single logging level by SetColour using a Colour
basiclogger.SetColour(slog.LevelWarn, basicLogger.Colour.LightMagenta)
Or for all logging levels with SetColours using a ColourMap
basicLogger.SetColours(basiclogger.ColourMap{
Debug: basiclogger.Colour.LightBlack,
Info: basiclogger.Colour.White,
Warn: basiclogger.Colour.Yellow,
Error: basiclogger.Colour.Red,
})
Index ¶
- Constants
- Variables
- func Debug(msg string, a ...any)
- func DebugE(err error)
- func Error(msg string, a ...any)
- func ErrorE(err error)
- func Fatal(msg string, code int, a ...any)
- func FatalE(err error, code int)
- func Info(msg string, a ...any)
- func InfoE(err error)
- func SetColor(level slog.Level, colour colour)
- func SetColors(cmap ColourMap)
- func SetColour(level slog.Level, colour colour)
- func SetColours(cmap ColourMap)
- func SetTimestampFormat(fmt string)
- func SetTimestampLayout(layout string)
- func SetTimestamps(enabled bool)
- func Warn(msg string, a ...any)
- func WarnE(err error)
- type ColorMap
- type ColourMap
Constants ¶
const ( LevelDebug = slog.LevelDebug LevelInfo = slog.LevelInfo LevelWarn = slog.LevelWarn LevelError = slog.LevelError )
Variables ¶
var Color = Colour
var Colour = colours{
Black: colour{30},
Red: colour{31},
Green: colour{32},
Yellow: colour{33},
Blue: colour{34},
Magenta: colour{35},
Cyan: colour{36},
White: colour{37},
LightBlack: colour{90},
LightRed: colour{91},
LightGreen: colour{92},
LightYellow: colour{93},
LightBlue: colour{94},
LightMagenta: colour{95},
LightCyan: colour{96},
LightWhite: colour{97},
}
Colour values are defined as a custom unexported struct. The only colours supported currently are the basic ANSI 4-bit foreground colours.
Functions ¶
func SetColour ¶
SetColour configures the text colour for the provided logging [Level].
Levels are specified as slog.Level values
func SetColours ¶
func SetColours(cmap ColourMap)
SetColours will set the colours of all logging levels at once
func SetTimestampFormat ¶
func SetTimestampFormat(fmt string)
SetTimestampFormat defines the format string for timestamps. fmt must contain exactly one instance of the %s placeholder
By default this is "%s"
func SetTimestampLayout ¶
func SetTimestampLayout(layout string)
SetTimestampLayout defines the time.Layout string used for formatting the timestamp value.
By default this is time.DateTime
func SetTimestamps ¶
func SetTimestamps(enabled bool)
SetTimestamps enables or disables timestamp prefixes on all logging messages.
By default timestamps are disabled.