1

I'm using Winston logger for node.js, and everytime I restart the app, the logs get overwritten by the blank ones, starting at the moment of the restart. I need to keep the logs, SPECIALLY when I have to restart the app, since it's surely due to an error. I've read the documentation at GitHub, but found nothing about this. This is how I'm using the transports:

winston.add(winston.transports.Console, {
    level: config.logLevel,
    silent: false,
    colorize: true, 
    timestamp: true
});
winston.add(winston.transports.File, {
    filename: config.logFile,
    maxsize: 524288000, // 500MB
    maxFiles: 4,
    handleExceptions: true,
    json: false,
    level: 'debug'
});

Is there any way to rotate the logs on app restart so I can see what happenned? Thanks!

1
  • Just add a timestamp to your filename: config.logFile + Date.now() + '.log' Dec 25, 2019 at 13:18

1 Answer 1

0

You can do that by using a stream, instead of a filename, as an option for the file transport: https://github.com/winstonjs/winston/blob/master/docs/transports.md#file-transport Open a file for appending and then

winston.add(winston.transports.File, { stream: my_already_opened_file} )

If that's not possible, you could try and generate a new filename based on the process number, for instance, or use another logging option. There does not seem to be a way in winston to append existing log files. There are also several ways of generating random or temporary file names: nodejs - Temporary file name but you can always use the filename itself as a store for a sequence name: "log-x.log", por instance, and read the file at the beginning of your program and create another with the sequence number incremented by one

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.