Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4js javascript create daily log file

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 :)

like image 498
phuchoangmai Avatar asked Mar 08 '23 10:03

phuchoangmai


2 Answers

winston is recommended for nodejs. Its pretty easy to use.

Create a logger.js file and have this configuration '

require('winston-daily-rotate-file');

var winston = require('winston');

winston.loggers.add('logger', {
    transports: [
new (winston.transports.Console)(
            {
                level: config.debugLogLevel,
                colorize: true
            }),

        //new files will be generated each day, the date patter indicates the frequency of creating a file.
        new winston.transports.DailyRotateFile({
                name: 'debug-log',
                filename: '<log file name>',
                level: '<level>',
                prepend: true,
                datePattern: '<pattern>',
                maxFiles: <max file>
            }
        ),
        new (winston.transports.DailyRotateFile)({
            name: 'error-log',
            filename: '<log file name>',
                level: '<level>',
                prepend: true,
                datePattern: '<pattern>',
                maxFiles: <max file>
        })
    ]
});

var logger = winston.loggers.get('logger');
Object.defineProperty(exports, "LOG", {value: logger});

now you can use it anywhere like

var log = require('./logger.js').LOG
log.error('hello');
like image 185
Kumawat Avatar answered Mar 30 '23 11:03

Kumawat


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.

like image 37
Dev9 Avatar answered Mar 30 '23 11:03

Dev9