From 89470619459b63e4d763fc3ee990ac1d46904820 Mon Sep 17 00:00:00 2001 From: Tucker Gary Siegel <tgsiegel@terpmail.umd.edu> Date: Wed, 18 Aug 2021 19:46:23 -0400 Subject: [PATCH] update levels --- logger/logger.go | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/logger/logger.go b/logger/logger.go index 4957fe5..4a41cef 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -16,6 +16,7 @@ import ( type RequestLog struct { Date string PID string + Level string RequestId string Error *errors.DawnError StatusCode string @@ -29,6 +30,7 @@ type Request struct { type MessageLog struct { Date string + Level string PID string RequestId string Message string @@ -63,6 +65,7 @@ func BuildMessage(c *fiber.Ctx) RequestLog { message := RequestLog{ Date: time.Now().UTC().Format(layout), RequestId: fmt.Sprintf("%s", requestId), + Level: "INFO", StatusCode: strconv.Itoa(c.Response().StatusCode()), Method: c.Method(), Path: c.Route().Path, @@ -73,11 +76,14 @@ func BuildMessage(c *fiber.Ctx) RequestLog { func LogRequest(message RequestLog) { logString := "" + if message.Error != nil { + message.Level = "ERROR" + } if viper.GetString("app.logType") == "json" { tempLogString, _ := json.MarshalIndent(message, "", " ") logString = string(tempLogString) } else { - logString = fmt.Sprintf("%s %s %s %s - %s %s", message.Date, message.PID, message.RequestId, message.StatusCode, message.Method, message.Path) + logString = fmt.Sprintf("[%s] %s %s %s %s - %s %s", message.Level, message.Date, message.PID, message.RequestId, message.StatusCode, message.Method, message.Path) if message.Error != nil { logString += " - " + message.Error.Error() } @@ -125,33 +131,41 @@ func ErrorHandler(ctx *fiber.Ctx, err error) *errors.DawnError { func stringToLevel(str string) int { switch str { - case "DEBUG": + case "TRACE": return 1 - case "INFO": + case "DEBUG": return 2 + case "INFO": + return 3 } return 1 } +func TRACE(c *fiber.Ctx, message string) { + if stringToLevel("TRACE") >= stringToLevel(viper.GetString("app.logLevel")) { + _log(c, "TRACE", message) + } +} func DEBUG(c *fiber.Ctx, message string) { if stringToLevel("DEBUG") >= stringToLevel(viper.GetString("app.logLevel")) { - _log(c, message) + _log(c, "DEBUG", message) } } func INFO(c *fiber.Ctx, message string) { if stringToLevel("INFO") >= stringToLevel(viper.GetString("app.logLevel")) { - _log(c, message) + _log(c, "INFO", message) } } -func _log(c *fiber.Ctx, message string) { +func _log(c *fiber.Ctx, level, message string) { lg := buildMessageLog(c, message) + lg.Level = level logString := "" if viper.GetString("app.logType") == "json" { tempLogString, _ := json.MarshalIndent(lg, "", " ") logString = string(tempLogString) } else { - logString = fmt.Sprintf("%s %s %s %s", lg.Date, lg.PID, lg.RequestId, lg.Message) + logString = fmt.Sprintf("[%s] %s %s %s %s", lg.Level, lg.Date, lg.PID, lg.RequestId, lg.Message) } fmt.Println(logString) } -- GitLab