fatal

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 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 gmail 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 handles the return values of functions that return (val T, err error) and returns a function that logs the error and message using slog.Logger and returns val T. example:

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. It uses Assign with the default slog.Logger to handle the 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` with the specified logging level `lev`.

func Debug added in v0.0.3

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

Debug logs an error message with the source position of the caller.

func Error added in v0.0.3

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

Error logs an error message with the source position of the caller.

func Info added in v0.0.3

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

Info logs an error message with the source position of the caller.

func Log

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

Log logs the provided error with the specified message and logger, then exits the program. 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
JackTT - Gopher 🇻🇳