log

package module
v1.1.5 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2024 License: MIT Imports: 13 Imported by: 7

README

Shady Logs

Go Reference

This is a highly opinionated yet expandable logger. The main focus of this logger is easy of use, which is why this does not have logger instances, but rather a global logger (all of which log to the same set of outputs).

To get started, you have to initialize the logger along with the desired outputs:

package main

import "github.com/shadiestgoat/log"

func main() {
    log.Init(
        log.NewLoggerPrint(), 
        log.NewLoggerFile("log.txt"), 
        log.NewLoggerDiscordWebhook("<@376079696489742338>,", "https://canary.discord.com/api/webhooks/{CHANNEL}/{TOKEN}"),
        // And maybe some custom functions?
    )
}

After that, you can call global package functions to log

package main

import "github.com/shadiestgoat/log"

func main() {
    log.Init(log.NewLoggerPrint())

    log.Success("Logger has started!")
    log.Warn("I can also do warnings <3")
    log.Error("Aaaa!! Errorss!!!!!!")
    g()
}

func g() {
    log.Fatal("Oh no, I shall now proceed to panic :(")
}

There are also util functions for error handling

// ... in a request handler for example
postID := "abc123"

post, err := apiWrapper.FetchPost(postID) // example api wrapper, that could return an error

// If err != nil: this will log "While fetching post 'abc123': {whatever the error message is}", and exit the req handler. Outputs the level 'ERROR'
// If err == nil: Does nothing
if log.ErrorIfErr(err, "fetching post '%s'", postID) {
    return
}

There is also log.FatalIfErr(...) which acts in the same way way that log.ErrorIfErr acts, but, in the end it panics (so no need to do error checking).

// ... in a request handler for example
postID := "abc123"

post, err := apiWrapper.FetchPost(postID) // example api wrapper, that could return an error

// If err != nil: this will log "While fetching post 'abc123': {whatever the error message is}", then crash the programme through panic(). Outputs the level 'ERROR'
// If err == nil: Does nothing
log.FatalIfErr(err)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close()

Close all the outputs. Note: The logger does not check if the output is close or not, so it is safe to call this only at the end of the application.

func Debug

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

func Error

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

func ErrorIfErr

func ErrorIfErr(err error, context string, args ...any) bool

"While {CONTEXT}: {ERROR}" Returns true if err != nil, intended for inline usage: if ErrorIfErr(err, "fetching api info, status: %d", resp.StatusCode) { return }

func Fatal

func Fatal(msg string, args ...any)

Warning! This function causes panic!

func FatalIfErr

func FatalIfErr(err error, context string, args ...any)

"While {CONTEXT}: {ERROR}" This causes panic!

func GetColor

func GetColor(l LogLevel) *color.Color

func Init

func Init(callbacks ...LogCB)

func PrintDebug

func PrintDebug(msg string, args ...any)

Doesn't log anything, but prints in the pretty debug color Doesn't include a prefix.

func PrintSuccess

func PrintSuccess(msg string, args ...any)

Doesn't log anything, but prints in the pretty success color Doesn't include a prefix.

func PrintWarn

func PrintWarn(msg string, args ...any)

Doesn't log anything, but prints in the pretty warning color Doesn't include a prefix.

func Success

func Success(msg string, args ...any)

func Warn

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

Types

type Closer

type Closer func()

Represents a callback to closing a logger

type DoLog

type DoLog func(c LogLevel, prefix, msg string)

Represents a callback to logging a message

type FileInitorMode added in v1.1.0

type FileInitorMode int
const (
	// This mode will overwrite the previous file that was written
	// Under this mode, the name will always be consistent
	FILE_OVERWRITE FileInitorMode = iota
	// This mode will append to the file names. The files will be named as 'fileName.0', 'fileName.1' etc. The most recent file will be the one with the highest extension.
	FILE_ASCENDING
	// This mode will 'shift' the files up by 1. The files will be named as 'fileName.0', 'fileName.1' etc. The most recent file will be the .0 one.
	FILE_DESCENDING
)

type LogCB

type LogCB func() (logger DoLog, closer Closer)

Function that is used in Init() - adds another callback to the caller. closer can be nil here, in which case it will not be called

func NewLoggerDiscordWebhook

func NewLoggerDiscordWebhook(cPrefix, webhook string) LogCB

Send warnings/errors through a discord webhook

func NewLoggerFile

func NewLoggerFile(fileName string) LogCB

Write logs to file This is an alias for NewLoggerFileComplex(fileName, FILE_ASCENDING, 10)

func NewLoggerFileComplex added in v1.1.0

func NewLoggerFileComplex(fileName string, mode FileInitorMode, maxNumber int) LogCB

maxNumber must be either >= 0. maxNumber indicates the max amount of files that can be stored. 0 indicates that an infinite amount of files can be stored. if mode == FILE_OVERWRITE, maxNumber is ignored. Please note the negative numbers do not count towards the maxNumber count! These are only made if a user renames a file to a negative number.

func NewLoggerPrint

func NewLoggerPrint() LogCB

Print to terminal

type LogLevel

type LogLevel int
const (
	LL_DEBUG LogLevel = iota
	LL_SUCCESS
	LL_WARN
	LL_ERROR
	LL_FATAL
)

Jump to

Keyboard shortcuts

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