fatal

package module
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2024 License: MIT Imports: 7 Imported by: 1

README

fatal

fatal by LitFill. Library for fatal assignment or logging (error management). Using log/slog package.

Example

Log(http.ServeAndListen(port),
    logger,
    "Can not serve and listen",
    "port", port,
)

file := Assign(os.Create(filename),
    logger,
    "Can not create file",
    "file name", filename,
)

Usage notes

[!NOTE] I don't know how to optimize this any further.

how this typically used:

func main() {
  logFile := fatal.CreateLogFile("log.json")
  defer logFile.Close()
  logger := fatal.CreateLogger(io.MultiWriter(logFile, os.Stderr), slog.LevelInfo)
}

and then use it like normal.

Documentation

Overview

fatal, LitFill <marrazzy54 at email dot com> library for fatal assignment or logging (error management)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assign

func Assign[T any](val T, err error) func(*slog.Logger, string, ...any) T

Assign takes the return values of functions that return (val T, err error) and returns a function that takes msg and logs in style of slog.logger and return the val T.

file := Assign(os.Create(fileName))(
	myLogger,
    "cannot create file",
    "filename", fileName,
)

func CreateLogFile added in v0.1.8

func CreateLogFile(filename string) *os.File

CreateLogFile creates a new log file with the specified filename and returns a pointer to the opened file. CreateLogFile uses Assign with default slog.Logger to do a proper assignment.

func CreateLogger added in v0.1.8

func CreateLogger(w io.Writer, lev slog.Level) *slog.Logger

CreateLogger creates a new logger object using the slog package and configures it to write logs to the provided writer w. The logger's logging level is also set based on the lev parameter.

func Debug added in v0.0.3

func Debug(logger *slog.Logger, msg string, args ...any)

Debug is a function that wraps slog. The log record contains the source position of the caller of Debug.

func Error added in v0.0.3

func Error(logger *slog.Logger, msg string, args ...any)

Error is a function that wraps slog. The log record contains the source position of the caller of Error.

func Info added in v0.0.3

func Info(logger *slog.Logger, msg string, args ...any)

Info is a function that wraps slog. The log record contains the source position of the caller of Info.

func Log

func Log(err error, logger *slog.Logger, msg string, args ...any)

Log wraps function call returning error to log it using `log/slog` so it has `msg` and `log`. example:

Log(http.ServeAndListen(port),
	myLogger,
    "Can not serve and listen",
    "port", port
)

Types

This section is empty.

Jump to

Keyboard shortcuts

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