ultraviolet

package module
v0.0.0-...-2a9c3f2 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2024 License: Unlicense Imports: 10 Imported by: 0

README




Ultraviolet is a simple, non-structured logging library for Go. Documentation is available on pkgo.dev Ultraviolet should not be considered stable or fit for production environments.




Alacritty screenshot showcasing Ultraviolet

package main

import (
	"math"

	"codeberg.org/Ast3nix/ultraviolet"
)

var uv = ultraviolet.New()

func main() {
	uv.Info(math.Pi, -1, 0, 1, uint32(2), true, false, []string{"hello to whoever is reading this"})

	uv.Subscope("level1").Critical("a")
	uv.Subscope("level1").Subscope("error").Error("a")
	uv.Subscope("level1").Subscope("warn").Warn("a")
	uv.Subscope("level1").Subscope("info").Info("a")
	uv.Subscope("level1").Subscope("debug").Debug("a")
}

Documentation

Overview

Package ultraviolet provides the main logging interface for Ultraviolet. Ultraviolet supports two environment variables — LOG_LEVELS and LOG.

LOG (separated by commas, supports wildcards) controls which log scopes are enabled. Subscopes are separated by the ScopeSeparator set in the logger (it's janky)

LOG_LEVELS (separated by commas) controls which log levels are enabled. The default levels are debug, info, warn, error, critical, all of which are on by default.

Index

Constants

This section is empty.

Variables

View Source
var DefaultPrefixes = map[string]string{
	"debug":    clk.Gray("%s") + "(" + clk.Gray("?") + "):",
	"info":     clk.BrightBlue("%s") + "(" + clk.BrightBlue("i") + "):",
	"warn":     clk.WithBrightYellow().Bold("%s") + "(" + clk.WithBrightYellow().Bold("#") + "):",
	"error":    clk.WithRed().Bold("%s") + "(" + clk.WithRed().Bold("!") + "):",
	"critical": clk.WithBrightMagenta().Bold("%s") + "(" + clk.WithBrightMagenta().Bold("CRITICAL") + "):",
}

DefaultPrefixes is a map of the default prefixes in the library

Functions

This section is empty.

Types

type Logger

type Logger struct {
	// Whether the logger will print anything
	Toggled bool

	// The scope of the logger's messages, is inferred from
	// the main module name by default.
	Scope string

	ThreadSafe bool // Makes ratelimiting thread-safe with `atomic` at the cost of performance

	TimestampFormat string // Enables timestamps. Accepts Go's standard timestamp format.
	TimestampIsUTC  bool   // Whether the timestamp should be in UTC

	// Enables the ratelimiting feature.
	RatelimitThroughput uint64 // The amount of messages allowed in a timeframe
	RatelimitInterval   uint64 // The desired timeframe divided by RatelimitThroughput (microseconds)

	ScopeSeparator string // Separator between subscopes

	// Prefixes for different log levels. The default log levels are:
	// debug, info, warn, error, critical
	// Custom log levels can also be added and used via uv.WithLevel()
	Prefixes       map[string]string
	UnsignedFormat string // Format for unsigned integers

	PIntFormat string // Format for positive integers
	NIntFormat string // Format for negative integers

	PFloatFormat string // Format for positive floats
	NFloatFormat string // Format for negative floats

	TrueFormat  string // Format for 'true'
	FalseFormat string // Format for 'false'

	// Log levels that are displayed (default: empty, empty = all levels)
	// The env variable LOG_LEVELS (separated by commas) overrides this
	EnabledLevels []string

	Output io.Writer // Stream to write to, os.Stderr by default

	// This function runs after uv.Critical() or uv.Fatal() is called. Does nothing by default.
	// The arguments are whatever was passed to Critical/Fatal
	ExitFunction func(args ...any)

	// This function is used to format complex objects such as maps and arrays.
	// Uses pkgo.dev/github.com/k0kubun/pp/v3 by default!
	ObjectFormatter func(obj any) string
	// contains filtered or unexported fields
}

Logger is the main logger structure. It can be cloned using uv.Clone() Note: "format" refers to the format used in Printf()

func ConvertSimple

func ConvertSimple(simple *SimpleLogger) *Logger

ConvertSimple turns a SimpleLogger into a Logger.

func New

func New() *Logger

New creates a new logger with the default settings

func NewSimple

func NewSimple() *Logger

NewSimple creates a logger with simplified default settings

func (*Logger) Clone

func (uv *Logger) Clone() *Logger

Clone creates an identical copy of this logger

func (*Logger) Critical

func (uv *Logger) Critical(args ...any)

Critical sends a message with the "critical" log level.

func (*Logger) Debug

func (uv *Logger) Debug(args ...any)

Debug sends a message with the "debug" log level.

func (*Logger) Error

func (uv *Logger) Error(args ...any)

Error sends a message with the "error" log level.

func (*Logger) Fatal

func (uv *Logger) Fatal(args ...any)

Fatal sends a message with the "critical" log level.

func (*Logger) Info

func (uv *Logger) Info(args ...any)

Info sends a message with the "info" log level.

func (*Logger) Subscope

func (uv *Logger) Subscope(scope string) *Logger

Subscope clones this logger and adds a subscope to it. If the logger's previous scope was inferred, it is replaced. Otherwise, it is appended using a separator from uv.ScopeSeparator

func (*Logger) Warn

func (uv *Logger) Warn(args ...any)

Warn sends a message with the "warn" log level.

func (*Logger) WithLevel

func (uv *Logger) WithLevel(level string, args ...any)

WithLevel prints a message with a specified log level.

type SimpleLogger

type SimpleLogger struct {
	Prefixes map[string]string // Prefixes for different log levels

	Scope          string // Scope of the logger's messages
	ScopeSeparator string // Separator between subscopes
	FloatFormat    string // Format for floats
	IntegerFormat  string // Format for integers
	BooleanFormat  string // Format for booleans

	ExitFunction func(args ...any)

	Output io.Writer
	// contains filtered or unexported fields
}

SimpleLogger is a simplified version of Logger which can be converted using ConvertSimple(), check documentation for Logger for more detailed explanations

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳