Skip to content

Commit

Permalink
response body updated
Browse files Browse the repository at this point in the history
  • Loading branch information
muzamil-apy committed Sep 19, 2023
1 parent d6c29b3 commit 89333b1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
3 changes: 3 additions & 0 deletions constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package logware

const UnknownContentType = "Unknown Content-Type"
13 changes: 9 additions & 4 deletions logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (r *responseData) formatLog() string {
}

if r.Body != nil {
if !isJson(r.Headers["Content-Type"][0]) {
if len(r.Headers["Content-Type"]) > 0 && !isJson(r.Headers["Content-Type"][0]) {
logString = append(logString, getLogPart(`"body"`, getQuotedOrJson(r.Body)))
} else {
logString = append(logString, fmt.Sprintf(`"body": %s`, r.Body))
Expand All @@ -230,11 +230,16 @@ func (rw *customResponseWriter) WriteHeader(statusCode int) {
func (rw *customResponseWriter) Write(b []byte) (int, error) {
rw.LogData.Response.Headers = rw.ResponseWriter.Header()
contentType := rw.ResponseWriter.Header().Get("Content-Type")
if isStringRepresent(contentType) {
rw.LogData.Response.Body = b
if strings.TrimSpace(contentType) != "" {
if isStringRepresent(contentType) {
rw.LogData.Response.Body = b
} else {
rw.LogData.Response.Body = []byte(contentType + " data")
}
} else {
rw.LogData.Response.Body = []byte(contentType + " data")
rw.LogData.Response.Body = []byte(UnknownContentType)
}

if !(200 <= rw.LogData.StatusCode && rw.LogData.StatusCode <= 299) {
stackTrace := strings.Split(strings.ReplaceAll(captureStackTrace(), "\t", ""), "\n")
rw.LogData.StackTrace = stackTrace[:len(stackTrace)-1]
Expand Down

0 comments on commit 89333b1

Please sign in to comment.