feat: new priority key settings for key value formatter
This commit is contained in:
parent
f44e60cca4
commit
1d7177fb26
2 changed files with 54 additions and 2 deletions
30
cmd/keyValueFormatter/main.go
Normal file
30
cmd/keyValueFormatter/main.go
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.martin-riedl.de/golang/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
advancedSettings()
|
||||||
|
}
|
||||||
|
|
||||||
|
func advancedSettings() {
|
||||||
|
// Key Value Formatter with special settings
|
||||||
|
formatter := log.NewFormatterKeyValue()
|
||||||
|
formatter.HighPriorityKeys = []string{"firstField"}
|
||||||
|
formatter.PriorityKeys = []string{"secondField"}
|
||||||
|
formatter.TimeFormat = time.RFC3339Nano
|
||||||
|
|
||||||
|
// create new log instance
|
||||||
|
output := log.NewOutput(log.LevelDebug, formatter, log.NewPrinterStdout())
|
||||||
|
logger := log.NewLogger([]*log.Output{output})
|
||||||
|
|
||||||
|
// log some message
|
||||||
|
logger.WithMap(log.Map{
|
||||||
|
"someField": "Martin",
|
||||||
|
"firstField": "John",
|
||||||
|
"secondField": "Doe",
|
||||||
|
}).Info("This is an info message")
|
||||||
|
}
|
|
@ -16,12 +16,17 @@ package log
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FormatterKeyValue struct {
|
type FormatterKeyValue struct {
|
||||||
TimeFormat string
|
TimeFormat string
|
||||||
|
// HighPriorityKeys are printed before the actual log message
|
||||||
|
HighPriorityKeys []string
|
||||||
|
// PriorityKeys are printed after the log message
|
||||||
|
PriorityKeys []string
|
||||||
builder strings.Builder
|
builder strings.Builder
|
||||||
isFirstKVWritten bool
|
isFirstKVWritten bool
|
||||||
}
|
}
|
||||||
|
@ -45,9 +50,23 @@ func (formatter *FormatterKeyValue) Begin(entry *Entry) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (formatter *FormatterKeyValue) Process(entry *Entry) {
|
func (formatter *FormatterKeyValue) Process(entry *Entry) {
|
||||||
|
// log high priority keys
|
||||||
|
for _, content := range entry.Content {
|
||||||
|
if slices.Contains(formatter.HighPriorityKeys, content.Key) {
|
||||||
|
formatter.addKV(content.Key, content.Value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// add message
|
// add message
|
||||||
formatter.addKV("message", entry.Message)
|
formatter.addKV("message", entry.Message)
|
||||||
|
|
||||||
|
// log priority keys
|
||||||
|
for _, content := range entry.Content {
|
||||||
|
if slices.Contains(formatter.PriorityKeys, content.Key) {
|
||||||
|
formatter.addKV(content.Key, content.Value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// add call stack
|
// add call stack
|
||||||
if len(entry.CallStack) > 0 {
|
if len(entry.CallStack) > 0 {
|
||||||
caller := entry.CallStack[0]
|
caller := entry.CallStack[0]
|
||||||
|
@ -57,7 +76,10 @@ func (formatter *FormatterKeyValue) Process(entry *Entry) {
|
||||||
|
|
||||||
// add additional fields
|
// add additional fields
|
||||||
for _, content := range entry.Content {
|
for _, content := range entry.Content {
|
||||||
formatter.addKV(content.Key, content.Value)
|
if !slices.Contains(formatter.HighPriorityKeys, content.Key) &&
|
||||||
|
!slices.Contains(formatter.PriorityKeys, content.Key) {
|
||||||
|
formatter.addKV(content.Key, content.Value)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue