From c67d4b3c3c7214c93f799dfacf7e9c8bf1572cba Mon Sep 17 00:00:00 2001 From: Janos Bonic <86970079+janosdebugs@users.noreply.github.com> Date: Wed, 27 Apr 2022 14:56:50 +0200 Subject: [PATCH] Adding context.Context support for logging for use in the Terraform provider --- .github/workflows/build.yml | 7 ++++++- README.md | 6 ++++++ go.mod | 2 +- go.sum | 4 ---- go_logger.go | 5 +++++ go_logger_test.go | 2 +- logger.go | 6 ++++++ noop_logger.go | 6 ++++++ test_logger.go | 5 +++++ 9 files changed, 36 insertions(+), 7 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ed827d6..1c6c8aa 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,9 +1,14 @@ name: Build on: push: + branches: + - main pull_request: schedule: - cron: '0 17 * * 2' +permissions: + contents: write + pull-requests: write jobs: fmt: name: gofmt @@ -48,7 +53,7 @@ jobs: - name: Run golangci-lint uses: golangci/golangci-lint-action@v2 with: - version: v1.32.2 + version: v1.45.2 args: --timeout=5m -E asciicheck -E bodyclose -E dupl -E errorlint -E exportloopref -E funlen codeql: name: CodeQL diff --git a/README.md b/README.md index 4d0c18d..e37c7be 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,8 @@ type Logger interface { Infof(format string, args ...interface{}) Warningf(format string, args ...interface{}) Errorf(format string, args ...interface{}) + + WithContext(ctx context.Context) Logger } ``` @@ -129,6 +131,10 @@ func (k klogLogger) Warningf(format string, args ...interface{}) { func (k klogLogger) Errorf(format string, args ...interface{}) { klog.Errorf(format, args...) } + +func (k klogLogger) WithContext(_ context.Context) Logger { + return k +} ``` You can then create a new logger copy like this: diff --git a/go.mod b/go.mod index 2790f3b..48e238b 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/ovirt/go-ovirt-client-log/v2 +module github.com/ovirt/go-ovirt-client-log/v3 go 1.16 diff --git a/go.sum b/go.sum index 7dcc7f6..e69de29 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +0,0 @@ -github.com/go-logr/logr v0.4.0 h1:K7/B1jt6fIBQVd4Owv2MqGQClcgf0R266+7C/QjRcLc= -github.com/go-logr/logr v0.4.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU= -k8s.io/klog/v2 v2.10.0 h1:R2HDMDJsHVTHA2n4RjwbeYXdOcBymXdX/JRb1v0VGhE= -k8s.io/klog/v2 v2.10.0/go.mod h1:hy9LJ/NvuK+iVyP4Ehqva4HxZG/oXyIS3n3Jmire4Ec= diff --git a/go_logger.go b/go_logger.go index c5c6bb7..468c05b 100644 --- a/go_logger.go +++ b/go_logger.go @@ -1,6 +1,7 @@ package ovirtclientlog import ( + "context" "fmt" "log" ) @@ -24,6 +25,10 @@ type goLogger struct { logger *log.Logger } +func (g *goLogger) WithContext(_ context.Context) Logger { + return g +} + func (g *goLogger) write(format string, args ...interface{}) { if g.logger == nil { log.Printf(fmt.Sprintf("%s\n", format), args...) diff --git a/go_logger_test.go b/go_logger_test.go index 859e6d9..e87618b 100644 --- a/go_logger_test.go +++ b/go_logger_test.go @@ -6,7 +6,7 @@ import ( "strings" "testing" - "github.com/ovirt/go-ovirt-client-log/v2" + "github.com/ovirt/go-ovirt-client-log/v3" ) func TestGoLogger(t *testing.T) { diff --git a/logger.go b/logger.go index d5e43cc..0160fd3 100644 --- a/logger.go +++ b/logger.go @@ -1,9 +1,15 @@ package ovirtclientlog +import "context" + // Logger provides pluggable logging for oVirt client libraries. type Logger interface { Debugf(format string, args ...interface{}) Infof(format string, args ...interface{}) Warningf(format string, args ...interface{}) Errorf(format string, args ...interface{}) + + // WithContext returns a logger that adheres to a specific context. This is useful when the backend logger needs + // access to the context the current operation is running under. + WithContext(ctx context.Context) Logger } diff --git a/noop_logger.go b/noop_logger.go index c8dbc05..7ad67bd 100644 --- a/noop_logger.go +++ b/noop_logger.go @@ -1,5 +1,7 @@ package ovirtclientlog +import "context" + // NewNOOPLogger returns a logger that does nothing. func NewNOOPLogger() Logger { return &noopLogger{} @@ -8,6 +10,10 @@ func NewNOOPLogger() Logger { type noopLogger struct { } +func (n noopLogger) WithContext(_ context.Context) Logger { + return n +} + func (n noopLogger) Debugf(_ string, _ ...interface{}) { } diff --git a/test_logger.go b/test_logger.go index f377926..0a0e2c8 100644 --- a/test_logger.go +++ b/test_logger.go @@ -1,6 +1,7 @@ package ovirtclientlog import ( + "context" "testing" ) @@ -15,6 +16,10 @@ type testLogger struct { t *testing.T } +func (t *testLogger) WithContext(_ context.Context) Logger { + return t +} + func (t *testLogger) Debugf(format string, args ...interface{}) { t.t.Helper() t.t.Logf(format, args...)