mg-transport-core/core/logger/attrs.go

95 lines
2.5 KiB
Go
Raw Normal View History

2023-10-18 14:27:23 +03:00
package logger
2023-10-18 15:19:01 +03:00
import (
2023-10-18 16:55:46 +03:00
"fmt"
2024-06-07 17:55:45 +03:00
"io"
2023-10-18 15:19:01 +03:00
"net/http"
2023-11-24 15:23:07 +03:00
2024-06-14 18:10:48 +03:00
json "github.com/goccy/go-json"
2023-11-24 15:23:07 +03:00
"go.uber.org/zap"
2023-10-18 15:19:01 +03:00
)
2023-10-18 14:27:23 +03:00
2024-06-07 17:55:45 +03:00
// HandlerAttr represents the attribute name for the handler.
const HandlerAttr = "handler"
// ConnectionAttr represents the attribute name for the connection.
const ConnectionAttr = "connection"
// AccountAttr represents the attribute name for the account.
const AccountAttr = "account"
// CounterIDAttr represents the attribute name for the counter ID.
const CounterIDAttr = "counterId"
// ErrorAttr represents the attribute name for an error.
const ErrorAttr = "error"
// FailureMessageAttr represents the attribute name for a failure message.
const FailureMessageAttr = "failureMessage"
// BodyAttr represents the attribute name for the request body.
const BodyAttr = "body"
// HTTPMethodAttr represents the attribute name for the HTTP method.
const HTTPMethodAttr = "method"
// HTTPStatusAttr represents the attribute name for the HTTP status code.
const HTTPStatusAttr = "statusCode"
// HTTPStatusNameAttr represents the attribute name for the HTTP status name.
const HTTPStatusNameAttr = "statusName"
2023-10-18 14:27:23 +03:00
2024-06-07 17:55:45 +03:00
// Err returns a zap.Field with the given error value.
2023-11-24 15:23:07 +03:00
func Err(err any) zap.Field {
2023-10-18 14:27:23 +03:00
if err == nil {
2023-11-24 15:23:07 +03:00
return zap.String(ErrorAttr, "<nil>")
2023-10-18 14:27:23 +03:00
}
2023-11-24 15:23:07 +03:00
return zap.Any(ErrorAttr, err)
}
2024-06-07 17:55:45 +03:00
// Handler returns a zap.Field with the given handler name.
2023-11-24 15:23:07 +03:00
func Handler(name string) zap.Field {
return zap.String(HandlerAttr, name)
2023-10-18 14:27:23 +03:00
}
2023-10-18 15:19:01 +03:00
2024-06-07 17:55:45 +03:00
// HTTPStatusCode returns a zap.Field with the given HTTP status code.
2023-11-24 15:23:07 +03:00
func HTTPStatusCode(code int) zap.Field {
return zap.Int(HTTPStatusAttr, code)
2023-10-18 15:36:55 +03:00
}
2024-06-07 17:55:45 +03:00
// HTTPStatusName returns a zap.Field with the given HTTP status name.
2023-11-24 15:23:07 +03:00
func HTTPStatusName(code int) zap.Field {
return zap.String(HTTPStatusNameAttr, http.StatusText(code))
2023-10-18 15:19:01 +03:00
}
2023-10-18 15:30:01 +03:00
2024-06-07 17:55:45 +03:00
// Body returns a zap.Field with the given request body value.
2023-11-24 15:23:07 +03:00
func Body(val any) zap.Field {
2023-10-18 16:55:46 +03:00
switch item := val.(type) {
case string:
2024-06-07 17:55:45 +03:00
var m map[string]interface{}
if err := json.Unmarshal([]byte(item), &m); err == nil {
return zap.Any(BodyAttr, m)
}
2023-11-24 15:23:07 +03:00
return zap.String(BodyAttr, item)
2023-10-18 16:55:46 +03:00
case []byte:
2024-06-07 17:55:45 +03:00
var m interface{}
if err := json.Unmarshal(item, &m); err == nil {
return zap.Any(BodyAttr, m)
}
2023-11-24 15:23:07 +03:00
return zap.String(BodyAttr, string(item))
2024-06-07 17:55:45 +03:00
case io.Reader:
data, err := io.ReadAll(item)
if err != nil {
return zap.String(BodyAttr, fmt.Sprintf("%#v", val))
}
var m interface{}
if err := json.Unmarshal(data, &m); err == nil {
return zap.Any(BodyAttr, m)
}
return zap.String(BodyAttr, string(data))
2023-10-18 16:55:46 +03:00
default:
2023-11-24 15:23:07 +03:00
return zap.String(BodyAttr, fmt.Sprintf("%#v", val))
2023-10-18 16:55:46 +03:00
}
2023-10-18 15:30:01 +03:00
}