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

Formatter is very fragile #44

Open
ramblingenzyme opened this issue Nov 6, 2023 · 1 comment
Open

Formatter is very fragile #44

ramblingenzyme opened this issue Nov 6, 2023 · 1 comment
Labels
documentation Improvements or additions to documentation

Comments

@ramblingenzyme
Copy link

https://github.com/pinojs/pino/blob/master/docs/api.md#messagekey-string

Configuring messageKey will cause the formatter to fail.

Additionally we configure a formatter for level which returns status, to meet Datadog's expectations https://docs.datadoghq.com/logs/log_configuration/attributes_naming_convention/#reserved-attributes.

    level: (label: string) => ({
      status: label,
    }),
  },
@carbonrobot carbonrobot added bug Something isn't working documentation Improvements or additions to documentation labels Nov 6, 2023
@carbonrobot
Copy link
Collaborator

pino runs the custom destination as the last function in the call chain, so the level and messagekey are not known to pino-lambda by the time pino calls our formatter. The fix is pretty simple, however, by supplying your own custom formatter.

import { LogData, ILogFormatter } from 'pino-lambda';

class CustomLogFormatter implements ILogFormatter {
  format(data: LogData): string {
    const { 
      awsRequestId,
      // your custom message key
      customMessageKey,
      // your custom level label key
      status
    } = data;

    // extract parts for message format
    const time = new Date().toISOString();

    return `${time}${
      awsRequestId ? `\t${awsRequestId}` : ''
    }\t${status.toUpperCase()}\t${msg}\t${JSON.stringify(data)}`;
  }
}

const destination = pinoLambdaDestination({
  formatter: new CustomLogFormatter(),
});
const logger = pino(destination);

@carbonrobot carbonrobot removed the bug Something isn't working label Nov 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants