Skip to content

Feature request: Reusing previous logger instance when already instantiated #1398

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

Closed
1 of 2 tasks
RobinGroundfog opened this issue Apr 7, 2023 · 2 comments
Closed
1 of 2 tasks
Assignees
Labels
duplicate This issue is a duplicate of an existing one feature-request This item refers to a feature request for an existing or new utility logger This item relates to the Logger Utility

Comments

@RobinGroundfog
Copy link

Use case

As far as I can tell, calling new Logger() instantiates a new logger, which means we can't access previously appended keys across the code.

Solution/User Experience

It would be great if a new instance that uses the same service name - env var or explicit parameter - would reuse a previous Logger instance, as it's the case for the Python Powertools

Alternative solutions

No response

Acknowledgment

Future readers

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

@RobinGroundfog RobinGroundfog added triage This item has not been triaged by a maintainer, please wait feature-request This item refers to a feature request for an existing or new utility labels Apr 7, 2023
@dreamorosi
Copy link
Contributor

Hi @RobinGroundfog, thank you for taking the time to open an issue in this repo.

You are right, at the moment all the core utilities including Logger are not singletons and creating a new instance does create an instance that is completely separated by the original one.

This is a topic that has come up in the past and that has been discussed (#777) and tested (#767) but ultimately discarded.

The usage that we would like to promote, at least until new information/use cases come up, is to create a module where you instantiate your Powertools utilities like src/powertools.ts which you can then use throughout your project:

import { Logger } from '@aws-lambda-powertools/logger';
import { Metrics } from '@aws-lambda-powertools/metrics';
import { Tracer } from '@aws-lambda-powertools/tracer';

const awsLambdaPowertoolsVersion = '1.8.0';

const defaultValues = {
  awsAccountId: process.env.AWS_ACCOUNT_ID || 'N/A',
  environment:  process.env.ENVIRONMENT || 'N/A'
};

const logger = new Logger({
  persistentLogAttributes: {
    ...defaultValues,
    logger: {
      name: '@aws-lambda-powertools/logger',
      version: awsLambdaPowertoolsVersion,
    }
  },
});

const metrics = new Metrics({
  defaultDimensions: defaultValues
});

const tracer = new Tracer();

export {
  logger,
  metrics,
  tracer,
};

For Logger specifically, you can then import the logger elsewhere and optionally create children that override some of the settings:

// src/index.ts
import { logger } from './powertools';

export const handler = async () => {
  logger.debug('...');
};

or src/my-other-module.ts:

import type { Logger } from '@aws-lambda-powertools/logger;
import { logger } from './powertools';

export class MyUtility {
  private logger = Logger;

  public constructor() {
    this.logger = logger.createChild({ logLevel: 'CRITICAL' });
  } 
};

Using this type of pattern removes any kind of ambiguity around module import order which might cause confusion as detailed here, and still allows you to reuse the same instance throughout your code.

We have already updated our examples to use this pattern but we haven't yet gotten around creating a dedicated section that explains this in the docs. This effort is tracked in #777 and we'll try to address it as soon as possible.

I'm going to close this issue as duplicate of #777, if you'd like to bring additional information and/or use cases around this topic please feel free to do so in that issue so we can keep track of all points of view in a single place.

@dreamorosi dreamorosi closed this as not planned Won't fix, can't repro, duplicate, stale Apr 7, 2023
@dreamorosi dreamorosi added duplicate This issue is a duplicate of an existing one logger This item relates to the Logger Utility and removed triage This item has not been triaged by a maintainer, please wait labels Apr 7, 2023
@dreamorosi dreamorosi self-assigned this Apr 7, 2023
@dreamorosi dreamorosi moved this from Coming soon to Closed in AWS Lambda Powertools for TypeScript Apr 7, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Apr 7, 2023

⚠️ 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue is a duplicate of an existing one feature-request This item refers to a feature request for an existing or new utility logger This item relates to the Logger Utility
Projects
None yet
Development

No branches or pull requests

2 participants