lol

package
v0.8.7 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2025 License: Unlicense Imports: 9 Imported by: 0

README

lol

location of log

This is a very simple, but practical library for logging in applications. Its main feature is printing source code locations to make debugging easier.

terminals

Due to how so few terminals actually support source location hyperlinks, pretty much tilix and intellij terminal are the only two that really provide adequate functionality, this logging library defaults to output format that works best with intellij. As such, the terminal is aware of the CWD and the code locations printed are relative, as required to get the hyperlinkization from this terminal. Handling support for Tilix requires more complications and due to advances with IntelliJ's handling it is not practical to support any other for this purpose. Users of this library can always fall back to manually interpreting and accessing the relative file path to find the source of a log.

In addition, due to this terminal's slow rendering of long lines, long log strings are automatically broken into 80 character lines, and if there is comma separators in the line, the line is broken at the comma instead of at column80. This works perfectly for this purpose.

Documentation

Overview

Package lol (log of location) is a simple logging library that prints a high precision unix timestamp and the source location of a log print to make tracing errors simpler. Includes a set of logging levels and the ability to filter out higher log levels for a more quiet output.

Index

Constants

View Source
const (
	Off = iota
	Fatal
	Error
	Warn
	Info
	Debug
	Trace
)

Variables

View Source
var (
	// Writer can be swapped out for any io.*Writer* that you want to use instead of stdout.
	Writer io.Writer = os.Stderr

	// LevelSpecs specifies the id, string name and color-printing function
	LevelSpecs = []LevelSpec{
		{Off, "", NoSprint},
		{Fatal, "FTL", color.New(color.BgRed, color.FgHiWhite).Sprint},
		{Error, "ERR", color.New(color.FgHiRed).Sprint},
		{Warn, "WRN", color.New(color.FgHiYellow).Sprint},
		{Info, "INF", color.New(color.FgHiGreen).Sprint},
		{Debug, "DBG", color.New(color.FgHiBlue).Sprint},
		{Trace, "TRC", color.New(color.FgHiMagenta).Sprint},
	}
	NoTimeStamp atomic.Bool
	ShortLoc    atomic.Bool
)

Level is the level that the logger is printing at.

View Source
var LevelNames = []string{
	"off",
	"fatal",
	"error",
	"warn",
	"info",
	"debug",
	"trace",
}
View Source
var Main = &Logger{}

Main is the main logger.

View Source
var Tracer = getTracer()

Functions

func GetLoc

func GetLoc(skip int) (output string)

GetLoc returns the code location of the caller.

func GetLogLevel

func GetLogLevel(level string) (i int)

GetLogLevel returns the log level number of a string log level.

func GetNLoc

func GetNLoc(n int) (output string)

GetNLoc returns multiple levels of depth of code location from the current.

func JoinStrings

func JoinStrings(a ...any) (s string)

JoinStrings joins together anything into a set of strings with space separating the items.

func New

func New(writer io.Writer, skip int) (l *Log, c *Check, errorf *Errorf)

New creates a new logger with all the levels and things.

func NoSprint

func NoSprint(a ...any) string

NoSprint is a noop for sprint (it returns nothing no matter what is given to it).

func SetLogLevel

func SetLogLevel(level string)

SetLogLevel sets the log level of the logger.

func SetLoggers

func SetLoggers(level int)

SetLoggers configures a log level.

func TimeStamper

func TimeStamper() (s string)

TimeStamper generates the timestamp for logs.

Types

type C

type C func(closure func() string)

C accepts a function so that the extra computation can be avoided if it is not being viewed

type Check

type Check struct {
	F, E, W, I, D, T Chk
}

Check is the set of log levels for a Check operation (prints an error if the error is not nil).

type Chk

type Chk func(e error) bool

Chk is a shortcut for printing if there is an error, or returning true

type Entry

type Entry struct {
	Time         time.Time
	Level        string
	Package      string
	CodeLocation string
	Text         string
}

Entry is a log entry to be printed as json to the log file

type Err

type Err func(format string, a ...any) error

Err is a pass-through function that uses fmt.Errorf to construct an error and returns the error after printing it to the log

type Errorf

type Errorf struct {
	F, E, W, I, D, T Err
}

Errorf prints an error that is also returned as an error, so the error is logged at the site.

type F

type F func(format string, a ...interface{})

F prints like fmt.Println surrounded []byte log details

type LevelPrinter

type LevelPrinter struct {
	Ln
	F
	S
	C
	Chk
	Err
}

LevelPrinter is the set of log printers on each log level.

func GetNullPrinter

func GetNullPrinter() LevelPrinter

GetNullPrinter is a logger that doesn't log.

func GetPrinter

func GetPrinter(l int32, writer io.Writer, skip int) LevelPrinter

GetPrinter returns a full logger that writes to the provided io.Writer.

type LevelSpec

type LevelSpec struct {
	ID        int
	Name      string
	Colorizer func(a ...any) string
}

LevelSpec is the name, ID and Colorizer for a log level.

type Ln

type Ln func(a ...interface{})

Ln prints lists of server with spaces in between

type Log

type Log struct {
	F, E, W, I, D, T LevelPrinter
}

Log is a set of log printers for the various Level items.

type Logger

type Logger struct {
	*Log
	*Check
	*Errorf
}

Logger is a collection of things that creates a logger, including levels.

type S

type S func(a ...interface{})

S prints a spew.Sdump for an enveloper slice

Jump to

Keyboard shortcuts

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