Logging in nodejs using bunyan logger

后端 未结 3 1639
Happy的楠姐
Happy的楠姐 2021-01-15 12:39

I am initializing the bunyan logger in my nodejs code as below:

var log = bunyan.createLogger({
    name: \'myapp\',
    stream: process.stdout,
    level: \         


        
3条回答
  •  梦谈多话
    2021-01-15 13:29

    Steps to install bunyan logger:

    npm install bunyan bunyan-rotating-file-stream --save
    

    By bunyan-rotating-file-stream module we can set the logfile with datetime.

    create logs folder manually.

        var bunyan  = require('bunyan');
        var RotatingFileStream = require('bunyan-rotating-file-stream');
    
        var applogger = new bunyan.createLogger({
            name: 'project name',            
            streams: [{
                stream: new RotatingFileStream({
                    type: 'rotating-file',
                    path: './logs/server-%Y%m%d.log',
                    period: '1d',          // daily rotation 
                    totalFiles: 2,        // keep up to 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 
                    template: 'server-%Y%m%d.log' //you can add. - _ before datestamp.
                })
            }]
        });
    

提交回复
热议问题