fleet/server/service/logging.go
Zach Wasserman d827a9d6b7
Log all errors regardless of log level (#496)
Modify the loggerDebug function to check whether the error is nil before
choosing the log level.

Fixes #473
2021-03-18 16:06:06 -07:00

32 lines
874 B
Go

package service
import (
"github.com/fleetdm/fleet/server/kolide"
kitlog "github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
)
// logging middleware logs the service actions
type loggingMiddleware struct {
kolide.Service
logger kitlog.Logger
}
// NewLoggingService takes an existing service and adds a logging wrapper
func NewLoggingService(svc kolide.Service, logger kitlog.Logger) kolide.Service {
return loggingMiddleware{Service: svc, logger: logger}
}
// loggerDebug returns the the info level if there error is non-nil, otherwise defaulting to the debug level.
func (mw loggingMiddleware) loggerDebug(err error) kitlog.Logger {
if err != nil {
return level.Info(mw.logger)
}
return level.Debug(mw.logger)
}
// loggerInfo returns the info level
func (mw loggingMiddleware) loggerInfo(err error) kitlog.Logger {
return level.Info(mw.logger)
}