Skip to content
Snippets Groups Projects
Commit 89470619 authored by Tucker Gary Siegel's avatar Tucker Gary Siegel
Browse files

update levels

parent 7c40a11a
No related branches found
No related tags found
No related merge requests found
......@@ -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)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment