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
|
|
|
"log/slog"
|
|
|
|
"net/http"
|
|
|
|
)
|
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-10-18 16:55:46 +03:00
|
|
|
func Err(err any) slog.Attr {
|
2023-10-18 14:27:23 +03:00
|
|
|
if err == nil {
|
|
|
|
return slog.String(ErrorAttr, "<nil>")
|
|
|
|
}
|
|
|
|
return slog.Any(ErrorAttr, err)
|
|
|
|
}
|
2023-10-18 15:19:01 +03:00
|
|
|
|
2023-10-18 15:36:55 +03:00
|
|
|
func HTTPStatusCode(code int) slog.Attr {
|
|
|
|
return slog.Int(HTTPStatusAttr, code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func HTTPStatusName(code int) slog.Attr {
|
|
|
|
return slog.String(HTTPStatusNameAttr, http.StatusText(code))
|
2023-10-18 15:19:01 +03:00
|
|
|
}
|
2023-10-18 15:30:01 +03:00
|
|
|
|
|
|
|
func Body(val any) slog.Attr {
|
2023-10-18 16:55:46 +03:00
|
|
|
switch item := val.(type) {
|
|
|
|
case string:
|
|
|
|
return slog.String(BodyAttr, item)
|
|
|
|
case []byte:
|
|
|
|
return slog.String(BodyAttr, string(item))
|
|
|
|
default:
|
|
|
|
return slog.String(BodyAttr, fmt.Sprintf("%#v", val))
|
|
|
|
}
|
2023-10-18 15:30:01 +03:00
|
|
|
}
|