Open platform to collect and prioritize feedback
Colorized logs (#146)
Guilherme Oenning873124f2c0df
diff --git a/app/middlewares/domain.go b/app/middlewares/domain.goindex 5e744fec6..7d3874364 100644--- a/app/middlewares/domain.go+++ b/app/middlewares/domain.go@@ -58,8 +58,12 @@ func MultiTenant() web.MiddlewareFunc {return next(c)}- c.Logger().Debugf("Tenant not found for '%s'.", hostname)- return c.NotFound()+ if err == app.ErrNotFound {+ c.Logger().Debugf("Tenant not found for '%s'.", hostname)+ return c.NotFound()+ }++ return c.Failure(err)}}}diff --git a/app/middlewares/setup.go b/app/middlewares/setup.goindex 7f6e26f96..dee92b589 100644--- a/app/middlewares/setup.go+++ b/app/middlewares/setup.go@@ -5,6 +5,7 @@ import ("runtime/debug""github.com/getfider/fider/app/pkg/email"+ "github.com/getfider/fider/app/pkg/log""github.com/getfider/fider/app""github.com/getfider/fider/app/pkg/dbx"@@ -26,7 +27,8 @@ func Noop() web.MiddlewareFunc {func Setup(db *dbx.Database, emailer email.Sender) web.MiddlewareFunc {return func(next web.HandlerFunc) web.HandlerFunc {return func(c web.Context) error {- c.Logger().Debugf("HTTP Request %s %s", c.Request.Method, c.Request.URL.String())+ path := log.Reverse(log.Magenta(c.Request.Method + " " + c.Request.URL.String()))+ c.Logger().Debugf("Starting Request %s", path)trx, err := db.Begin()if err != nil {@@ -47,16 +49,20 @@ func Setup(db *dbx.Database, emailer email.Sender) web.MiddlewareFunc {if trx != nil {trx.Rollback()}+ c.Logger().Debugf("Finished Request %s", path)c.Failure(err)}}()err = next(c)+ c.Logger().Debugf("Finished Request %s", path)if err != nil {+ c.Logger().Debugf("Transaction rolled back")trx.Rollback()return err}+ c.Logger().Debugf("Transaction committed")return trx.Commit()}}diff --git a/app/pkg/dbx/dbx.go b/app/pkg/dbx/dbx.goindex 72c6073f7..3365339da 100644--- a/app/pkg/dbx/dbx.go+++ b/app/pkg/dbx/dbx.go@@ -158,21 +158,21 @@ type Trx struct {// QueryRow the database with given SQL command and returns only one rowfunc (trx Trx) QueryRow(command string, args ...interface{}) *sql.Row {command = formatCommand(command)- trx.logger.Debugf("%s %v", command, args)+ trx.logger.Debugf("%s %v", log.Yellow(command), log.Blue(args))return trx.tx.QueryRow(command, args...)}// Query the database with given SQL commandfunc (trx Trx) Query(command string, args ...interface{}) (*sql.Rows, error) {command = formatCommand(command)- trx.logger.Debugf("%s %v", command, args)+ trx.logger.Debugf("%s %v", log.Yellow(command), log.Blue(args))return trx.tx.Query(command, args...)}// Execute given SQL commandfunc (trx Trx) Execute(command string, args ...interface{}) error {command = formatCommand(command)- trx.logger.Debugf("%s %v", command, args)+ trx.logger.Debugf("%s %v", log.Yellow(command), log.Blue(args))_, err := trx.tx.Exec(command, args...)if err != nil {return errdiff --git a/app/pkg/log/colors.go b/app/pkg/log/colors.gonew file mode 100644index 000000000..32b445420--- /dev/null+++ b/app/pkg/log/colors.go@@ -0,0 +1,49 @@+package log++import "fmt"++var (+ colorRed = "\033[31m"+ colorGreen = "\033[32m"+ colorYellow = "\033[33m"+ colorBlue = "\033[34m"+ colorMagenta = "\033[36m"+ styleBold = "\033[1m"+ styleReverse = "\033[7m"+ resetAll = "\033[0m"+)++// Red return content with red color+func Red(content interface{}) string {+ return fmt.Sprintf("%s%v%s", colorRed, content, resetAll)+}++// Green return content with green color+func Green(content interface{}) string {+ return fmt.Sprintf("%s%v%s", colorGreen, content, resetAll)+}++// Yellow return content with yellow color+func Yellow(content interface{}) string {+ return fmt.Sprintf("%s%v%s", colorYellow, content, resetAll)+}++// Blue return content with blue color+func Blue(content interface{}) string {+ return fmt.Sprintf("%s%v%s", colorBlue, content, resetAll)+}++// Magenta return content with magenta color+func Magenta(content interface{}) string {+ return fmt.Sprintf("%s%v%s", colorMagenta, content, resetAll)+}++// Bold return content with bold text+func Bold(content interface{}) string {+ return fmt.Sprintf("%s%v%s", styleBold, content, resetAll)+}++// Reverse return content with reverse colors+func Reverse(content interface{}) string {+ return fmt.Sprintf("%s%v%s", styleReverse, content, resetAll)+}diff --git a/app/pkg/log/logger.go b/app/pkg/log/logger.goindex b4d70aa98..e2271ce19 100644--- a/app/pkg/log/logger.go+++ b/app/pkg/log/logger.go@@ -64,7 +64,7 @@ func (l *ConsoleLogger) Errorf(format string, args ...interface{}) {l.log(ERROR, format, args...)}-// Errorf logs a ERROR message+// Error logs a ERROR messagefunc (l *ConsoleLogger) Error(err error) {l.log(ERROR, err.Error())}@@ -85,14 +85,14 @@ func (l *ConsoleLogger) log(level Level, format string, args ...interface{}) {func levelString(level Level) string {switch level {case DEBUG:- return "DEBUG"+ return Bold(Magenta("DEBUG"))case INFO:- return "INFO"+ return Bold(Blue("INFO"))case WARN:- return "WARN"+ return Bold(Yellow("WARN"))case ERROR:- return "ERROR"+ return Bold(Red("ERROR"))default:- return "???"+ return Bold(Red("???"))}}diff --git a/app/pkg/web/context.go b/app/pkg/web/context.goindex e82cc1d79..6e5c7a02c 100644--- a/app/pkg/web/context.go+++ b/app/pkg/web/context.go@@ -108,8 +108,9 @@ func (ctx *Context) NotFound() error {//Failure returns a 500 pagefunc (ctx *Context) Failure(err error) error {- ctx.Logger().Error(err)- return ctx.Render(http.StatusInternalServerError, "500.html", Map{})+ ctx.Logger().Errorf("%s", log.Red(err.Error()))+ ctx.Render(http.StatusInternalServerError, "500.html", Map{})+ return err}//HandleValidation handles given validation result property to return 400 or 500diff --git a/app/pkg/web/engine.go b/app/pkg/web/engine.goindex dc97449c6..a96458b90 100644--- a/app/pkg/web/engine.go+++ b/app/pkg/web/engine.go@@ -134,9 +134,7 @@ func (g *Group) handler(handler HandlerFunc) httprouter.Handle {params[p.Key] = p.Value}ctx := g.engine.NewContext(res, req, params)- if err := next(ctx); err != nil {- ctx.Failure(err)- }+ next(ctx)}return h}