From b6fd533436c58eb918621e5671abad5a02db18e9 Mon Sep 17 00:00:00 2001 From: Jason Weathered Date: Mon, 15 Feb 2021 12:05:22 +1000 Subject: [PATCH] Wire up xlog to retryablehttp Wrapper inspired by https://github.com/hashicorp/go-retryablehttp/pull/101#issuecomment-735206810. --- internal/app/xhttp/handler.go | 3 +++ internal/pkg/xlog/xlog.go | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/internal/app/xhttp/handler.go b/internal/app/xhttp/handler.go index 445febc5..681090a2 100644 --- a/internal/app/xhttp/handler.go +++ b/internal/app/xhttp/handler.go @@ -15,6 +15,7 @@ import ( "github.com/thecodingmachine/gotenberg/internal/pkg/conf" "github.com/thecodingmachine/gotenberg/internal/pkg/printer" "github.com/thecodingmachine/gotenberg/internal/pkg/xerror" + "github.com/thecodingmachine/gotenberg/internal/pkg/xlog" "github.com/thecodingmachine/gotenberg/internal/pkg/xrand" "github.com/thecodingmachine/gotenberg/internal/pkg/xtime" ) @@ -344,6 +345,7 @@ func convertAsync(ctx context.Context, p printer.Printer, filename, fpath string webhookURL, ) httpClient := retryablehttp.NewClient() + httpClient.Logger = retryablehttp.LeveledLogger(xlog.NewLeveledLogger(logger, op)) httpClient.HTTPClient.Timeout = xtime.Duration(webhookURLTimeout) req, err := retryablehttp.NewRequest(http.MethodPost, webhookURL, f) if err != nil { @@ -418,6 +420,7 @@ func sendToErrorWebhook(ctx context.Context, xerr error) { } req.Header.Set(echo.HeaderContentType, "application/json") httpClient := retryablehttp.NewClient() + httpClient.Logger = retryablehttp.LeveledLogger(xlog.NewLeveledLogger(logger, op)) httpClient.HTTPClient.Timeout = xtime.Duration(webhookURLTimeout) resp, err := httpClient.Do(req) /* #nosec */ if err != nil { diff --git a/internal/pkg/xlog/xlog.go b/internal/pkg/xlog/xlog.go index af571d91..5b990dcb 100644 --- a/internal/pkg/xlog/xlog.go +++ b/internal/pkg/xlog/xlog.go @@ -139,3 +139,41 @@ func (l Logger) ErrorOpf(op, format string, args ...interface{}) { func (l Logger) FatalOp(op string, err error) { l.entry.WithField("op", op).Fatal(err.Error()) } + +type LeveledLogger struct { + logger Logger + op string +} + +func NewLeveledLogger(logger Logger, op string) *LeveledLogger { + return &LeveledLogger{ + logger: logger, + op: op, + } +} + +func (l *LeveledLogger) fields(keysAndValues ...interface{}) map[string]interface{} { + fields := make(map[string]interface{}) + + for i := 0; i < len(keysAndValues)-1; i += 2 { + fields[keysAndValues[i].(string)] = keysAndValues[i+1] + } + + return fields +} + +func (l *LeveledLogger) Error(msg string, keysAndValues ...interface{}) { + l.logger.WithFields(l.fields(keysAndValues...)).ErrorOpf(l.op, "%s", msg) +} + +func (l *LeveledLogger) Info(msg string, keysAndValues ...interface{}) { + l.logger.WithFields(l.fields(keysAndValues...)).InfoOpf(l.op, "%s", msg) +} + +func (l *LeveledLogger) Debug(msg string, keysAndValues ...interface{}) { + l.logger.WithFields(l.fields(keysAndValues...)).DebugOpf(l.op, "%s", msg) +} + +func (l *LeveledLogger) Warn(msg string, keysAndValues ...interface{}) { + l.logger.WithFields(l.fields(keysAndValues...)).ErrorOpf(l.op, "%s", msg) +}