Compare commits

..

No commits in common. "v0.6.0" and "v0.5.0" have entirely different histories.

7 changed files with 8 additions and 61 deletions

View file

@ -43,6 +43,7 @@ var myLog := log.NewLogger(
[]*Output{output1, output2}
)
myLog.Info("My First Info Message")
```
## Custom Formatter / Printer

View file

@ -1,30 +0,0 @@
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")
}

View file

@ -17,7 +17,7 @@ func someMethod() {
}
func anotherMethod() {
log.Default.WithMap(log.Map{
log.Default.WithMap(map[string]any{
"foo": "bar",
"bar": "baz",
}).Info("Second Hello World")

View file

@ -34,8 +34,6 @@ type Content struct {
Value any `json:"value"`
}
type Map map[string]any
func NewEntry(logger *Logger) *Entry {
return &Entry{
Logger: logger,
@ -51,7 +49,7 @@ func (entry *Entry) With(key string, value any) *Entry {
return entry
}
func (entry *Entry) WithMap(entries Map) *Entry {
func (entry *Entry) WithMap(entries map[string]any) *Entry {
for key, value := range entries {
entry.With(key, value)
}

View file

@ -24,7 +24,7 @@ import (
type FormatterJSON struct {
FlatContent bool
TimeFormat string
data map[string]any
data map[string]interface{}
}
func NewFormatterJSON() *FormatterJSON {
@ -35,7 +35,7 @@ func NewFormatterJSON() *FormatterJSON {
func (formatter *FormatterJSON) Begin(entry *Entry) {
// reset formatter
formatter.data = make(map[string]any)
formatter.data = make(map[string]interface{})
// add timestamp
formatter.data["time"] = entry.Time.Format(formatter.TimeFormat)

View file

@ -16,17 +16,12 @@ package log
import (
"fmt"
"slices"
"strings"
"time"
)
type FormatterKeyValue struct {
TimeFormat string
// HighPriorityKeys are printed before the actual log message
HighPriorityKeys []string
// PriorityKeys are printed after the log message
PriorityKeys []string
TimeFormat string
builder strings.Builder
isFirstKVWritten bool
}
@ -50,23 +45,9 @@ func (formatter *FormatterKeyValue) Begin(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
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
if len(entry.CallStack) > 0 {
caller := entry.CallStack[0]
@ -76,10 +57,7 @@ func (formatter *FormatterKeyValue) Process(entry *Entry) {
// add additional fields
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)
}
}

View file

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