api/log: removed dependency to certificates/logging

This commit is contained in:
Panagiotis Siatras 2022-11-17 16:04:21 +02:00
parent b7f4881972
commit 9197de3e96
No known key found for this signature in database

View file

@ -7,8 +7,6 @@ import (
"os" "os"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/smallstep/certificates/logging"
) )
// StackTracedError is the set of errors implementing the StackTrace function. // StackTracedError is the set of errors implementing the StackTrace function.
@ -21,16 +19,20 @@ type StackTracedError interface {
StackTrace() errors.StackTrace StackTrace() errors.StackTrace
} }
type fieldCarrier interface {
WithFields(map[string]any)
}
// Error adds to the response writer the given error if it implements // Error adds to the response writer the given error if it implements
// logging.ResponseLogger. If it does not implement it, then writes the error // logging.ResponseLogger. If it does not implement it, then writes the error
// using the log package. // using the log package.
func Error(rw http.ResponseWriter, err error) { func Error(rw http.ResponseWriter, err error) {
rl, ok := rw.(logging.ResponseLogger) fc, ok := rw.(fieldCarrier)
if !ok { if !ok {
return return
} }
rl.WithFields(map[string]interface{}{ fc.WithFields(map[string]any{
"error": err, "error": err,
}) })
@ -40,7 +42,7 @@ func Error(rw http.ResponseWriter, err error) {
var st StackTracedError var st StackTracedError
if errors.As(err, &st) { if errors.As(err, &st) {
rl.WithFields(map[string]interface{}{ fc.WithFields(map[string]any{
"stack-trace": fmt.Sprintf("%+v", st.StackTrace()), "stack-trace": fmt.Sprintf("%+v", st.StackTrace()),
}) })
} }
@ -48,9 +50,9 @@ func Error(rw http.ResponseWriter, err error) {
// EnabledResponse log the response object if it implements the EnableLogger // EnabledResponse log the response object if it implements the EnableLogger
// interface. // interface.
func EnabledResponse(rw http.ResponseWriter, v interface{}) { func EnabledResponse(rw http.ResponseWriter, v any) {
type enableLogger interface { type enableLogger interface {
ToLog() (interface{}, error) ToLog() (any, error)
} }
if el, ok := v.(enableLogger); ok { if el, ok := v.(enableLogger); ok {
@ -61,8 +63,8 @@ func EnabledResponse(rw http.ResponseWriter, v interface{}) {
return return
} }
if rl, ok := rw.(logging.ResponseLogger); ok { if rl, ok := rw.(fieldCarrier); ok {
rl.WithFields(map[string]interface{}{ rl.WithFields(map[string]any{
"response": out, "response": out,
}) })
} }