Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

enhancement: add structured logging to output

goenning8da878507cd6
diff --git a/app/pkg/env/env.go b/app/pkg/env/env.go
index 75ccc715c..e85420114 100644
--- a/app/pkg/env/env.go
+++ b/app/pkg/env/env.go
@@ -51,7 +51,8 @@ type config struct {
Host string `env:"CDN_HOST"`
}
Log struct {
- Level string `env:"LOG_LEVEL,default=INFO"`
+ Level string `env:"LOG_LEVEL,default=INFO"`
+ Structured bool `env:"LOG_STRUCTURED,default=false"`
}
OAuth struct {
Google struct {
diff --git a/app/services/log/console/console.go b/app/services/log/console/console.go
index 46adba46b..168d2e726 100644
--- a/app/services/log/console/console.go
+++ b/app/services/log/console/console.go
@@ -2,6 +2,7 @@ package console
import (
"context"
+ "encoding/json"
stdLog "log"
"os"
"time"
@@ -69,18 +70,19 @@ func writeLog(ctx context.Context, level log.Level, message string, props dto.Pr
}
props = log.GetProperties(ctx).Merge(props)
- message = log.Parse(message, props, true)
- tag := props[log.PropertyKeyTag]
- if tag == nil {
- tag = "???"
+ props["Level"] = level.String()
+ props["Message"] = log.Parse(message, props, !env.Config.Log.Structured)
+ props["Timestamp"] = time.Now().Format(time.RFC3339)
+ if props[log.PropertyKeyTag] == nil {
+ props[log.PropertyKeyTag] = "???"
}
- contextID := props[log.PropertyKeyContextID]
- if contextID == nil {
- stdOut.Printf("%s [%s] [%s] %s\n", colorizeLevel(level), time.Now().Format(time.RFC3339), tag, message)
- } else {
- stdOut.Printf("%s [%s] [%s] [%s] %s\n", colorizeLevel(level), time.Now().Format(time.RFC3339), tag, contextID, message)
+ if env.Config.Log.Structured {
+ _ = json.NewEncoder(stdOut.Writer()).Encode(props)
+ return
}
+
+ stdOut.Printf("%s [%s] [%s] %s\n", colorizeLevel(level), props["Timestamp"], props[log.PropertyKeyTag], props["Message"])
}
func colorizeLevel(level log.Level) string {