5

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

phuchoangmai
  • 305
  • 1
  • 5
  • 15

3 Answers3

4

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');
ManishKumar
  • 1,476
  • 2
  • 14
  • 22
  • and most interesting part is, you can also configure email as transport. All the log can be emailed you with just some configuration. – ManishKumar Aug 03 '17 at 09:25
  • The first. thank you for your help. I had use search for keyword winston log and have answer. But I have a problem with dependency :). In file package.json I add line "winston-daily-rotate-file": "~1.4.6" but when I run it, have a error Error: Cannot find module 'winston' – phuchoangmai Aug 03 '17 at 09:58
  • Hi @Manish Kumanwat. I try same you answer. But it not working, it not show any error and not create file log? Can you help me – phuchoangmai Aug 03 '17 at 10:16
  • @NguyễnPhúc Can you please provide me detail about configuration that you have in your code? – ManishKumar Aug 03 '17 at 11:31
3

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.

Dev9
  • 31
  • 4
2

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