Compare commits
4 commits
v0.5.0
...
v0.6.0-bet
Author | SHA1 | Date | |
---|---|---|---|
1d7177fb26 | |||
f44e60cca4 | |||
5d174f8685 | |||
a2883c1077 |
7 changed files with 61 additions and 8 deletions
|
@ -43,7 +43,6 @@ var myLog := log.NewLogger(
|
||||||
[]*Output{output1, output2}
|
[]*Output{output1, output2}
|
||||||
)
|
)
|
||||||
myLog.Info("My First Info Message")
|
myLog.Info("My First Info Message")
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Custom Formatter / Printer
|
## Custom Formatter / Printer
|
||||||
|
|
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")
|
||||||
|
}
|
|
@ -17,7 +17,7 @@ func someMethod() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func anotherMethod() {
|
func anotherMethod() {
|
||||||
log.Default.WithMap(map[string]any{
|
log.Default.WithMap(log.Map{
|
||||||
"foo": "bar",
|
"foo": "bar",
|
||||||
"bar": "baz",
|
"bar": "baz",
|
||||||
}).Info("Second Hello World")
|
}).Info("Second Hello World")
|
||||||
|
|
4
entry.go
4
entry.go
|
@ -34,6 +34,8 @@ type Content struct {
|
||||||
Value any `json:"value"`
|
Value any `json:"value"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Map map[string]any
|
||||||
|
|
||||||
func NewEntry(logger *Logger) *Entry {
|
func NewEntry(logger *Logger) *Entry {
|
||||||
return &Entry{
|
return &Entry{
|
||||||
Logger: logger,
|
Logger: logger,
|
||||||
|
@ -49,7 +51,7 @@ func (entry *Entry) With(key string, value any) *Entry {
|
||||||
return entry
|
return entry
|
||||||
}
|
}
|
||||||
|
|
||||||
func (entry *Entry) WithMap(entries map[string]any) *Entry {
|
func (entry *Entry) WithMap(entries Map) *Entry {
|
||||||
for key, value := range entries {
|
for key, value := range entries {
|
||||||
entry.With(key, value)
|
entry.With(key, value)
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ import (
|
||||||
type FormatterJSON struct {
|
type FormatterJSON struct {
|
||||||
FlatContent bool
|
FlatContent bool
|
||||||
TimeFormat string
|
TimeFormat string
|
||||||
data map[string]interface{}
|
data map[string]any
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFormatterJSON() *FormatterJSON {
|
func NewFormatterJSON() *FormatterJSON {
|
||||||
|
@ -35,7 +35,7 @@ func NewFormatterJSON() *FormatterJSON {
|
||||||
|
|
||||||
func (formatter *FormatterJSON) Begin(entry *Entry) {
|
func (formatter *FormatterJSON) Begin(entry *Entry) {
|
||||||
// reset formatter
|
// reset formatter
|
||||||
formatter.data = make(map[string]interface{})
|
formatter.data = make(map[string]any)
|
||||||
|
|
||||||
// add timestamp
|
// add timestamp
|
||||||
formatter.data["time"] = entry.Time.Format(formatter.TimeFormat)
|
formatter.data["time"] = entry.Time.Format(formatter.TimeFormat)
|
||||||
|
|
|
@ -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,8 +76,11 @@ func (formatter *FormatterKeyValue) Process(entry *Entry) {
|
||||||
|
|
||||||
// add additional fields
|
// add additional fields
|
||||||
for _, content := range entry.Content {
|
for _, content := range entry.Content {
|
||||||
|
if !slices.Contains(formatter.HighPriorityKeys, content.Key) &&
|
||||||
|
!slices.Contains(formatter.PriorityKeys, content.Key) {
|
||||||
formatter.addKV(content.Key, content.Value)
|
formatter.addKV(content.Key, content.Value)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (formatter *FormatterKeyValue) addKV(key string, value any) {
|
func (formatter *FormatterKeyValue) addKV(key string, value any) {
|
||||||
|
|
|
@ -113,7 +113,7 @@ func (logger *Logger) With(key string, value any) *Entry {
|
||||||
return entry
|
return entry
|
||||||
}
|
}
|
||||||
|
|
||||||
func (logger *Logger) WithMap(entries map[string]any) *Entry {
|
func (logger *Logger) WithMap(entries Map) *Entry {
|
||||||
entry := NewEntry(logger)
|
entry := NewEntry(logger)
|
||||||
entry.WithMap(entries)
|
entry.WithMap(entries)
|
||||||
return entry
|
return entry
|
||||||
|
|
Loading…
Add table
Reference in a new issue