pinojs/pino

[Question] Same logger for both node and browser

Closed this issue · 3 comments

In a framework like next.js where you have both server- and client-side rendering, is it possible to use the same logger for both? And can i condition those transporters based on the NODE_ENV since it seems that transport isnt available on the client side?

I was trying to do something like this but that doesnt seem to be the way

import pino from "pino";

const pretty = pino.transport({ target: "pino-pretty" });
const std = pino.transport({ target: "std" });

const logger = pino({
  browser: { asObject: true },
  level: process.env.LOGGING_LEVEL,
  base: { env: process.env.NODE_ENV },
  transport: process.env.NODE_ENV === "development" ? pretty : std,
});

export default logger;

Is there any way i can do this, or would i have to export two different loggers?

Unfortunately you'd need to do some bundlers config to have a file create two different loggers.

What i ended up doing was having two different configs and then use an env var to control which one should be used.

import pino from "pino";

const defaultConfig = {
  browser: {
    asObject: true,
  },
  level: process.env.LOGGING_LEVEL,
  base: {
    env: process.env.NODE_ENV,
  },
};

const developmentConfig = {
  ...defaultConfig,
  transport: {
    target: "pino-pretty",
    options: {
      colorize: true,
      colorizeObjects: true,
    },
  },
};

const productionConfig = {
  ...defaultConfig,
};

const logger = pino(
  process.env.APP_ENV === "development" ? developmentConfig : productionConfig
);

export default logger;

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.