2022-10-11 06:25:08 +02:00
|
|
|
# Simple logger for Go
|
2022-10-10 05:22:22 +02:00
|
|
|
|
2022-10-11 06:25:08 +02:00
|
|
|
## Installation
|
|
|
|
|
|
|
|
`go get -u https://gitea.larvit.se/pwrpln/go_log`
|
|
|
|
|
|
|
|
## Example usage
|
|
|
|
|
2022-10-11 07:30:46 +02:00
|
|
|
Most basic usage with default settings:
|
|
|
|
|
2022-10-11 06:25:08 +02:00
|
|
|
```go
|
|
|
|
import "gitea.larvit.se/pwrpln/go_log"
|
|
|
|
|
|
|
|
func main() {
|
2022-10-11 07:30:46 +02:00
|
|
|
log := go_log.Log{}
|
2022-10-11 06:25:08 +02:00
|
|
|
log.SetDefaultValues{}
|
2022-10-11 07:30:46 +02:00
|
|
|
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
|
2022-10-11 06:25:08 +02:00
|
|
|
}
|
2022-10-11 07:30:46 +02:00
|
|
|
// Don't run log.SetDefaultValues{} since it will override your settings
|
2022-10-11 06:25:08 +02:00
|
|
|
```
|