how can i rotate log files in pino.js logger

9.5k Views Asked by At

I am trying to use pino for logging in to my node app Server and I do have some large logs coming, so rotating the files every day would be more useful for reading the logs afterwards.

I can do this using morgan but with pino I can't find a way to do it. I tried to dynamically assign the folder based on the current date but doing so in the main app.js file means it will only run once and cycling date would mean stopping and rerunning the server.

Here is my code:

var date = d.getFullYear()+'-'+(d.getMonth()+1)+'-'+d.getDate();
const fileLogger = pino(
     pino.destination({
          dest: './log/userLog '+date, sync: false
     })
);
4

There are 4 best solutions below

0
On

As stated in pino documentation you need to use a separate tool for log rotation.

Using logrotate you can do it easily.

node myapp.js > /var/log/myapp.log

Install logrotate on you server and create a /etc/logrotate.d/myapp file with the logrotation config, e.g.:

/var/log/myapp.log {
   su root
   daily
   rotate 7
   delaycompress
   compress
   notifempty
   missingok
   copytruncate
}

Be sure to read its configuration documentation to achieve your rigth rotation. Obviously, explore the many other questions about logrotate if you have problems with it.

1
On

Using logrotate is a viable approach. But the pino-rotating-file transport for pino seems a better idea.

0
On

Rotating the file stream library does work seamlessly with pino. But it slows down pino performance as it creates and maintains the file.Pino/file transport, which is the default one, has a better performance by default. using custom transport for log rotation using a rotating file stream slows down the performance

0
On

this is really late to your question, but i found this recently and didn't find either of the solutions ideal. logrotate is a C program for linux and pino-rotating-file requires changing the program invocation command line to pipe stdout through multiple invocations of program.

i have not finished testing, but the solution that i'm evaluating at this time is rotating-file-stream. it hides the rotation in the file stream itself and works (so far) seamlessly with pino.

the main program:

'use strict';

const path = require('path');
const pino = require('pino');

const logger = pino({
  name: 'testing123',
  level: 'trace',
  transport: {
    targets: [
      {
        target: path.resolve('transport-stream.js'),
        level: 'trace',
        options: {
          append: true,
          destination: path.resolve('delete-me.log'),
          interval: '5s',
          compress: 'gzip',
        }
      }
    ]
  }
});

logger.info('started');

let counter = 1;
setInterval(() => {
  logger.info(`interval #${counter}`);
}, 10_000);

transport-stream.js:

'use strict';

const {createWriteStream} = require('fs');
const rfs = require('rotating-file-stream');

module.exports = function(options) {
  const { size, interval, compress } = options;
  return rfs.createStream(options.destination, {
    size: size || "1000B",
    interval: interval || "10m",
    compress: compress || "gzip",
  });
}