Compare commits

..

3 commits

Author SHA1 Message Date
ba2ba315d0
docs: enhanced simple example
All checks were successful
Build / Checks (push) Successful in 33s
Build / Code Coverage (push) Successful in 43s
Build / Build (push) Successful in 30s
Release / Semantic Release (push) Successful in 45s
2025-03-04 20:56:50 +01:00
a1c991bcbd
style: fix goland warnings for unused parameters 2025-03-04 20:56:30 +01:00
eb8b9abe1a
feat: new WithMap method for adding multiple values with one call 2025-03-04 20:56:00 +01:00
6 changed files with 28 additions and 4 deletions

View file

@ -12,6 +12,8 @@ func main(){
} }
``` ```
Check out this [simple example file](cmd/simple/main.go) for the basic usage.
## Multiple Log Instance ## Multiple Log Instance
Create a new log instance (instead of using the `Default`). Create a new log instance (instead of using the `Default`).

View file

@ -1,15 +1,24 @@
package main package main
import ( import (
"git.martin-riedl.de/golang/log"
"runtime" "runtime"
"git.martin-riedl.de/golang/log"
) )
func main() { func main() {
someMethod() someMethod()
anotherMethod()
} }
func someMethod() { func someMethod() {
log.Default.Info("Hello World") log.Default.Info("Hello World")
log.Default.With("os", runtime.GOOS).Info("environment detected") log.Default.With("os", runtime.GOOS).Warning("environment detected")
}
func anotherMethod() {
log.Default.WithMap(map[string]any{
"foo": "bar",
"bar": "baz",
}).Info("Second Hello World")
} }

View file

@ -49,6 +49,13 @@ func (entry *Entry) With(key string, value any) *Entry {
return entry return entry
} }
func (entry *Entry) WithMap(entries map[string]any) *Entry {
for key, value := range entries {
entry.With(key, value)
}
return entry
}
func (entry *Entry) WithContent(content []Content) *Entry { func (entry *Entry) WithContent(content []Content) *Entry {
entry.Content = append(entry.Content, content...) entry.Content = append(entry.Content, content...)
return entry return entry

View file

@ -65,7 +65,7 @@ func (formatter *FormatterJSON) Process(entry *Entry) {
} }
} }
func (formatter *FormatterJSON) End(entry *Entry) []byte { func (formatter *FormatterJSON) End(_ *Entry) []byte {
// build JSON // build JSON
data, err := json.Marshal(formatter.data) data, err := json.Marshal(formatter.data)
if err != nil { if err != nil {

View file

@ -82,7 +82,7 @@ func (formatter *FormatterKeyValue) addKV(key string, value any) {
formatter.builder.WriteString(val) formatter.builder.WriteString(val)
} }
func (formatter *FormatterKeyValue) End(entry *Entry) []byte { func (formatter *FormatterKeyValue) End(_ *Entry) []byte {
// send data // send data
return []byte(formatter.builder.String()) return []byte(formatter.builder.String())
} }

View file

@ -113,6 +113,12 @@ func (logger *Logger) With(key string, value any) *Entry {
return entry return entry
} }
func (logger *Logger) WithMap(entries map[string]any) *Entry {
entry := NewEntry(logger)
entry.WithMap(entries)
return entry
}
func (logger *Logger) WithContent(content []Content) *Entry { func (logger *Logger) WithContent(content []Content) *Entry {
entry := NewEntry(logger) entry := NewEntry(logger)
entry.WithContent(content) entry.WithContent(content)