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

Bug: decorated class methods cannot access this in Logger #1058

Closed
dreamorosi opened this issue Aug 13, 2022 · 2 comments · Fixed by #1060
Closed

Bug: decorated class methods cannot access this in Logger #1058

dreamorosi opened this issue Aug 13, 2022 · 2 comments · Fixed by #1060
Assignees
Labels
bug Something isn't working completed This item is complete and has been merged/shipped logger This item relates to the Logger Utility

Comments

@dreamorosi
Copy link
Contributor

Bug description

A long form description of this issue is available in #1056. This is an issue that affects all three Powertools utilities because they all share similar implementation for the decorators.


When using Powertools in with a Class and when decorating a method, the decorated method does not have access to the correct value of this and so it's not able to call other methods nor read attributes from its same class.

This is due to the fact that the decorator implementation uses an () => () arrow function instead of a regular function () {} one. This mixes the value of this which then causes the wrong value to be passed.

Additionally there's a missing detail in the documentation of the Class instrumentation that prevents compounds the issue described above.

Expected Behavior

Decorated methods have access to the correct value of this and is able to call methods or access attributes/properties.

Current Behavior

See "Steps to Reproduce" section.

Possible Solution

Apply the same changes done to Tracer in #1055

Steps to Reproduce

import {
  LambdaInterface,
  SyncHandler,
  AsyncHandler,
} from "@aws-lambda-powertools/commons";
import { Logger } from "@aws-lambda-powertools/logger";

const logger = new Logger({ serviceName: "serverlessAirline" });

class Lambda implements LambdaInterface {
  greeting: string;

  constructor(message: string) {
    this.greeting = message;
  }

  public getGreeting(): string {
    /* ... */
    return `Hello ${this.greeting}`;
  }

  @logger.injectLambdaContext()
  public handler(_event: any, _context: any, _callback: any): void {
    console.log(this.getGreeting());
  }
}

export const myFunction = new Lambda("World");
export const handler = myFunction.handler.bind(myFunction);

The value logged from console.log(this.getGreeting()); should be Hello World but instead is Hello undefined:
image

Environment

  • Powertools version used: 1.1.0
  • Packaging format (Layers, npm): all
  • AWS Lambda function runtime: all
  • Debugging logs: N/A

Related issues, RFCs

#1056

@dreamorosi dreamorosi added bug Something isn't working logger This item relates to the Logger Utility triage This item has not been triaged by a maintainer, please wait labels Aug 13, 2022
@dreamorosi dreamorosi self-assigned this Aug 14, 2022
@dreamorosi dreamorosi removed the triage This item has not been triaged by a maintainer, please wait label Aug 14, 2022
@dreamorosi dreamorosi linked a pull request Aug 14, 2022 that will close this issue
11 tasks
@github-actions
Copy link
Contributor

⚠️ COMMENT VISIBILITY WARNING ⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added the pending-release This item has been merged and will be released soon label Aug 18, 2022
@dreamorosi dreamorosi removed the pending-release This item has been merged and will be released soon label Aug 18, 2022
@dreamorosi
Copy link
Contributor Author

Released in v1.1.1

@dreamorosi dreamorosi changed the title Bug (logger): decorated class methods cannot access this Bug: decorated class methods cannot access this in Logger Nov 14, 2022
@dreamorosi dreamorosi added the completed This item is complete and has been merged/shipped label Nov 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working completed This item is complete and has been merged/shipped logger This item relates to the Logger Utility
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant