Log4js javascript create daily log file

Viewed 6883

I have a project nodejs and use log4js to write log. I want create new file log when start new date.
Example:
daily.2017_07_31.log
daily.2017_08_01.log
daily.2017_08_02.log
daily.2017_08_03.log

In java, I know config log4j but in nodejs with log4js I don't know. Thank every body for your help :)

3 Answers

See: https://github.com/log4js-node/log4js-node/blob/master/docs/dateFile.md

log4js.configure({
  appenders: {
  everything: { type: 'dateFile', filename: 'all-the-logs.log' }
},
   categories: {
     default: { appenders: [ 'everything' ], level: 'debug' }
   }
});

This example will result in files being rolled every day. The initial file will be all-the-logs.log, with the daily backups being all-the-logs.log.2017-04-30, etc.

Not found for daily rolling, but for size the configuration of log4js allows file rolling. Pay attention to maxLogSize, backups and compress properties. Example from its docs:

    log4js.configure({
        appenders: {
            everything: { 
                type: 'file', 
                filename: 'all-the-logs.log', 
                maxLogSize: 10485760, 
                backups: 3, 
                compress: true 
            }
        },
        categories: {
            default: { appenders: [ 'everything' ], level: 'debug'}
        }
    });

See https://github.com/log4js-node/log4js-node/blob/master/docs/file.md

Related