1

I am newbie in node.js and want to create new log/debug file on each day to print console.log value within it because if there is one file it become large in size and unable to view properly too.

please give me suggestion how can i do?

Any running example will appreciate me.

httpNick
  • 2,524
  • 1
  • 22
  • 34
vicky
  • 77
  • 1
  • 7
  • Are you creating log file manually or using some npm package? Can you put the code that you are executing? It would be easy to help if we know what you're doing. – Pratik Gaikwad Sep 27 '16 at 13:52
  • 1
    Use `logrotate` or `PM2`, a process manager in Node also helps with log rotation. – psiyumm Sep 27 '16 at 13:52
  • var log_file = fs.createWriteStream('./debug' + timeStamp + '.log', { flags: 'w' }); console.log = function(d) { // var dt = new Date(); var utcDate = dt.toUTCString(); log_file.write(utcDate + " : " + util.format(d) + '\n'); }; – vicky Sep 27 '16 at 13:56
  • I am doing as per above code... – vicky Sep 27 '16 at 13:56

2 Answers2

3

Such element of any program is called 'logger' . Logger can easily handle your log files.

There are a lot of implementation for logging of your server

You can find any logger either npm or github or even in Google;)

for example, npm install winston

https://github.com/winstonjs/winston

  var winston = require('winston');

  winston.log('info', 'Hello distributed log files!');
  winston.info('Hello again distributed logs');

  winston.level = 'debug';
  winston.log('debug', 'Now my debug messages are written to console!');
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
0

I would suggest to rotate log based on size of the log file, using size you can do it as given below.

You can use logrotate-stream for this.

  1. Install it globally using npm install -g logrotate-stream

  2. Run your server using node app.js 2>&1 | logrotate-stream app.log --keep 5 --size '1m' --compress

Explanation

2>&1 send stderr to stdout (detail about 2>&1)

| pipe output(here stderr and stdout) to next command as input (here logrotate-stream)

app.log log file name

--keep 5 maximum number of file to keep

--size 1m maximum size of each file in mb --compmress compress files

Log files will look like this

app.log
app.log.0.gz
app.log.1.gz
app.log.2.gz
app.log.3.gz
app.log.4.gz
Community
  • 1
  • 1
suraj.tripathi
  • 417
  • 2
  • 15
  • Thanks but still unable to create log file for each day. – vicky Oct 01 '16 at 11:37
  • thanks for everyone but I found its by another way and its working properly. – vicky Nov 03 '16 at 09:37
  • /*test1.js*/ var fs = require('fs'); var logs = require('tracer').colorConsole({ format : "{{timestamp}} <{{title}}>: {{message}} ==>> (in {{file}}:{{line}})", dateformat : "yyyy_mm_dd_hh:MM:ss TT", transport : function(log) { console.log(log.output); var timestamp = log.timestamp.substring(0,10); fs.open('./debug.' + timestamp + '.log', 'a', function(err, fileId) { fs.write(fileId, log.output + "\n", null, 'utf8', function() { fs.close(fileId, function() { }); }); }); } }); module.exports = logs; – vicky Nov 03 '16 at 10:55
  • /*test2.js*/ var logger = require('./loggerFile.js'); setInterval(function() { test(); }, 2 * 60 * 1000); function test(){ logger.debug("inside test function."); logger.info("within if condition."); logger.error("this is not an error."); } – vicky Nov 03 '16 at 10:59