Skip to content

Commit

Permalink
Wire up xlog to retryablehttp
Browse files Browse the repository at this point in the history
  • Loading branch information
jasoncodes committed Feb 15, 2021
1 parent a021a47 commit b6fd533
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/app/xhttp/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
38 changes: 38 additions & 0 deletions internal/pkg/xlog/xlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit b6fd533

Please sign in to comment.