Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

feature: added file logger and env var to enable / disable loggers (#977)

Théo SZANTO8a4057bbc8f1
diff --git a/.example.env b/.example.env
index 68e4c9b20..33bacf714 100644
--- a/.example.env
+++ b/.example.env
@@ -1,11 +1,16 @@
HOST_DOMAIN=localhost
GO_ENV=development
-LOG_LEVEL=DEBUG
DATABASE_URL=postgres://fider:fider_pw@localhost:5555/fider?sslmode=disable
JWT_SECRET=hsjl]W;&ZcHxT&FK;s%bgIQF:#ch=~#Al4:5]N;7V<qPZ3e9lT4'%;go;LIkc%k
CDN_HOST=dev.fider.io:3000
+LOG_LEVEL=DEBUG
+LOG_CONSOLE=true
+LOG_SQL=true
+LOG_FILE=false
+LOG_FILE_OUTPUT=logs/output.log
+
# EXPERIMENTAL_SSR_SEO=true
# MAINTENANCE=true
diff --git a/app/cmd/server.go b/app/cmd/server.go
index 9cca2f7bf..35df87c61 100644
--- a/app/cmd/server.go
+++ b/app/cmd/server.go
@@ -23,6 +23,7 @@ import (
_ "github.com/getfider/fider/app/services/email/smtp"
_ "github.com/getfider/fider/app/services/httpclient"
_ "github.com/getfider/fider/app/services/log/console"
+ _ "github.com/getfider/fider/app/services/log/file"
_ "github.com/getfider/fider/app/services/log/sql"
_ "github.com/getfider/fider/app/services/oauth"
_ "github.com/getfider/fider/app/services/sqlstore/postgres"
diff --git a/app/pkg/env/env.go b/app/pkg/env/env.go
index e85420114..90f576bd7 100644
--- a/app/pkg/env/env.go
+++ b/app/pkg/env/env.go
@@ -53,6 +53,10 @@ type config struct {
Log struct {
Level string `env:"LOG_LEVEL,default=INFO"`
Structured bool `env:"LOG_STRUCTURED,default=false"`
+ Console bool `env:"LOG_CONSOLE,default=true"`
+ Sql bool `env:"LOG_SQL,default=true"`
+ File bool `env:"LOG_FILE,default=false"`
+ OutputFile string `env:"LOG_FILE_OUTPUT,default=logs/output.log"`
}
OAuth struct {
Google struct {
diff --git a/app/services/log/console/console.go b/app/services/log/console/console.go
index 168d2e726..18af2770a 100644
--- a/app/services/log/console/console.go
+++ b/app/services/log/console/console.go
@@ -32,7 +32,7 @@ func (s Service) Category() string {
}
func (s Service) Enabled() bool {
- return !env.IsTest()
+ return !env.IsTest() && env.Config.Log.Console
}
func (s Service) Init() {
diff --git a/app/services/log/file/file.go b/app/services/log/file/file.go
new file mode 100644
index 000000000..fcd9c819b
--- /dev/null
+++ b/app/services/log/file/file.go
@@ -0,0 +1,105 @@
+package file
+
+import (
+ "context"
+ "encoding/json"
+ stdLog "log"
+ "os"
+ "path/filepath"
+ "time"
+
+ "github.com/getfider/fider/app/models/cmd"
+ "github.com/getfider/fider/app/models/dto"
+ "github.com/getfider/fider/app/pkg/bus"
+ "github.com/getfider/fider/app/pkg/env"
+ "github.com/getfider/fider/app/pkg/log"
+)
+
+var stdOut *stdLog.Logger
+
+func init() {
+ bus.Register(Service{})
+}
+
+type Service struct{}
+
+func (s Service) Name() string {
+ return "File"
+}
+
+func (s Service) Category() string {
+ return "log"
+}
+
+func (s Service) Enabled() bool {
+ return !env.IsTest() && env.Config.Log.File
+}
+
+func (s Service) Init() {
+ bus.AddListener(logDebug)
+ bus.AddListener(logWarn)
+ bus.AddListener(logInfo)
+ bus.AddListener(logError)
+
+ // Get configured output file and directory
+ file := env.Config.Log.OutputFile
+ dir := filepath.Dir(file)
+
+ // Ensure directory exists
+ if _, err := os.Stat(dir); err != nil {
+ err = os.MkdirAll(dir, 0750)
+ if err != nil {
+ panic(err)
+ }
+ }
+
+ // Open (or create) output file
+ output, err := os.OpenFile(file, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0640)
+ if err != nil {
+ panic(err)
+ }
+ stdOut = stdLog.New(output, "", 0)
+}
+
+func logDebug(ctx context.Context, c *cmd.LogDebug) {
+ writeLog(ctx, log.DEBUG, c.Message, c.Props)
+}
+
+func logWarn(ctx context.Context, c *cmd.LogWarn) {
+ writeLog(ctx, log.WARN, c.Message, c.Props)
+}
+
+func logInfo(ctx context.Context, c *cmd.LogInfo) {
+ writeLog(ctx, log.INFO, c.Message, c.Props)
+}
+
+func logError(ctx context.Context, c *cmd.LogError) {
+ if c.Err != nil {
+ writeLog(ctx, log.ERROR, c.Err.Error(), c.Props)
+ } else if c.Message != "" {
+ writeLog(ctx, log.ERROR, c.Message, c.Props)
+ } else {
+ writeLog(ctx, log.ERROR, "nil", c.Props)
+ }
+}
+
+func writeLog(ctx context.Context, level log.Level, message string, props dto.Props) {
+ if !log.IsEnabled(level) {
+ return
+ }
+
+ props = log.GetProperties(ctx).Merge(props)
+ props["Level"] = level.String()
+ props["Message"] = log.Parse(message, props, false)
+ props["Timestamp"] = time.Now().Format(time.RFC3339)
+ if props[log.PropertyKeyTag] == nil {
+ props[log.PropertyKeyTag] = "???"
+ }
+
+ if env.Config.Log.Structured {
+ _ = json.NewEncoder(stdOut.Writer()).Encode(props)
+ return
+ }
+
+ stdOut.Printf("%s [%s] [%s] %s\n", level, props["Timestamp"], props[log.PropertyKeyTag], props["Message"])
+}
diff --git a/app/services/log/sql/sql.go b/app/services/log/sql/sql.go
index fb51d4898..d413b3d80 100644
--- a/app/services/log/sql/sql.go
+++ b/app/services/log/sql/sql.go
@@ -27,7 +27,7 @@ func (s Service) Category() string {
}
func (s Service) Enabled() bool {
- return !env.IsTest()
+ return !env.IsTest() && env.Config.Log.Sql
}
func (s Service) Init() {