Skip to content

Commit c20f085

Browse files
gdavisonbbasata
andauthored
Adds logging sink that uses the standard logger (#162)
* Renames `newSink` to `newTestSink` * Adds log sink that uses the standard logger * Add changelog entry --------- Co-authored-by: Baraa Basata <bbasata@gmail.com>
1 parent 5926d5b commit c20f085

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: FEATURES
2+
body: Added `tfsdklog.RegisterStdlogSink()` for test sweeper logging
3+
time: 2025-10-15T12:15:24.302228-04:00
4+
custom:
5+
Issue: "162"

tfsdklog/sink.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"fmt"
99
"io"
10+
"log"
1011
"os"
1112
"strings"
1213
"sync"
@@ -75,7 +76,7 @@ var invalidLogLevelMessage sync.Once
7576
// no longer maintained. Use ContextWithTestLogging instead of
7677
// RegisterTestSink.
7778
func RegisterTestSink(ctx context.Context, t testing.T) context.Context {
78-
logger, loggerOptions := newSink(t.Name())
79+
logger, loggerOptions := newTestSink(t.Name())
7980

8081
ctx = logging.SetSink(ctx, logger)
8182
ctx = logging.SetSinkOptions(ctx, loggerOptions)
@@ -93,15 +94,15 @@ func RegisterTestSink(ctx context.Context, t testing.T) context.Context {
9394
// ContextWithTestLogging must be called prior to any loggers being setup or
9495
// instantiated.
9596
func ContextWithTestLogging(ctx context.Context, testName string) context.Context {
96-
logger, loggerOptions := newSink(testName)
97+
logger, loggerOptions := newTestSink(testName)
9798

9899
ctx = logging.SetSink(ctx, logger)
99100
ctx = logging.SetSinkOptions(ctx, loggerOptions)
100101

101102
return ctx
102103
}
103104

104-
func newSink(testName string) (hclog.Logger, *hclog.LoggerOptions) {
105+
func newTestSink(testName string) (hclog.Logger, *hclog.LoggerOptions) {
105106
logOutput := io.Writer(os.Stderr)
106107
var json bool
107108
var logLevel hclog.Level
@@ -176,3 +177,30 @@ func isValidLogLevel(level string) bool {
176177

177178
return false
178179
}
180+
181+
// RegisterStdlogSink sets up a logging sink for use with test sweepers and
182+
// other cases where plugin logs don't get routed through Terraform and the
183+
// built-in Go `log` package is also used.
184+
//
185+
// RegisterStdlogSink should only ever be called by test sweepers, providers
186+
// should never call it.
187+
//
188+
// RegisterStdlogSink must be called prior to any loggers being setup or
189+
// instantiated.
190+
func RegisterStdlogSink(ctx context.Context) context.Context {
191+
logger, loggerOptions := newStdlogSink()
192+
193+
ctx = logging.SetSink(ctx, logger)
194+
ctx = logging.SetSinkOptions(ctx, loggerOptions)
195+
196+
return ctx
197+
}
198+
199+
func newStdlogSink() (hclog.Logger, *hclog.LoggerOptions) {
200+
loggerOptions := &hclog.LoggerOptions{
201+
IndependentLevels: true,
202+
JSONFormat: false,
203+
}
204+
205+
return hclog.FromStandardLogger(log.Default(), loggerOptions), loggerOptions
206+
}

0 commit comments

Comments
 (0)