1

As I have seen many logs in blogs and I find bunyan suitable for logging but there is problem with it that it can't log to file according to their level.

Below is code structure I am following

const RotatingFileStream = require('bunyan-rotating-file-stream');
const bunyan = require('bunyan');

    var log = bunyan.createLogger({
          name: 'ShotPitch',


          streams: [{
            name: 'info',
            level: 'info',
            stream: new RotatingFileStream({
              path: 'info.%d-%b-%y.log',
              period: '1d', // daily rotation
              totalFiles: 10, // keep 10 back copies
              rotateExisting: true, // Give ourselves a clean file when we start up, based on period
              threshold: '10m', // Rotate log files larger than 10 megabytes
              totalSize: '20m', // Don't keep more than 20mb of archived log files
              gzip: true // Compress the archive log files to save space
            })
          }, {
            name: 'error',
            level: 'error',
            stream: new RotatingFileStream({
              path: 'error.%d-%b-%y.log',
              period: '1d', // daily rotation
              totalFiles: 10, // keep 10 back copies
              rotateExisting: true, // Give ourselves a clean file when we start up, based on period
              threshold: '10m', // Rotate log files larger than 10 megabytes
              totalSize: '20m', // Don't keep more than 20mb of archived log files
              gzip: true // Compress the archive log files to save space
            })
          }] 
        });

 log.info('Hello World');
 log.error('Hello World error log');

o/p: info.log :

{"name":"ShotPitch", "pid":7621,"level":30,"msg":"Hello World","time":"2017-09-03T18:29:04.181Z","v":0}

{"name":"ShotPitch", "pid":7621,"level":50,"msg":"Hello World","time":"2017-09-03T18:29:04.181Z","v":0}

o/p: error.log :

{"name":"ShotPitch", "pid":7621,"level":50,"msg":"Hello World","time":"2017-09-03T18:29:04.181Z","v":0}

Conclusion:

info.log shows both info and error logs

error.log shows only error logs

I want info logs only in info.log but unable to do. Is there anyone that can help ?. Also if tell me how to change to level: "info" rather than level:30

raj
  • 388
  • 5
  • 24

2 Answers2

0

A log level for a rotating file stream needs to be specified when configuring bunyan.

By default, log output is to stdout and at the "info" level.

Setting a logger instance (or one of its streams) to a particular level implies that all log records at that level and above are logged. E.g. a logger set to level "info" will log records at level info and above (warn, error, fatal).

Error logs are thus also collected to the info log.

Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81
  • I can't make separate instance just for separate logging. If possible in single instance that is ok for me. Also if level set to string that is info,error then it would be more feasible to read logs – raj Sep 03 '17 at 19:50
  • You can only set a level of severity. See the severity levels here https://github.com/trentm/node-bunyan#levels – Oluwafemi Sule Sep 03 '17 at 19:55
0

I met the same problem, finally I created 2 variables like:

var debugLog = bunyan.createLogger({
  name: 'general',
  streams: [
    {
      level: 'debug',
      path: './debug.log'
    }]
});

var errLog = bunyan.createLogger({
  name: 'general',
  streams: [
    {
      level: 'error',
      path: './error.log'
    }]
});

debugLog.info('hello world');
errLog.error('error');

Then logs will be in different log files.

Rob
  • 26,989
  • 16
  • 82
  • 98
liuxuqing
  • 1
  • 1