Simple logger
Go to file
2022-10-15 23:11:56 +02:00
go.mod feat: tests and stuff 2022-10-15 23:06:06 +02:00
go.sum feat: tests and stuff 2022-10-15 23:06:06 +02:00
LICENSE Initial commit 2022-10-10 05:22:22 +02:00
main_test.go fix: changing name on the only package 2022-10-15 23:11:56 +02:00
main.go fix: changing name on the only package 2022-10-15 23:11:56 +02:00
README.md Better README 2022-10-11 07:30:46 +02:00

Simple logger for Go

Installation

go get -u https://gitea.larvit.se/pwrpln/go_log

Example usage

Most basic usage with default settings:

import "gitea.larvit.se/pwrpln/go_log"

func main() {
	log := go_log.Log{}
	log.SetDefaultValues{}
	log.Error("Apocalypse! :O"); // stderr
	log.Warn("The chaos is near"); // stderr
	log.Info("All is well, but this message is important"); // stdout

	// Will not be shown due to default log level-limit is "info"
	log.Verbose("Extra info, likely good in a production environment"); // stdout
	log.Debug("A lot of detailed logs to debug your application"); // stdout
}

Set log level:

log := go_log.Log{MinLogLvl: go_log.Debug}
log.SetDefaultValues{}

// Will now show on stdout
log.Debug("A lot of detailed logs to debug your application");
// 2022-10-11 07:13:49 [Deb] A lot of detailed logs to debug your application

Using metadata for structured logging:

log.InfoM("My log msg", []go_log.Metadata{{Name: "foo", Value: "bar"}})
// 2022-10-11 07:13:49 [Inf] My log msg foo: bar

Setting a logging context to prepend metadata on all log entries:

log := go_log.Log{}
log.SetDefaultValues{}
log.Context = []go_log.Metadata{{Name: "some", Value: "thing"}}

log.Info("A message")
// 2022-10-11 07:13:49 [Inf] A message some: thing

log.InfoM("Zep", []go_log.Metadata{{Name: "other", Value: "stuff"}})
// 2022-10-11 07:13:49 [Inf] A message some: thing other: stuff

All available options, and their defaults:

loc, _ := time.LoadLocation("UTC")
log := go_log.Log{
	Context:      []go_log.Metadata{},  // Will be prepended to metadata on all log entries
	MinLogLvl:    go_log.Info,          // Minimal log level to output
	Fmt:          go_log.DefaultFmt,    // Log message formatter
	Stderr:       go_log.DefaultStderr, // Log message outputter for Debug, Verbose and Info
	Stdout:       go_log.DefaultStdout, // Log message outputter for Warning and Error
	TimeLocation: loc,                  // Timestamp location/time zone setting
}
// Don't run log.SetDefaultValues{} since it will override your settings