pinojs / pino

🌲 super fast, all natural json logger
http://getpino.io
MIT License
14.21k stars 874 forks source link

Simple log file rotation #1906

Closed jjordan-quantum closed 7 months ago

jjordan-quantum commented 8 months ago

If I create a new instance of pino logger, and the previous instance no longer has any references (goes out of scope), what will be the consequence? I am looking for a function exposed in the API for pino to close a file, or exit worker thread. This is to rotate log files every day, and I am wondering if after 30 days there will be 30 files still open and 30 dangling worker threads.

// logs are written by calling PinoLogger.logger.info('message');
class PinoLogger {
  static logger = pino({
    transport: {
      target: 'pino-pretty',
      options: {
        colorize: false,
        ignore: 'pid,hostname',
        translateTime: "UTC:mm-dd-yyyy HH:MM:ss.l",
        destination: `${settings.logFilePath}/${settings.logFileName}`,
      },
    }
  });

  // assumes using file logging
  static rotate(): void {
    const now = new Date();

    // renames the current file being used in the pino transport
    // logger continues to write to this file, even when file name changes
    fs.renameSync(
      `${settings.logFilePath}/${settings.logFileName}`,
      `${settings.logFilePath}/${now.getFullYear()}-${now.getMonth()}-${now.getDate()}-debug.log`
    );

    // assign new pino instance to the logger field of the wrapper class
    // now all calls to PinoLogger.logger.info() will call this instance
    this.logger = pino({
      transport: {
        target: 'pino-pretty',
        options: {
          colorize: false,
          ignore: 'pid,hostname',
          translateTime: "UTC:mm-dd-yyyy HH:MM:ss.l",
          destination: `${settings.logFilePath}/${settings.logFileName}`,
        },
      }
    });
  }
}
mcollina commented 7 months ago

The workers threads are automatically closed and collected if there are no references to them anymore.

github-actions[bot] commented 6 months ago

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.