Compare commits
3 commits
Author | SHA1 | Date | |
---|---|---|---|
ba2ba315d0 | |||
a1c991bcbd | |||
eb8b9abe1a |
6 changed files with 28 additions and 4 deletions
|
@ -12,6 +12,8 @@ func main(){
|
|||
}
|
||||
```
|
||||
|
||||
Check out this [simple example file](cmd/simple/main.go) for the basic usage.
|
||||
|
||||
## Multiple Log Instance
|
||||
|
||||
Create a new log instance (instead of using the `Default`).
|
||||
|
|
|
@ -1,15 +1,24 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"git.martin-riedl.de/golang/log"
|
||||
"runtime"
|
||||
|
||||
"git.martin-riedl.de/golang/log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
someMethod()
|
||||
anotherMethod()
|
||||
}
|
||||
|
||||
func someMethod() {
|
||||
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")
|
||||
}
|
||||
|
|
7
entry.go
7
entry.go
|
@ -49,6 +49,13 @@ func (entry *Entry) With(key string, value any) *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 {
|
||||
entry.Content = append(entry.Content, content...)
|
||||
return entry
|
||||
|
|
|
@ -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
|
||||
data, err := json.Marshal(formatter.data)
|
||||
if err != nil {
|
||||
|
|
|
@ -82,7 +82,7 @@ func (formatter *FormatterKeyValue) addKV(key string, value any) {
|
|||
formatter.builder.WriteString(val)
|
||||
}
|
||||
|
||||
func (formatter *FormatterKeyValue) End(entry *Entry) []byte {
|
||||
func (formatter *FormatterKeyValue) End(_ *Entry) []byte {
|
||||
// send data
|
||||
return []byte(formatter.builder.String())
|
||||
}
|
||||
|
|
|
@ -113,6 +113,12 @@ func (logger *Logger) With(key string, value any) *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 {
|
||||
entry := NewEntry(logger)
|
||||
entry.WithContent(content)
|
||||
|
|
Loading…
Add table
Reference in a new issue