pinojs / pino-pretty

🌲Basic prettifier for Pino log lines
MIT License
1.25k stars 147 forks source link

[Question] Handling non-serializable options in Bundled Package #421

Closed victorlrz closed 1 year ago

victorlrz commented 1 year ago

Hello,

I have a shared package in my monorepo that contains a pino logger file, which is used across several Next.js applications. The logger file uses the pino-pretty module to format the logs.

I'm using the new pino v7+ transports, which require non-serializable options. To use messageFormat as a function, I'm trying to wrap pino-pretty as suggested in the documentation.

However, I am struggling to include the pino-pretty-transport file in my shared library and make the custom messageFormat works when I use the exported logger on one of the NextJs App

I am not sure how to proceed with resolving this issue. Could you please provide some guidance on how to include the custom pino-pretty-transport module in my shared library/Next.js builds?

Technical Details:

pino-pretty-transport.ts (shared library)

import { LoggerOptions } from 'pino';
import PinoPretty, { PrettyOptions } from 'pino-pretty';

export const pinoPrettyTransport = (opts: PrettyOptions) =>
  PinoPretty({
    ...opts,
    messageFormat: (log: LoggerOptions, messageKey) => {
      const message = log[messageKey] as string;

      return `custom Message ${message}`;
    },
  });

logger.ts (shared library)

export const pinoLogger = pino({
  customLevels: customLevels.levels,
  level: isProduction ? 'info' : 'debug',
  ...(isDev
    ? {
        transport: {
          target: './pino-pretty-transport',
          options: {
            colorize: true,
            customColors: 'warn:yellow,info:green,debug:blue,error:red',
            levelFirst: true,
            ignore: 'service,source,env,time',
          },
        },
      }
    : undefined),

Built with tsup (shared library):

export default defineConfig({
  entry: ['src/index.ts'],
  dts: true,
  format: ['esm', 'cjs'],
  clean: true,
  outExtension: ({ format }) => ({
    js: `.${format}.js`,
  }),
});

=> 3 files in shared dist folder

  "main": "dist/index.cjs.js",
  "module": "dist/index.esm.js",
  "types": "dist/index.d.ts",

Then using shared library in one of the NextApp ->

image

Tried with require.resolve, path.resolve, but not sure how to fix this if you have ideas. Thanks in advance

mcollina commented 1 year ago

Given how workers are setup, you'll need a separate entry point for this and you'd need to use https://github.com/pinojs/pino-webpack-plugin.

victorlrz commented 1 year ago

Thanks for your help @mcollina, I'll investigate