go_log/README.md

77 lines
2.1 KiB
Markdown
Raw Permalink Normal View History

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
2023-02-20 22:23:10 +01:00
`go get -u gitea.larvit.se/pwrpln/go_log`
2022-10-11 06:25:08 +02:00
## 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() {
log := go_log.GetLog()
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.GetLog()
log.MinLogLvl = go_log.Debug
2022-10-11 07:30:46 +02:00
// 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
2022-10-16 00:22:29 +02:00
log.Info("My log msg", "foo", "bar")
2022-10-11 07:30:46 +02:00
// 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.GetLog()
2022-10-16 00:22:29 +02:00
log.Context = []interface{{"some", "thing"}}
2022-10-11 07:30:46 +02:00
log.Info("A message")
// 2022-10-11 07:13:49 [Inf] A message some: thing
2022-10-16 00:22:29 +02:00
log.Info("Zep", "other", "stuff")
2022-10-11 07:30:46 +02:00
// 2022-10-11 07:13:49 [Inf] A message some: thing other: stuff
```
All available options, and their defaults:
```go
2023-02-20 22:23:10 +01:00
loc, _ := time.LoadLocation("UTC") // See more info at https://pkg.go.dev/time#LoadLocation
2022-10-11 07:30:46 +02:00
log := go_log.Log{
2023-02-20 23:15:08 +01:00
Context: []interface{}{}, // Will be prepended to metadata on all log entries
2023-02-20 22:23:10 +01:00
MinLogLvl: go_log.LogLvlFromStr("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
}
2023-02-20 22:23:10 +01:00
```
Or change them after initialization like this:
```go
log := go_log.GetLog()
log.MinLogLvl = go_log.LogLvlFromStr("Debug")
2023-02-20 23:15:08 +01:00
log.Context = []interface{}{"key1", "value1", "key2", "value2"}
2022-10-11 06:25:08 +02:00
```