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"
|
2023-10-18 15:19:01 +03:00
|
|
|
"net/http"
|
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
|
|
|
|
|
|
|
const (
|
|
|
|
HandlerAttr = "handler"
|
|
|
|
ConnectionAttr = "connection"
|
|
|
|
AccountAttr = "account"
|
|
|
|
CounterIDAttr = "counterId"
|
|
|
|
ErrorAttr = "error"
|
|
|
|
FailureMessageAttr = "failureMessage"
|
2023-10-18 15:30:01 +03:00
|
|
|
BodyAttr = "body"
|
2023-10-18 18:16:59 +03:00
|
|
|
HTTPMethodAttr = "method"
|
|
|
|
HTTPStatusAttr = "statusCode"
|
|
|
|
HTTPStatusNameAttr = "statusName"
|
2023-10-18 14:27:23 +03:00
|
|
|
)
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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:
|
2023-11-24 15:23:07 +03:00
|
|
|
return zap.String(BodyAttr, item)
|
2023-10-18 16:55:46 +03:00
|
|
|
case []byte:
|
2023-11-24 15:23:07 +03:00
|
|
|
return zap.String(BodyAttr, string(item))
|
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
|
|
|
}
|