basiclogger

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2025 License: MIT Imports: 5 Imported by: 0

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

View Source
const (
	LevelDebug = slog.LevelDebug
	LevelInfo  = slog.LevelInfo
	LevelWarn  = slog.LevelWarn
	LevelError = slog.LevelError
)

Variables

View Source
var Color = Colour
View Source
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 Debug

func Debug(msg string, a ...any)

Debug writes a message to the log at Debug level. Supports Printf-style placeholders.

func DebugE

func DebugE(err error)

DebugE logs an error to the log at Debug level.

func Error

func Error(msg string, a ...any)

Error writes a message to the log at Error level. Supports Printf-style placeholders.

func ErrorE

func ErrorE(err error)

ErrorE logs an error to the log at Info level.

func Fatal

func Fatal(msg string, code int, a ...any)

Fatal is equivalent

func FatalE

func FatalE(err error, code int)

func Info

func Info(msg string, a ...any)

Info writes a message to the log at Info level. Supports Printf-style placeholders.

func InfoE

func InfoE(err error)

InfoE logs an error to the log at Info level.

func SetColor

func SetColor(level slog.Level, colour colour)

SetColor is an alias to SetColour

func SetColors

func SetColors(cmap ColourMap)

SetColors is an alias for SetColours

func SetColour

func SetColour(level slog.Level, colour colour)

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.

func Warn

func Warn(msg string, a ...any)

Warn writes a message to the log at Warn level. Supports Printf-style placeholders.

func WarnE

func WarnE(err error)

WarnE logs an error to the log at Info level.

Types

type ColorMap

type ColorMap = ColourMap

type ColourMap

type ColourMap struct {
	Debug colour
	Info  colour
	Warn  colour
	Error colour
}

ColourMap is a struct used to map logging levels to their respective Colour

Jump to

Keyboard shortcuts

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