Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Fixed logging for underlying Go SDK #3917

Merged
merged 8 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/acceptance/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func init() {
rand.Seed(time.Now().UnixMicro())
databricks.WithProduct("tf-integration-tests", common.Version())
os.Setenv("TF_LOG", "DEBUG")
dbproviderlogger.SetLogger()
dbproviderlogger.SetLogger(context.Background())
}

func workspaceLevel(t *testing.T, steps ...step) {
Expand Down
36 changes: 25 additions & 11 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type TfLogger struct {
Name string
}

var loggerContext context.Context

// This function is always enabled because TfLogger implements the Logger interface from Go SDK and there we check
// if the logging is enabled based on level (which default to Info).
// This however isn't possible here since tflog isn't enabled / disabled based on log level.
Expand All @@ -22,45 +24,57 @@ func (tfLogger *TfLogger) Enabled(_ context.Context, _ logger.Level) bool {

func (tfLogger *TfLogger) Tracef(ctx context.Context, format string, v ...any) {
if tfLogger == nil {
tflog.Trace(ctx, fmt.Sprintf(format, v...), nil)
tflog.Trace(getLoggerValidContext(ctx), fmt.Sprintf(format, v...), nil)
} else {
tflog.SubsystemTrace(ctx, tfLogger.Name, fmt.Sprintf(format, v...), nil)
tflog.SubsystemTrace(getLoggerValidContext(ctx), tfLogger.Name, fmt.Sprintf(format, v...), nil)
}
}

func (tfLogger *TfLogger) Debugf(ctx context.Context, format string, v ...any) {
if tfLogger == nil {
tflog.Debug(ctx, fmt.Sprintf(format, v...), nil)
tflog.Debug(getLoggerValidContext(ctx), fmt.Sprintf(format, v...), nil)
} else {
tflog.SubsystemDebug(ctx, tfLogger.Name, fmt.Sprintf(format, v...), nil)
tflog.SubsystemDebug(getLoggerValidContext(ctx), tfLogger.Name, fmt.Sprintf(format, v...), nil)
}
}

func (tfLogger *TfLogger) Infof(ctx context.Context, format string, v ...any) {
if tfLogger == nil {
tflog.Info(ctx, fmt.Sprintf(format, v...), nil)
tflog.Info(getLoggerValidContext(ctx), fmt.Sprintf(format, v...), nil)
} else {
tflog.SubsystemInfo(ctx, tfLogger.Name, fmt.Sprintf(format, v...), nil)
tflog.SubsystemInfo(getLoggerValidContext(ctx), tfLogger.Name, fmt.Sprintf(format, v...), nil)
}
}

func (tfLogger *TfLogger) Warnf(ctx context.Context, format string, v ...any) {
if tfLogger == nil {
tflog.Warn(ctx, fmt.Sprintf(format, v...), nil)
tflog.Warn(getLoggerValidContext(ctx), fmt.Sprintf(format, v...), nil)
} else {
tflog.SubsystemWarn(ctx, tfLogger.Name, fmt.Sprintf(format, v...), nil)
tflog.SubsystemWarn(getLoggerValidContext(ctx), tfLogger.Name, fmt.Sprintf(format, v...), nil)
}
}

func (tfLogger *TfLogger) Errorf(ctx context.Context, format string, v ...any) {
if tfLogger == nil {
tflog.Error(ctx, fmt.Sprintf(format, v...), nil)
tflog.Error(getLoggerValidContext(ctx), fmt.Sprintf(format, v...), nil)
} else {
tflog.SubsystemError(ctx, tfLogger.Name, fmt.Sprintf(format, v...), nil)
tflog.SubsystemError(getLoggerValidContext(ctx), tfLogger.Name, fmt.Sprintf(format, v...), nil)
}
}

// Tflogger is called through Databricks provider and also Go SDK
// We need a way to check if the context is correctly set for logging, if not then we use the context we define in configureContextFunc
// We can't use the logging.GetProviderRootLogger(ctx) to check as it is internal
func getLoggerValidContext(ctx context.Context) context.Context {
logger := ctx.Value("provider")
if logger == nil {
return loggerContext
}
return ctx
}

func SetLogger() {
func SetLogger(ctx context.Context) {
renaudhartert-db marked this conversation as resolved.
Show resolved Hide resolved
var tfLogger *TfLogger
logger.DefaultLogger = tfLogger
loggerContext = ctx
}
2 changes: 1 addition & 1 deletion logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ func TestTfLogger_Enabled(t *testing.T) {
}

func TestSetLogger(t *testing.T) {
SetLogger()
SetLogger(context.Background())
assert.IsType(t, &TfLogger{}, goLogger.DefaultLogger)
}
2 changes: 1 addition & 1 deletion provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func DatabricksProvider() *schema.Provider {
if p.TerraformVersion != "" {
useragent.WithUserAgentExtra("terraform", p.TerraformVersion)
}
tflogger.SetLogger()
tflogger.SetLogger(ctx)
return ConfigureDatabricksClient(ctx, d)
}
common.AddContextToAllResources(p, "databricks")
Expand Down
Loading