Better README

This commit is contained in:
Lilleman auf Larv 2022-10-11 07:30:46 +02:00
parent a5cc91cebb
commit 8425944e0c
3 changed files with 80 additions and 9 deletions

View File

@ -6,12 +6,67 @@
## Example usage ## Example usage
Most basic usage with default settings:
```go ```go
import "gitea.larvit.se/pwrpln/go_log" import "gitea.larvit.se/pwrpln/go_log"
func main() { func main() {
log := Log{} log := go_log.Log{}
log.SetDefaultValues{} 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
```

12
main.go
View File

@ -32,12 +32,12 @@ type Fmt func(FmtOpts) string
type Std func(string) type Std func(string)
type Log struct { type Log struct {
Context []Metadata Context []Metadata // Will be prepended to metadata on all log entries
MinLogLvl LogLvl MinLogLvl LogLvl // Minimal log level to output
Fmt Fmt Fmt Fmt // Log message formatter
Stderr Std Stderr Std // Log message outputter for Debug, Verbose and Info
Stdout Std Stdout Std // Log message outputter for Warning and Error
TimeLocation *time.Location TimeLocation *time.Location // Timestamp location/time zone setting
} }
func LogName(logLvl LogLvl) string { func LogName(logLvl LogLvl) string {

View File

@ -1,6 +1,9 @@
package main package main
import "testing" import (
"testing"
"time"
)
func TestDefault(t *testing.T) { func TestDefault(t *testing.T) {
testLog := Log{} testLog := Log{}
@ -18,3 +21,16 @@ func TestDefault(t *testing.T) {
testLog.Info("bosse") testLog.Info("bosse")
testLog.ErrorM("frasse", []Metadata{{Name: "wat", Value: ":O"}}) 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")
}