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

Logs for trace and debug levels are not printed when using a custom formatters.level in Pino 9.5.0 #2094

Open
jenishk29 opened this issue Nov 19, 2024 · 0 comments

Comments

@jenishk29
Copy link

I’m facing an issue with my current Pino logger setup where logs for trace and debug levels are not being printed. Here’s a quick summary of the setup and problem:

Initial Setup:
Create a logger module named @pino-logger.

import { randomUUID } from 'node:crypto';
import pino from 'pino';

export const logger = pino(
  {
    enabled: !process?.env?.NOLOG,
    base: {
      traceId: randomUUID(),
      userId: 'undefined',
      source: process?.env?.AWS_LAMBDA_FUNCTION_NAME,
      requestId: 'undefined',
    },
    level: process?.env?.LOG_LEVEL || 'trace',
    redact: {
      paths: process.env.PATHS_TO_MASK_DATA?.split(',') || [],
      censor: '******',
    },
    timestamp: () => `,"timestamp":"${new Date(Date.now()).toISOString()}"`,
  },
  pino.destination({ sync: true }),
);

Usage in AWS Lambda Function:
Import and use the logger in a Lambda function.

import { logger } from '@pino-logger';
logger.trace('getCustomerProfile %s', customerId);

This setup correctly prints all log levels, including trace. Sample log:

{
    "level": 10,
    "timestamp": "2024-11-19T07:00:06.685Z",
    "traceId": "b61e86c6-f067-44ca-82ec-f0b057db7940",
    "userId": "anonymous",
    "source": "get-customer-profile",
    "requestId": "b37109f1-353b-4e30-a649-fe38a22cea4d",
    "msg": "GetCustomerProfile: 1879999002"
}

Updated Setup with formatters.level:
Modify the logger to replace numerical levels (e.g., 10) with their string equivalents (e.g., 'TRACE').

export const logger = pino(
  {
    enabled: !process?.env?.NOLOG,
    base: {
      traceId: randomUUID(),
      userId: 'undefined',
      source: process?.env?.AWS_LAMBDA_FUNCTION_NAME,
      requestId: 'undefined',
    },
    level: process?.env?.LOG_LEVEL || 'trace',
    formatters: {
      level: (label) => ({
        level: label.toUpperCase(),
      }),
    },
    redact: {
      paths: process.env.PATHS_TO_MASK_DATA?.split(',') || [],
      censor: '******',
    },
    timestamp: () => `,"timestamp":"${new Date(Date.now()).toISOString()}"`,
  },
  pino.destination({ sync: true }),
);

After this update, logs for trace and debug levels are no longer printed. However, other levels, such as info and error, work as expected.

Example of working log:

{
    "level": "ERROR",
    "timestamp": "2024-11-18T21:04:51.312Z",
    "traceId": "292aa1db-a2d4-4949-8394-5e292f52081d",
    "userId": "undefined",
    "source": "get-customer-profile",
    "requestId": "undefined",
    "msg": "error"
}

1. Is there something wrong with my current logger configuration or formatters.level usage?
2. How can I modify the setup to ensure that trace and debug logs are printed while maintaining formatted log levels (e.g., 'TRACE', 'DEBUG')?

@jenishk29 jenishk29 changed the title Logs for trace and debug levels are not printed when using a custom formatters.level in Pino Logs for trace and debug levels are not printed when using a custom formatters.level in Pino 9.5.0 Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant