mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-11 22:20:18 +00:00
Create passthroughLogger when LogOf is called and request has no logger
This commit is contained in:
@@ -40,6 +40,10 @@ func Handler(delegate http.Handler, pred StacktracePred) http.Handler {
|
||||
// StacktracePred returns true if a stacktrace should be logged for this status.
|
||||
type StacktracePred func(httpStatus int) (logStacktrace bool)
|
||||
|
||||
type logger interface {
|
||||
Addf(format string, data ...interface{})
|
||||
}
|
||||
|
||||
// Add a layer on top of ResponseWriter, so we can track latency and error
|
||||
// message sources.
|
||||
type respLogger struct {
|
||||
@@ -54,6 +58,14 @@ type respLogger struct {
|
||||
logStacktracePred StacktracePred
|
||||
}
|
||||
|
||||
// Simple logger that logs immediately when Addf is called
|
||||
type passthroughLogger struct{}
|
||||
|
||||
// Addf logs info immediately.
|
||||
func (passthroughLogger) Addf(format string, data ...interface{}) {
|
||||
glog.Infof(format, data...)
|
||||
}
|
||||
|
||||
// DefaultStacktracePred is the default implementation of StacktracePred.
|
||||
func DefaultStacktracePred(status int) bool {
|
||||
return status < http.StatusOK || status >= http.StatusBadRequest
|
||||
@@ -87,11 +99,12 @@ func NewLogged(req *http.Request, w *http.ResponseWriter) *respLogger {
|
||||
}
|
||||
|
||||
// LogOf returns the logger hiding in w. If there is not an existing logger
|
||||
// then one will be created because NewLogged() must have been previously
|
||||
// called for the log to work.
|
||||
func LogOf(req *http.Request, w http.ResponseWriter) *respLogger {
|
||||
// then a passthroughLogger will be created which will log to stdout immediately
|
||||
// when Addf is called.
|
||||
func LogOf(req *http.Request, w http.ResponseWriter) logger {
|
||||
if _, exists := w.(*respLogger); !exists {
|
||||
NewLogged(req, &w)
|
||||
pl := &passthroughLogger{}
|
||||
return pl
|
||||
}
|
||||
if rl, ok := w.(*respLogger); ok {
|
||||
return rl
|
||||
|
Reference in New Issue
Block a user