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

tflog+tfsdklog: Added WithAdditionalLocationOffset function #36

Merged
merged 6 commits into from
Mar 10, 2022

Conversation

bflad
Copy link
Contributor

@bflad bflad commented Mar 8, 2022

Closes #29

Allows implementations to fix the location offset of caller information when using helper functions. While typically expected for subsystem loggers, SDKs could also offer the configuration of the root provider logger as well.

Since each hclog.Logger is mainly immutable once its created (except for level and creating named sub-loggers), the hclog.LoggerOptions are saved to the context as well, allowing NewSubsystem to appropriately have all the root logger options available to create new sub-loggers. Maybe this can be changed upstream in the future, although that would technically be a breaking change since the interface would need to be modified.

This changeset also includes setting the name of the test root SDK/provider loggers, so that subsystem naming (and therefore the unit testing) matches real world usage.

@bflad bflad added the enhancement New feature or request label Mar 8, 2022
@bflad bflad added this to the v0.3.0 milestone Mar 8, 2022
@bflad bflad requested a review from a team as a code owner March 8, 2022 13:40
bflad added a commit that referenced this pull request Mar 8, 2022
Reference: #29

Allows implementations to fix the location offset of caller information when using helper functions. While typically expected for subsystem loggers, SDKs could also offer the configuration of the root provider logger as well.

Since each `hclog.Logger` is mainly immutable once its created (except for level and creating named sub-loggers), the `hclog.LoggerOptions` are saved to the context as well, allowing `NewSubsystem` to appropriately have all the root logger options available to create new sub-loggers. Maybe this can be changed upstream in the future, although that would technically be a breaking change since the interface would need to be modified.

This changeset also includes setting the name of the test root SDK/provider loggers, so that subsystem naming (and therefore the unit testing) matches real world usage.
@bflad bflad force-pushed the bflad-AdditionalLocationOffset branch from 1df84e1 to 05b0844 Compare March 8, 2022 18:58
Copy link
Contributor

@detro detro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good on the feature/implementation.

But I have left a few comments about the tests that I believe can be beneficial for future contributors.

@@ -29,6 +30,7 @@ func TestWith(t *testing.T) {
{
"@level": hclog.Trace.String(),
"@message": "test message",
"@module": logging.DefaultProviderRootLoggerName,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm from the "school of thought" that test fixtures should be hardcoded and explicit.

IMHO here you should have the same string "provider" duplicated a bunch of times, because using the const itself is helping reducing the chance of the test breaking if, for example, one introduces a typo in the value of DefaultProviderRootLoggerName above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated everywhere.

const testSubsystem = "test_subsystem"
const (
testSubsystem = "test_subsystem"
testSubsystemModule = logging.DefaultProviderRootLoggerName + "." + testSubsystem
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same point about fixtures as above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

@@ -31,7 +35,7 @@ func TestSubsystemWith(t *testing.T) {
{
"@level": hclog.Trace.String(),
"@message": "test message",
"@module": testSubsystem,
"@module": testSubsystemModule,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And for all of the @module in this test file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the constant to include the hardcoded root logger name.

@@ -29,6 +30,7 @@ func TestWith(t *testing.T) {
{
"@level": hclog.Trace.String(),
"@message": "test message",
"@module": logging.DefaultSDKRootLoggerName,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

@@ -31,7 +35,7 @@ func TestSubsystemWith(t *testing.T) {
{
"@level": hclog.Trace.String(),
"@message": "test message",
"@module": testSubsystem,
"@module": testSubsystemModule,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the constant as noted previously.

const testSubsystem = "test_subsystem"
const (
testSubsystem = "test_subsystem"
testSubsystemModule = logging.DefaultSDKRootLoggerName + "." + testSubsystem
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

},
expectedOutput: []map[string]interface{}{
{
"@caller": "/tflog/options_test.go:30",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be beneficial for future contributors to know how the integer/line is determined.

I can see someone coming here, changing something like an import or adding a test case above or whatever, and seeing that suddenly all those tests are failing.

A note above the line that warns that the :line part of that string is dependent on the very content of the file is in, it can save someone a bit of head scratching.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in 32c0060

Copy link
Contributor

@detro detro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - thank you

Comment on lines +34 to +35
// Caller line (number after colon) should match
// tflog.SubsystemTrace() line in test case implementation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👩‍🍳 💋

@@ -29,6 +29,7 @@ func TestWith(t *testing.T) {
{
"@level": hclog.Trace.String(),
"@message": "test message",
"@module": "provider",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👩‍🍳 💋

@bflad bflad merged commit 561793d into main Mar 10, 2022
@bflad bflad deleted the bflad-AdditionalLocationOffset branch March 10, 2022 12:37
@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consider Helper() Function to Fix Log Entry @caller Information
2 participants