0

To register any events in my project, I use pino. This is my 'Logger.js' file:

require('dotenv').config();
const pino = require('pino');

const logger = (
  name, 
  level = (process.env.LOG_LEVEL || 'info'),
  file = process.env.LOG_FILE || './log.log',
) => pino({
  name,
  level,
  transport:
  {
    targets: [
      { // то screen
        target: 'pino-pretty',
        level,
        options:
        {
          colorize: true,
          translateTime: true,
          sync: true,
        },
      },
      { // то file
        target: 'pino-pretty',
        level,
        options:
        {
          colorize: false,
          translateTime: true,
          sync: true,
          destination: file,
        },
      },
    ],
  },
});

module.exports = logger;

Then I use this logger in any classes. For example, such as this class:

const logger = require('./Logger')('User');

class testClass1 {
  constructor(a) {
    this.a = a;
  }

  async increase(x) {
    logger.debug(`x=${x}`);
    this.a += x;
    return this.a
  }
}

module.exports = testClass1;

Classes can be used in project files, for example:

const testClass1 = require('./testClass1');

async function test() {
  const test1 = new testClass1(2);
  test1.increase(2);
}

test();

Everything works well. But if the number of used classes with logger is more than 10, I have a warning: Possible EventEmitter memory leak detected.

What can be done to avoid this? Maybe there are some recommendations on how to keep a log?

1 Answers1

1

Your logger.js file exports a function that creates a new logger.

I would try to create only one logger and if needed I would create child loggers for uses like the User.js file you have