Skip to content

improv: improve logger extra keys feature #244

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

Conversation

amirkaws
Copy link
Contributor

@amirkaws amirkaws commented Apr 14, 2023

Please provide the issue number

Issue number: #166

Summary

This PR improves extra keys feature of logger.

Changes

Please provide a summary of what's being changed

  • it accepts any type as provided not only type class
  • it adds extra data provided under "Extra" key if user does not provide key name

User experience

1. Typed Object Type as Extra Key

For typed class key name will be class name/type name for both generic and non-generic types:

public class LookupInfo => LookupInfo
public class LookupInfo<T> => LookupInfo
public class LookupInfo<T0,T1,T2,...> => LookupInfo

Example:

var lookupInfo = new LookupInfo { LookupId = requestContextRequestId };
Logger.LogInformation(lookupInfo, "This is a log with typed object additional data");

Output:

{
    "cold_start": true,
    "lookup_info": {
       "lookup_id": "4c50eace-8b1e-43d3-92ba-0efacf5d1625"
    },
    "message": "This is a log with typed object additional data",
}

2. Anonymous Object Type as Extra Key

For anonymous class key name will be "Extra" for example:

var lookupInfo = new { LookupId = requestContextRequestId };
Logger.LogInformation(lookupInfo, "This is a log with anonymous type object additional data");

Output:

{
    "cold_start": true,
    "extra": {
       "lookup_id": "4c50eace-8b1e-43d3-92ba-0efacf5d1625"
    },
    "message": "This is a log with anonymous type object additional data",
}

3. Anonymous Object Type as Extra Key With Specified Key

When specify the key name for extra additional data (acceptable are tuple or key/value pair) for example:

var lookupInfo = new { LookupId = requestContextRequestId };
Logger.LogInformation(("LookupInfo", lookupInfo), "This is a log specifying the key name for additional data");

Output:

{
    "cold_start": true,
    "lookup_info": {
       "lookup_id": "4c50eace-8b1e-43d3-92ba-0efacf5d1625"
    },
    "message": "This is a log specifying the key name for additional data",
}

4. Multiple Key/Value Pairs

When passing multiple key/value pairs (acceptable are Dictionary or list or key/value pair or array of key/value pair ) for example:

var extraKeys = new Dictionary<string, object>()
{
     { "LookupInfo", new { LookupId = requestContextRequestId } },
     { "CorrelationIds", new { MyCorrelationId = "correlation_id_value" } }
};
Logger.LogInformation(extraKeys, "This is a log with multiple extra key/value pairs");

Output:

{
    "cold_start": true,
    "lookup_info": {
       "lookup_id": "4c50eace-8b1e-43d3-92ba-0efacf5d1625"
    },
    "correlation_ids": {
       "my_correlation_id": "correlation_id_value"
    },
    "message": "This is a log with multiple extra key/value pairs",
}

Please share what the user experience looks like before and after this change

Checklist

Please leave checklist items unchecked if they do not apply to your change.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@auto-assign auto-assign bot requested a review from hjgraca April 14, 2023 16:07
@codecov-commenter
Copy link

codecov-commenter commented Apr 14, 2023

Codecov Report

Attention: Patch coverage is 69.23077% with 12 lines in your changes missing coverage. Please review.

Project coverage is 56.48%. Comparing base (8bb7863) to head (4d76324).
Report is 1195 commits behind head on develop.

Files with missing lines Patch % Lines
...da.Powertools.Logging/Internal/PowertoolsLogger.cs 73.52% 9 Missing ⚠️
.../AWS.Lambda.Powertools.Logging/LoggerExtensions.cs 40.00% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #244      +/-   ##
===========================================
+ Coverage    56.29%   56.48%   +0.19%     
===========================================
  Files           41       41              
  Lines         1796     1827      +31     
===========================================
+ Hits          1011     1032      +21     
- Misses         785      795      +10     
Flag Coverage Δ
unittests 56.48% <69.23%> (+0.19%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@amirkaws amirkaws added the do-not-merge PRs that are blocked for varying reasons label Apr 16, 2023
@amirkaws amirkaws marked this pull request as draft April 16, 2023 07:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do-not-merge PRs that are blocked for varying reasons
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants