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

Feature request: Enable pretty printing using code instead of environment variable #3466

Open
1 of 2 tasks
TeaDrinkingProgrammer opened this issue Jan 10, 2025 · 8 comments
Open
1 of 2 tasks
Labels
discussing The issue needs to be discussed, elaborated, or refined feature-request This item refers to a feature request for an existing or new utility logger This item relates to the Logger Utility need-response This item requires a response from a customer and will considered stale after 2 weeks

Comments

@TeaDrinkingProgrammer
Copy link

Use case

Hi all,
I am very happy with the ability to pretty print locally, but because I am using SST, it would be easier for me to enable devmode in code than to set it as an environment variable. I currently need to do this, which works but feels very hacky:

  if (Resource.App.stage != 'staging' && Resource.App.stage != 'develop' && Resource.App.stage != 'production') {
    process.env.POWERTOOLS_DEV = "true";
  }

Solution/User Experience

Something like this would be great:

  const logger = new Logger({
    ...,
   devMode: true
  });

Alternative solutions

Acknowledgment

Future readers

Please react with 👍 and your use case to help us understand customer demand.

@TeaDrinkingProgrammer TeaDrinkingProgrammer added feature-request This item refers to a feature request for an existing or new utility triage This item has not been triaged by a maintainer, please wait labels Jan 10, 2025
Copy link

boring-cyborg bot commented Jan 10, 2025

Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #typescript channel on our Powertools for AWS Lambda Discord: Invite link

@dreamorosi
Copy link
Contributor

dreamorosi commented Jan 10, 2025

Hi @TeaDrinkingProgrammer, thank you for opening this issue.

The pretty printing option has been purposefully added as an environment variable to reduce the chance of developers forgetting to remove it within their code and inadvertently increasing their logging bill.

The rationale is that this type of setting should be dependent on where the function is being deployed, which is a concern that the function code itself should not have. Keeping this separate allows you to dynamically change the setting without having to modify your code.

As such, I am not sure adding a constructor parameter is a good idea.

Based on my understanding of SST, a good place to add this setting could be in your sst.config.ts file, where you can use the $transform global and conditionally override the config of your functions to enable the POWERTOOLS_DEV setting:

export default $config({
  app(input) {
    return {
      name: 'your-app',
      home: 'aws'
    }
  },
  async run() {
    const isPersonalStage = $app.stage === 'your-stage-name';
    $transform(sst.aws.Function, (args) => {
      args.environment = {
        ...args.environment,
        ...(isPersonalStage && { POWERTOOLS_DEV: 'true' }),
      };
    });
  }
})

This way you don't have to add the environment variable to every single Function resource you create, and other settings in the resource are left as they are.

I took the liberty to shorten the conditional to narrow it down to a single stage name, but you can also use one similar to the one you are already using.

@dreamorosi dreamorosi added logger This item relates to the Logger Utility discussing The issue needs to be discussed, elaborated, or refined need-response This item requires a response from a customer and will considered stale after 2 weeks and removed triage This item has not been triaged by a maintainer, please wait labels Jan 10, 2025
@dreamorosi dreamorosi moved this from Triage to On hold in Powertools for AWS Lambda (TypeScript) Jan 10, 2025
@dreamorosi
Copy link
Contributor

I have been looking into the SST docs a bit more, and I think there's an even better way to detect if you're running in dev mode from within your sst.config.ts file, which is explained here and can be accessed by using the $dev global variable.

That method is pretty much what you're trying to do here and is described in relation to environment variables. Using it would change the code above like this:

export default $config({
  app(input) {
    return {
      name: 'your-app',
      home: 'aws'
    }
  },
  async run() {
    $transform(sst.aws.Function, (args) => {
      args.environment = {
        ...args.environment,
        ...($dev === true && { POWERTOOLS_DEV: 'true' }),
      };
    });
  }
})

@TeaDrinkingProgrammer
Copy link
Author

I have tried implementing this, and as far as I understand from the docs, this should work, but it doesn't. I have asked in the Discord for help, will report back if I have a snippet to share.

@dreamorosi
Copy link
Contributor

I have implemented this and I can confirm it works.

Not sure if you're already doing this, but this $transform block needs to be added before you create your Function resources.

@TeaDrinkingProgrammer
Copy link
Author

I have put the transform before all of my resources, and it does not work, so this is quite interesting. Seems like a bug, but I'm not sure.

@dreamorosi
Copy link
Contributor

You are right, the only way it works is if I write the transform like this:

$transform(sst.aws.Function, (args) => {
  args.environment = {
    ...($dev === true && { POWERTOOLS_DEV: "true" }),
  }
});

but in this case it overwrites any other env variable I set in the function/route itself.

Looks like a bug, you're right.

@dreamorosi
Copy link
Contributor

After rereading the $transform docs, I am not 100% sure that objects are merged. Basically I don't know if when encountering an object like environment SST merges the one present in the $transform with the one provided in the resource or not.

Based on what we're seeing it looks like this is not the case, however I can see how this is unintuitive and also limiting in the case of environment variables.

Either way, this seems to be a SST issue rather than a Powertools for AWS one - so I'll leave the issue open for a bit longer and resolve it in a few days unless something new comes up. From our side we stand by our decision of keeping the feature outside of the function code and leaving it in the environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussing The issue needs to be discussed, elaborated, or refined feature-request This item refers to a feature request for an existing or new utility logger This item relates to the Logger Utility need-response This item requires a response from a customer and will considered stale after 2 weeks
Projects
Development

No branches or pull requests

2 participants