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

Fallback to Generic Logger Output #52

Open
bflad opened this issue Apr 26, 2022 · 3 comments
Open

Fallback to Generic Logger Output #52

bflad opened this issue Apr 26, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@bflad
Copy link
Member

bflad commented Apr 26, 2022

terraform-plugin-log version

v0.3.0

Use cases

For much of the terraform-plugin-log logging logic, it will fallback to dropping messages when the context.Context is not appropriately setup, e.g.

if logger == nil {
// this essentially should never happen in production
// the root logger for provider code should be injected
// by whatever SDK the provider developer is using, so
// really this is only likely in unit tests, at most
// so just making this a no-op is fine
return
}

In production, is quite trivial for provider developers to get into situations where the context.Context is not setup properly. For example, using the non-context-aware fields of github.com/hashicorp/terraform-plugin-sdk/helper/schema.Resource or using any SDK, errantly using a separate context.Context that was not setup (e.g. via context.Background() or context.TODO()).

Another use case is SDK-based unit testing, where it takes some effort to setup the logging contexts or its easy to miss in each and every test.

Not having any log output with no indication of an issue is more confusing than expecting everyone downstream to figure out the issue on their own.

Proposal

If a logging message is to be written, but the appropriate logging context is not available, fallback to a last resort github.com/hashicorp/hc-log.Logger and write to it instead. On first usage of the last resort Logger, it should also output a warning message about the missing context-based Logger, so provider or SDK developers can understand any differences in logging behavior.

References

@bflad bflad added the enhancement New feature or request label Apr 26, 2022
@bflad bflad added this to the v0.4.0 milestone Apr 26, 2022
@bflad
Copy link
Member Author

bflad commented Apr 26, 2022

Another way provider developers can get into this situation is if they are using a github.com/hashicorp/terraform-plugin-sdk or github.com/hashicorp/terraform-plugin-framework Go dependency before the introduction of the logging context handling -- prior to v2.10.0 and v0.6.0 respectfully.

@ghost
Copy link

ghost commented May 31, 2022

Same here tflog is not printing output to console for me. Just starting with Go and tf provider development so everything is still confusing to me...

I tried:
tflog.Warn(context.TODO(), string(postBody))

But I guess this is not working because I am using TODO context? What should I use instead?

@ghost
Copy link

ghost commented May 31, 2022

Ok, i resolved it by passing the context from parents to children and using that instead of "dummy" context

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant