nodejs 日志插件比较 VS 自定义日志插件

in 编程
关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9

morgan

log4js-node

"use strict";
var path = require('path')
, log4js = require('../lib/log4js');

log4js.configure(
  {
    appenders: [
      {
        type: "file",
        filename: "important-things.log",
        maxLogSize: 10*1024*1024, // = 10Mb
        numBackups: 5, // keep five backup files
        compress: true, // compress the backups
        encoding: 'utf-8',
        mode: parseInt('0640', 8),
        flags: 'w+'
      },
      {
        type: "dateFile",
        filename: "more-important-things.log",
        pattern: "yyyy-MM-dd-hh",
        compress: true
      },
      {
        type: "stdout"
      }
    ]
  }
);

var logger = log4js.getLogger('things');
logger.debug("This little thing went to market");
logger.info("This little thing stayed at home");
logger.error("This little thing had roast beef");
logger.fatal("This little thing had none");
logger.trace("and this little thing went wee, wee, wee, all the way home.");

winston

fluent-logger-node

express-winston

如何自定义一个日志插件

使用rotating-file-stream

var path = require('path');
var fs = require('fs');
var rfs = require('rotating-file-stream');

var logDirectory = __dirname;

function Wpad(num) {
    return (num > 9 ? "" : "0") + num;
}


/**
 * [Wgenerator 创建文件名函数]
 * @Author   Wdd
 * @DateTime 2017-02-22T10:13:39+0800
 * 日志会保留一个月的:因为日志文件名是只使用日期,9月8号的日志就会覆盖8月8号的日志
 * 文件的格式是gzip
 * 文件名例如:22-log.gizp
 */
function Wgenerator(time, index) {
    if(! time){
        return "temp-log.txt.gzip";
    }

    return "/storage/"+ Wpad(time.getDate()) +"-log.txt.gzip";
}

var accessLogStream = rfs(Wgenerator, {
    interval: '1d', // 周期为1天
    path: logDirectory,
    compress: 'gzip' ,
    rotationTime:true
});

/**
 * [exports description]
 * @Author   Wdd
 * @DateTime 2017-02-22T10:24:06+0800
 * 使用方式:
 * 1. 安装rotating-file-stream
 * 2. 在根目录下创建一个文件夹,例如logs。然后把access-log.js放进去
 * 3. 在app.js中var mylog = require('./logs/access-log');
 * 4. 在app = express(); 后添加一句 app.use(mylog);
 * 5. 日志文件会自动生成在./logs/storage文件夹下面,当天的日志会保存在暂存的./logs/temp-log.gzip里
 */
module.exports = function(req, res, next){

    req._startTime = new Date();

    res.once('finish', function(){

        var msg = "";
        //hostname
        msg = process.env.hostname+" ";

        // 时间
        msg += new Date()+" ";

        // 请求方式
        msg += req.method+" ";

        // 响应状态码
        msg += res.statusCode+" ";

        // sessionId
        msg += req.headers.sessionid+" ";

        // 响应时长
        msg += new Date() - req._startTime ;

        // 请求路径
        msg += " " + req.originalUrl +'\n\r\n\r';

        accessLogStream.write(msg);
    });

    next();
};
关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9
扫一扫关注公众号添加购物返利助手,领红包
Comments are closed.

推荐使用阿里云服务器

超多优惠券

服务器最低一折,一年不到100!

朕已阅去看看