Pino logger not logging anything when setup with multiple transports

2.4k Views Asked by At

When I setup a simple pino logger with custom log levels like so:

import pino from "pino";

const simpleLogger = pino({
  mixin() {
    return { appName: "TEST SIMPLE LOGGER" };
  },
  level: "silly",
  useOnlyCustomLevels: true,
  customLevels: {
    error: 70,
    warn: 60,
    info: 50,
    http: 40,
    verbose: 30,
    debug: 20,
    silly: 10
  },
  timestamp: () => `,"time":"${new Date(Date.now()).toISOString()}"`,
  formatters: {
    level: label => {
      return {
        level: label
      };
    }
  }
});

simpleLogger.silly("SIMPLE TESTING TESTING 123");

If I then run this code I get output as I would expect:

-> % ts-node test

{"level":"silly","time":"2023-07-18T17:51:40.349Z","pid":60494,"hostname":"MBP","appName":"TEST SIMPLE LOGGER","msg":"SIMPLE TESTING TESTING 123"}

All good so far - but I need to setup a logger which outputs to both console and a file, so I have a MWE like the following:

import { format } from "date-fns";
import pino from "pino";

const date = new Date();
const logDirectory = `/Users/ben/dev/logs/${format(date, "yyyy-MM-dd")}/`;
const logFilename = `${logDirectory}${date.toISOString()}_TESTLOG.log`;

const transports = pino.transport({
  targets: [
    {
      target: "pino/file",
      level: "silly",
      options: {
        destination: logFilename,
        mkdir: true
      }
    },
    {
      level: "silly",
      target: "pino-pretty",
      options: {}
    }
  ],
  levels: {
    error: 70,
    warn: 60,
    info: 50,
    http: 40,
    verbose: 30,
    debug: 20,
    silly: 10
  }
});

const logger = pino(
  {
    mixin() {
      return { appName: "TEST" };
    },
    level: "silly",
    useOnlyCustomLevels: true,
    customLevels: {
      error: 70,
      warn: 60,
      info: 50,
      http: 40,
      verbose: 30,
      debug: 20,
      silly: 10
    },
    formatters: {
      level: label => {
        return {
          level: label
        };
      }
    },
    timestamp: () => `,"time":"${new Date(Date.now()).toISOString()}"`
  },
  transports
);

logger.silly("TESTING TESTING 123");

I then run the logger again:

-> % ts-node test

And there's no output to console, or to the respective log file directory, but I will add that an empty file is created correctly by the logger.

Though if I omit passing transports to the logger in my second example I do get output in the console:

-> % ts-node test

{"level":"silly","time":"2023-07-18T17:57:37.046Z","pid":61240,"hostname":"MBP","appName":"TEST","msg":"TESTING TESTING 123"}

So I suspect there is something wrong with how I am forming the transports - but I'm not getting any general errors when running the code, nor am I getting any typescript errors...

Am I missing something obvious?

I should add I'm using, pino 8.14.1 and pino-pretty 10.0.1.

1

There are 1 best solutions below

0
On

I believe the issue is that you cannot use a custom level formatter (formatters.level) when using multiple transports. When I attempt to use a level formatter with multiple transports I get the following:

option.transport.targets do not allow custom level formatters

This essentially means you cannot override the 'level' property of a log. If you want to add a friendly label you'll need to use the mixin(mergeObject, level) callback.