繁体   English   中英

使用Bunyan记录器登录Node.js

[英]Logging in nodejs using bunyan logger

我在nodejs代码中初始化bunyan记录器,如下所示:

var log = bunyan.createLogger({
    name: 'myapp',
    stream: process.stdout,
    level: 'info'
});

这是来自https://www.npmjs.org/package/bunyan上的bunyan文档

文档提到默认情况下,日志输出为stdout并处于“信息”级别。

我想知道的是,记录器是否应要求我在其中记录事情的文件名。 日志记录在哪里发生?

尝试这个 ...

var Logger = require('bunyan');
var applogger = new Logger({
  name: 'helloapi',
  streams: [
    {
      level: 'info',
      path: './log/applogging.log'
    }
  ]
});

检查其主页以获取更多说明https://github.com/trentm/node-bunyan#streams-introduction

希望这有所帮助。 :)

process.stdout is for Logging on Console.

如果要登录文件,则需要提供不同的流。

var log = bunyan.createLogger({ 
                name: 'myapp',
                streams: [
                    {
                        level: 'trace',
                        stream: process.stdout
                    },
                    {
                        level: 'warn',
                        path: './filename.log'
                    }
                ]
            });

安装bunyan记录器的步骤:

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

通过bunyan-rotating-file-stream模块,我们可以将日志文件设置为datetime。

手动创建日志文件夹。

    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.
            })
        }]
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM