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

Format of JSON not as expected #230

Open
bjhogan opened this issue Apr 3, 2023 · 3 comments · May be fixed by #244
Open

Format of JSON not as expected #230

bjhogan opened this issue Apr 3, 2023 · 3 comments · May be fixed by #244
Assignees
Labels
area/logging Core logging utility not-a-bug New and existing bug reports incorrectly submitted as bug
Milestone

Comments

@bjhogan
Copy link

bjhogan commented Apr 3, 2023

Expected Behaviour

For an object Person, that has an Address I expect to see it logged like this -

When logging using Logger.LogInformation<Person>(person, "{Name} and is {Age} years old", new object[]{person.Name, person.Age}); I expect the output to look like -

"person": {
    "name": "Alan Adams",
    "age": 11,
    "address": {
        "street": "123 Main Street",
        "city": "Boston"
    }
},

Current Behaviour

Instead the output is -

"name": "Alan Adams",
"age": 11,
"address": {
    "street": "123 Main Street",
    "city": "Boston"
},

Person is lost.

Code snippet

using Amazon.Lambda.Core;
using AWS.Lambda.Powertools.Logging;

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace ThreeSimpleWaysToLog;

public class Function

{
    [Logging(LogEvent = true, LoggerOutputCase = LoggerOutputCase.CamelCase)]
    public void FunctionHandler(string input, ILambdaContext context)
    {
        Person person = new Person { Name ="Alan Adams", Age = 11, Address = new Address{ Street = "123 Main Street",  City ="Boston" } };
        Logger.LogInformation<Person>(person, "{Name} and is {Age} years old", new object[]{person.Name, person.Age});
 
    }
}


public class Person 
{
    public string Name {get ; set;} 
    public int Age { get; set; }
    public Address Address { get; set; }
}
public class Address 
{
    public string Street { get; set; }
    public string City { get; set; }
}

Possible Solution

I would expect the log out to look the same as it does when using like this -

Logger.AppendKey("Person", person);
Logger.LogInformation("{Name} and is {Age} years old", new object[]{person.Name, person.Age});
Logger.RemoveKeys("Person");

The output from this is -

{
    "coldStart": true,
    "xrayTraceId": "1-642b077d-0e615d154a66119a5585838b",
    "person": {
        "name": "Alan Adams",
        "age": 11,
        "address": {
            "street": "123 Main Street",
            "city": "Boston"
        }
    },
    "functionName": "ThreeSimpleWaysToLog",
    "functionVersion": "$LATEST",
    "functionMemorySize": 256,
    "functionArn": "arn:aws:lambda:us-east-1:694977046108:function:ThreeSimpleWaysToLog",
    "functionRequestId": "5bf50f21-2c0e-4327-82df-fd0939bf6aa5",
    "timestamp": "2023-04-03T17:06:07.0296164Z",
    "level": "Information",
    "service": "ThreeSimpleWaysToLog",
    "name": "AWS.Lambda.Powertools.Logging.Logger",
    "message": "Alan Adams and is 11 years old"
}

The Person is clearly shown.

Steps to Reproduce

Deploy the code provided and invoke.

AWS Lambda Powertools for .NET version

latest

AWS Lambda function runtime

dotnet6

Debugging logs

No response

@bjhogan bjhogan added bug Unexpected, reproducible and unintended software behaviour triage Pending triage from maintainers labels Apr 3, 2023
@hjgraca hjgraca self-assigned this Apr 12, 2023
@hjgraca
Copy link
Contributor

hjgraca commented Apr 12, 2023

@bjhogan thanks for reporting. Having a look

@sliedig
Copy link
Contributor

sliedig commented Apr 13, 2023

Related to #116

@sliedig sliedig added area/logging Core logging utility not-a-bug New and existing bug reports incorrectly submitted as bug labels Apr 13, 2023
@sliedig sliedig self-assigned this Apr 13, 2023
@sliedig sliedig removed the triage Pending triage from maintainers label Apr 13, 2023
@amirkaws
Copy link
Contributor

Hi @bjhogan, thanks for opening the issue. The extraKeys parameter in Logger.LogInformation(extraKeys, "message"), expect dictionary or a list of key/value pairs. So the object also be treated as a bag of key/value pair and will be logged in the root level. You can pass "person" object as value in combination with the key name to achieve the desired outcome.

var extraKeys = new { Person = person };
Logger.LogInformation(extraKeys, $"{person.Name} and is {person.Age} years old");

On a separate notes, we have already another issue #166 to improve the documentation.

@sliedig sliedig assigned amirkaws and unassigned sliedig and hjgraca Apr 14, 2023
@hjgraca hjgraca linked a pull request May 30, 2023 that will close this issue
7 tasks
@hjgraca hjgraca removed the bug Unexpected, reproducible and unintended software behaviour label Jun 21, 2023
@hjgraca hjgraca added this to the Logging V2 milestone Dec 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/logging Core logging utility not-a-bug New and existing bug reports incorrectly submitted as bug
Projects
Status: 📋 Backlog
Development

Successfully merging a pull request may close this issue.

4 participants