Compare commits
7 commits
Author | SHA1 | Date | |
---|---|---|---|
1d7177fb26 | |||
f44e60cca4 | |||
5d174f8685 | |||
a2883c1077 | |||
ba2ba315d0 | |||
a1c991bcbd | |||
eb8b9abe1a |
7 changed files with 86 additions and 9 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`).
|
||||
|
@ -41,7 +43,6 @@ var myLog := log.NewLogger(
|
|||
[]*Output{output1, output2}
|
||||
)
|
||||
myLog.Info("My First Info Message")
|
||||
|
||||
```
|
||||
|
||||
## 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")
|
||||
}
|
|
@ -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(log.Map{
|
||||
"foo": "bar",
|
||||
"bar": "baz",
|
||||
}).Info("Second Hello World")
|
||||
}
|
||||
|
|
9
entry.go
9
entry.go
|
@ -34,6 +34,8 @@ type Content struct {
|
|||
Value any `json:"value"`
|
||||
}
|
||||
|
||||
type Map map[string]any
|
||||
|
||||
func NewEntry(logger *Logger) *Entry {
|
||||
return &Entry{
|
||||
Logger: logger,
|
||||
|
@ -49,6 +51,13 @@ func (entry *Entry) With(key string, value any) *Entry {
|
|||
return entry
|
||||
}
|
||||
|
||||
func (entry *Entry) WithMap(entries Map) *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
|
||||
|
|
|
@ -24,7 +24,7 @@ import (
|
|||
type FormatterJSON struct {
|
||||
FlatContent bool
|
||||
TimeFormat string
|
||||
data map[string]interface{}
|
||||
data map[string]any
|
||||
}
|
||||
|
||||
func NewFormatterJSON() *FormatterJSON {
|
||||
|
@ -35,7 +35,7 @@ func NewFormatterJSON() *FormatterJSON {
|
|||
|
||||
func (formatter *FormatterJSON) Begin(entry *Entry) {
|
||||
// reset formatter
|
||||
formatter.data = make(map[string]interface{})
|
||||
formatter.data = make(map[string]any)
|
||||
|
||||
// add timestamp
|
||||
formatter.data["time"] = entry.Time.Format(formatter.TimeFormat)
|
||||
|
@ -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 {
|
||||
|
|
|
@ -16,12 +16,17 @@ package log
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
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
|
||||
isFirstKVWritten bool
|
||||
}
|
||||
|
@ -45,9 +50,23 @@ 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]
|
||||
|
@ -57,7 +76,10 @@ func (formatter *FormatterKeyValue) Process(entry *Entry) {
|
|||
|
||||
// add additional fields
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -82,7 +104,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) *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