From 84d71d0c22ef7f7083bc08a1f85205ad53acde92 Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Tue, 12 Apr 2022 14:55:39 -0400 Subject: [PATCH] internal/logging: Ensure `@caller` in SDK subsystem logging entries accurately reflects calling code location Reference: https://github.com/hashicorp/terraform-plugin-go/pull/179 Previously: ``` 2022-04-12T11:11:26.086-0400 [TRACE] provider.terraform-provider-aws: Found data source type: @caller=/Users/bflad/src/github.com/hashicorp/terraform-plugin-sdk/internal/logging/helper_schema.go:21 @module=sdk.helper_schema tf_data_source_type=aws_connect_bot_association timestamp=2022-04-12T11:11:25.917-0400 2022-04-12T11:11:26.086-0400 [TRACE] provider.terraform-provider-aws: Found data source type: @caller=/Users/bflad/src/github.com/hashicorp/terraform-plugin-sdk/internal/logging/helper_schema.go:21 @module=sdk.helper_schema tf_data_source_type=aws_datapipeline_pipeline timestamp=2022-04-12T11:11:25.917-0400 ``` Now: ``` 2022-04-12T14:54:22.571-0400 [TRACE] provider.terraform-provider-aws: Found data source type: @module=sdk.helper_schema tf_data_source_type=aws_connect_bot_association @caller=/Users/bflad/src/github.com/hashicorp/terraform-plugin-sdk/helper/schema/grpc_provider.go:95 timestamp=2022-04-12T14:54:22.571-0400 2022-04-12T14:54:22.574-0400 [TRACE] provider.terraform-provider-aws: Found data source type: @module=sdk.helper_schema tf_data_source_type=aws_datapipeline_pipeline @caller=/Users/bflad/src/github.com/hashicorp/terraform-plugin-sdk/helper/schema/grpc_provider.go:95 timestamp=2022-04-12T14:54:22.574-0400 ``` --- .changelog/pending.txt | 7 +++++++ internal/logging/context.go | 12 ++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 .changelog/pending.txt diff --git a/.changelog/pending.txt b/.changelog/pending.txt new file mode 100644 index 0000000000..08c6a89177 --- /dev/null +++ b/.changelog/pending.txt @@ -0,0 +1,7 @@ +```release-note:bug +helper/resource: Ensured `@caller` in SDK logging entries accurately reflected calling code location +``` + +```release-note:bug +helper/schema: Ensured `@caller` in SDK logging entries accurately reflected calling code location +``` diff --git a/internal/logging/context.go b/internal/logging/context.go index 7ab00a1821..a92cd86b21 100644 --- a/internal/logging/context.go +++ b/internal/logging/context.go @@ -13,7 +13,11 @@ import ( // already have the root SDK logger and root provider logger setup from // terraform-plugin-go tf5server RPC handlers. func InitContext(ctx context.Context) context.Context { - ctx = tfsdklog.NewSubsystem(ctx, SubsystemHelperSchema, tfsdklog.WithLevelFromEnv(EnvTfLogSdkHelperSchema)) + ctx = tfsdklog.NewSubsystem(ctx, SubsystemHelperSchema, tfsdklog.Options{ + // All calls are through the HelperSchema* helper functions + tfsdklog.WithAdditionalLocationOffset(1), + tfsdklog.WithLevelFromEnv(EnvTfLogSdkHelperSchema), + }...) return ctx } @@ -30,7 +34,11 @@ func InitTestContext(ctx context.Context, t testing.T) context.Context { ctx = tfsdklog.RegisterTestSink(ctx, t) ctx = tfsdklog.NewRootSDKLogger(ctx, tfsdklog.WithLevelFromEnv(EnvTfLogSdk)) - ctx = tfsdklog.NewSubsystem(ctx, SubsystemHelperResource, tfsdklog.WithLevelFromEnv(EnvTfLogSdkHelperResource)) + ctx = tfsdklog.NewSubsystem(ctx, SubsystemHelperResource, tfsdklog.Options{ + // All calls are through the HelperResource* helper functions + tfsdklog.WithAdditionalLocationOffset(1), + tfsdklog.WithLevelFromEnv(EnvTfLogSdkHelperResource), + }...) ctx = TestNameContext(ctx, t.Name()) return ctx