From 8425944e0c27192655e801c688353ee5d8a04daf Mon Sep 17 00:00:00 2001 From: Lilleman Date: Tue, 11 Oct 2022 07:30:46 +0200 Subject: [PATCH] Better README --- README.md | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++-- main.go | 12 +++++------ main_test.go | 18 +++++++++++++++- 3 files changed, 80 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 78b937f..2a05b5b 100644 --- a/README.md +++ b/README.md @@ -6,12 +6,67 @@ ## Example usage +Most basic usage with default settings: + ```go import "gitea.larvit.se/pwrpln/go_log" func main() { - log := Log{} + log := go_log.Log{} log.SetDefaultValues{} - log.Info("My little log message") + 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: + +```go +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: + +```go +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: + +```go +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: + +```go +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 ``` \ No newline at end of file diff --git a/main.go b/main.go index 97e87cf..7f355ae 100644 --- a/main.go +++ b/main.go @@ -32,12 +32,12 @@ type Fmt func(FmtOpts) string type Std func(string) type Log struct { - Context []Metadata - MinLogLvl LogLvl - Fmt Fmt - Stderr Std - Stdout Std - TimeLocation *time.Location + Context []Metadata // Will be prepended to metadata on all log entries + MinLogLvl LogLvl // Minimal log level to output + Fmt Fmt // Log message formatter + Stderr Std // Log message outputter for Debug, Verbose and Info + Stdout Std // Log message outputter for Warning and Error + TimeLocation *time.Location // Timestamp location/time zone setting } func LogName(logLvl LogLvl) string { diff --git a/main_test.go b/main_test.go index d618058..e212bf4 100644 --- a/main_test.go +++ b/main_test.go @@ -1,6 +1,9 @@ package main -import "testing" +import ( + "testing" + "time" +) func TestDefault(t *testing.T) { testLog := Log{} @@ -18,3 +21,16 @@ func TestDefault(t *testing.T) { testLog.Info("bosse") testLog.ErrorM("frasse", []Metadata{{Name: "wat", Value: ":O"}}) } + +func TestWoo(t *testing.T) { + loc, _ := time.LoadLocation("UTC") + log := Log{ + Context: []Metadata{}, // Will be prepended to metadata on all log entries + MinLogLvl: Info, // Minimal log level to output + Fmt: DefaultFmt, // Log message formatter + Stderr: DefaultStderr, // Log message outputter for Debug, Verbose and Info + Stdout: DefaultStdout, // Log message outputter for Warning and Error + TimeLocation: loc, // Timestamp location/time zone setting + } + log.Info("wat") +}