Simple logger with level and time information for Go https://pkg.go.dev/git.brokenmouse.studio/bms/go-simplelog
Go to file
ever 39502b2d5b readme: Added the import alias to the examples 2024-02-11 15:14:10 +01:00
.markdownlint.json Initial commit 2024-02-11 14:57:54 +01:00
LICENSE Initial commit 2024-02-11 14:57:54 +01:00
README.md readme: Added the import alias to the examples 2024-02-11 15:14:10 +01:00
go.mod Initial commit 2024-02-11 14:57:54 +01:00
logger.go Initial commit 2024-02-11 14:57:54 +01:00

README.md

simplelog

simplelog provides the logger package, a simple logger with level and time information.

Usage

package main

import logger "git.brokenmouse.studio/bms/go-simplelog"

func main() {
	logger.Info("0x%02X", 10)
	logger.Debug("Test")
	logger.Warn("This is weird...")
	logger.Error("Something went wrong!")
	logger.Fatal("Can't continue, everything is broken!")
}

Result:

[12:00:00/INFO] 0x0A
[12:00:00/DEBUG] Test
[12:00:00/WARN] This is weird...
[12:00:00/ERROR] Something went wrong!
[12:00:00/FATAL] Can't continue, everything is broken!
exit status 1

Debug Messages

Debug messages are only logged when LogDebug is true (the default).

package main

import (
	"flag"

	logger "git.brokenmouse.studio/bms/go-simplelog"
)

func main() {
	verboseFlag := flag.Bool("v", false, "Increase verbosity.")
	flag.Parse()
	logger.LogDebug = *verboseFlag

	logger.Debug("Building index ...")
}

Result:

The message is only logged when the -v flag is specified.

[12:00:00/DEBUG] Building index ...

Fatal Errors

When logging a fatal error, the program will exit with status code 1.

logger.Fatal("Failed to initialize!")

Result:

[12:00:00/FATAL] Failed to initialize!
exit status 1

Writing to files

Messages can additionally be logged to a file or another io.StringWriter by setting LogWriter.

package main

import (
	"os"

	logger "git.brokenmouse.studio/bms/go-simplelog"
)

func main() {
	logger.LogWriter, _ = os.OpenFile("simplelog.log", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0660)

	logger.Info("Read me later...")
}

Result:

The message will appear in standard output and a new file named simplelog.log.

[12:00:00/INFO] Read me later...