mirror of
https://github.com/smallstep/certificates.git
synced 2024-11-11 07:11:00 +00:00
00634fb648
* api/render: initial implementation of the package * acme/api: refactored to support api/render * authority/admin: refactored to support api/render * ca: refactored to support api/render * api: refactored to support api/render * api/render: implemented Error * api: refactored to support api/render.Error * acme/api: refactored to support api/render.Error * authority/admin: refactored to support api/render.Error * ca: refactored to support api/render.Error * ca: fixed broken tests * api/render, api/log: moved error logging to this package * acme: refactored Error so that it implements render.RenderableError * authority/admin: refactored Error so that it implements render.RenderableError * api/render: implemented RenderableError * api/render: added test coverage for Error * api/render: implemented statusCodeFromError * api: refactored RootsPEM to work with render.Error * acme, authority/admin: fixed pointer receiver name for consistency * api/render, errs: moved StatusCoder & StackTracer to the render package
80 lines
1.5 KiB
Go
80 lines
1.5 KiB
Go
// Package log implements API-related logging helpers.
|
|
package log
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/smallstep/certificates/logging"
|
|
)
|
|
|
|
// StackTracedError is the set of errors implementing the StackTrace function.
|
|
//
|
|
// Errors implementing this interface have their stack traces logged when passed
|
|
// to the Error function of this package.
|
|
type StackTracedError interface {
|
|
error
|
|
|
|
StackTrace() errors.StackTrace
|
|
}
|
|
|
|
// Error adds to the response writer the given error if it implements
|
|
// logging.ResponseLogger. If it does not implement it, then writes the error
|
|
// using the log package.
|
|
func Error(rw http.ResponseWriter, err error) {
|
|
rl, ok := rw.(logging.ResponseLogger)
|
|
if !ok {
|
|
log.Println(err)
|
|
|
|
return
|
|
}
|
|
|
|
rl.WithFields(map[string]interface{}{
|
|
"error": err,
|
|
})
|
|
|
|
if os.Getenv("STEPDEBUG") != "1" {
|
|
return
|
|
}
|
|
|
|
e, ok := err.(StackTracedError)
|
|
if !ok {
|
|
e, ok = errors.Cause(err).(StackTracedError)
|
|
}
|
|
|
|
if ok {
|
|
rl.WithFields(map[string]interface{}{
|
|
"stack-trace": fmt.Sprintf("%+v", e.StackTrace()),
|
|
})
|
|
}
|
|
}
|
|
|
|
// EnabledResponse log the response object if it implements the EnableLogger
|
|
// interface.
|
|
func EnabledResponse(rw http.ResponseWriter, v interface{}) {
|
|
type enableLogger interface {
|
|
ToLog() (interface{}, error)
|
|
}
|
|
|
|
if el, ok := v.(enableLogger); ok {
|
|
out, err := el.ToLog()
|
|
if err != nil {
|
|
Error(rw, err)
|
|
|
|
return
|
|
}
|
|
|
|
if rl, ok := rw.(logging.ResponseLogger); ok {
|
|
rl.WithFields(map[string]interface{}{
|
|
"response": out,
|
|
})
|
|
} else {
|
|
log.Println(out)
|
|
}
|
|
}
|
|
}
|