vicky
vicky

Reputation: 77

How to create a new log every each day

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.

Upvotes: 1

Views: 2763

Answers (2)

suraj.tripathi
suraj.tripathi

Reputation: 467

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

Upvotes: 0

Vyacheslav
Vyacheslav

Reputation: 27221

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!');

Upvotes: 3

Related Questions