Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feature: added file logger and env var to enable / disable loggers (#977
)
  • Loading branch information
indyteo committed Jul 8, 2021
1 parent a6e7aed commit 8a4057b
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 3 deletions.
7 changes: 6 additions & 1 deletion .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
Expand Down
1 change: 1 addition & 0 deletions app/cmd/server.go
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions app/pkg/env/env.go
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion app/services/log/console/console.go
Expand Up @@ -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() {
Expand Down
105 changes: 105 additions & 0 deletions 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"])
}
2 changes: 1 addition & 1 deletion app/services/log/sql/sql.go
Expand Up @@ -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() {
Expand Down

0 comments on commit 8a4057b

Please sign in to comment.