Better README
This commit is contained in:
parent
a5cc91cebb
commit
8425944e0c
59
README.md
59
README.md
|
@ -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
12
main.go
|
@ -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 {
|
||||||
|
|
18
main_test.go
18
main_test.go
|
@ -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")
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user