Skip to content

Commit

Permalink
Adding context.Context support for logging for use in the Terraform p…
Browse files Browse the repository at this point in the history
…rovider
  • Loading branch information
Janos Bonic committed Apr 27, 2022
1 parent 6da1b74 commit 1a771dc
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 7 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
pull_request:
schedule:
- cron: '0 17 * * 2'
permissions:
contents: write
pull-requests: write
jobs:
fmt:
name: gofmt
Expand Down Expand Up @@ -48,7 +51,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
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```

Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
5 changes: 5 additions & 0 deletions go_logger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ovirtclientlog

import (
"context"
"fmt"
"log"
)
Expand All @@ -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...)
Expand Down
2 changes: 1 addition & 1 deletion go_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 6 additions & 0 deletions noop_logger.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ovirtclientlog

import "context"

// NewNOOPLogger returns a logger that does nothing.
func NewNOOPLogger() Logger {
return &noopLogger{}
Expand All @@ -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{}) {

}
Expand Down
5 changes: 5 additions & 0 deletions test_logger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ovirtclientlog

import (
"context"
"testing"
)

Expand All @@ -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...)
Expand Down

0 comments on commit 1a771dc

Please sign in to comment.