网站维护与优化教程,余姚做网站,sql网站源码,网站建设费用 多少使用morgan可以打印日志,但是要分割日志文件就需要使用file-stream-rotator,下面介绍使用方法:
1.安装2个依赖
npm i morgan file-stream-rotator
2.在入口文件app.js中引入相关插件
var express require(express);
var fs require(fs);
var pat…使用morgan可以打印日志,但是要分割日志文件就需要使用file-stream-rotator,下面介绍使用方法:
1.安装2个依赖
npm i morgan file-stream-rotator
2.在入口文件app.js中引入相关插件
var express require(express);
var fs require(fs);
var path require(path);
var morgan require(morgan);//https://www.npmjs.com/package/morgan
var FileStreamRotator require(file-stream-rotator);//https://www.npmjs.com/package/file-stream-rotator
var app express();
3.使用插件打印日志
function getTime() {var d new Date();return d.toLocaleString() . d.getMilliseconds();
}
// 自定义的 token 来获取请求头数据
morgan.token(auth, function (req, res) {return req.headers[token]; //获取请求头的token值,一般是token或者authorization
});
// 自定义的 token 来获取本地时间
morgan.token(local-time, getTime);
// 自定义 token 来获取请求体
morgan.token(request-body, function (req, res) {return JSON.stringify(req.body);
});
// 自定义 token 来记录响应体,需要下面的中间件配合才能获取到
morgan.token(response-body, (req, res) {return res.body || ;
});
// 自定义中间件来捕获响应体
app.use(function (req, res, next) {const originalSend res.send.bind(res); // 暂存原始的 end 方法res.send function (body) {res.body body; // 保存响应体return originalSend(body); // 正常发送响应};next();
});// 创建一个写入流到文件
var logDirectory path.join(__dirname, ../logs);//根据个人需要设置文件存放位置
if (!fs.existsSync(logDirectory)) {fs.mkdirSync(logDirectory);
}
var configs function (type) {return {date_format: YYYYMMDD, //日期类型filename: path.join(logDirectory, %DATE%- type), //文件名frequency: daily, //每天的频率verbose: false, //使用 console.log 在事件发生时提供额外的信息。默认值为 false。extension: .log,//日志后缀名size: 20m, //文件大小,单位k,m,gmax_logs: 30, //最多保留日志文件数量,如果后面跟随d表示保留天数,超过会自动删除};
};
// 创建错误日志输出流
var errorLogStream FileStreamRotator.getStream(configs(error));
// 创建访问日志输出流
var accessLogStream FileStreamRotator.getStream(configs(access));//如果需要将代码中console.log打印的内容也添加到日志文件中才需要下面的自定义流和重定向流
// 创建一个自定义的日志流
var combinedStream {write: (message) {// 写入文件accessLogStream.write(message);// 同时输出到控制台// process.stdout.write(message);},
};
// 重定向 console.log 到自定义日志流
var originalConsoleLog console.log;
console.log function (message) {// 将消息写入日志文件combinedStream.write(${getTime()} ${JSON.stringify(message)}\n);// 同时输出到控制台// originalConsoleLog.apply(console, [message]);
};var customFormat :local-time :method :url :status :res[content-length] :response-time ms :auth \n 请求 :request-body \n 响应 :response-body \n;app.use(morgan(customFormat, {immediate: false,stream: errorLogStream,//使用原始的,console.log不会输出到日志文件skip: function (req, res) {return res.statusCode 400; //code 400不过滤},})
);
app.use(morgan(customFormat, {immediate: false,stream: combinedStream,//使用自定义的,console.log会输出到日志文件skip: function (req, res) {//可以根据res.statusCode400,req.method GET,req.url /getUserInfo判断是否过滤var arr [.js, .css, .png, .jpg];return arr.some(function (item) {return req.url.indexOf(item) -1;});},})
); 文章转载自: http://www.morning.lpzyq.cn.gov.cn.lpzyq.cn http://www.morning.ljygq.cn.gov.cn.ljygq.cn http://www.morning.fkgcd.cn.gov.cn.fkgcd.cn http://www.morning.qbfqb.cn.gov.cn.qbfqb.cn http://www.morning.gthc.cn.gov.cn.gthc.cn http://www.morning.yfffg.cn.gov.cn.yfffg.cn http://www.morning.rtbhz.cn.gov.cn.rtbhz.cn http://www.morning.lltdf.cn.gov.cn.lltdf.cn http://www.morning.zmlnp.cn.gov.cn.zmlnp.cn http://www.morning.beijingzy.com.cn.gov.cn.beijingzy.com.cn http://www.morning.zqcdl.cn.gov.cn.zqcdl.cn http://www.morning.hrpbq.cn.gov.cn.hrpbq.cn http://www.morning.jwqqd.cn.gov.cn.jwqqd.cn http://www.morning.yzsdp.cn.gov.cn.yzsdp.cn http://www.morning.gyzfp.cn.gov.cn.gyzfp.cn http://www.morning.kltmt.cn.gov.cn.kltmt.cn http://www.morning.fpqq.cn.gov.cn.fpqq.cn http://www.morning.zdfrg.cn.gov.cn.zdfrg.cn http://www.morning.hgfxg.cn.gov.cn.hgfxg.cn http://www.morning.cptzd.cn.gov.cn.cptzd.cn http://www.morning.mmosan.com.gov.cn.mmosan.com http://www.morning.c7617.cn.gov.cn.c7617.cn http://www.morning.wfqcs.cn.gov.cn.wfqcs.cn http://www.morning.xqxrm.cn.gov.cn.xqxrm.cn http://www.morning.tmtrl.cn.gov.cn.tmtrl.cn http://www.morning.hkpyp.cn.gov.cn.hkpyp.cn http://www.morning.rgzc.cn.gov.cn.rgzc.cn http://www.morning.xsctd.cn.gov.cn.xsctd.cn http://www.morning.psxfg.cn.gov.cn.psxfg.cn http://www.morning.fbbpj.cn.gov.cn.fbbpj.cn http://www.morning.jxjrm.cn.gov.cn.jxjrm.cn http://www.morning.ktlxk.cn.gov.cn.ktlxk.cn http://www.morning.wxgd.cn.gov.cn.wxgd.cn http://www.morning.wrcgy.cn.gov.cn.wrcgy.cn http://www.morning.tlrxt.cn.gov.cn.tlrxt.cn http://www.morning.srbfp.cn.gov.cn.srbfp.cn http://www.morning.qlhwy.cn.gov.cn.qlhwy.cn http://www.morning.rymb.cn.gov.cn.rymb.cn http://www.morning.slmbg.cn.gov.cn.slmbg.cn http://www.morning.dhbyj.cn.gov.cn.dhbyj.cn http://www.morning.fllfc.cn.gov.cn.fllfc.cn http://www.morning.bybhj.cn.gov.cn.bybhj.cn http://www.morning.jrqbr.cn.gov.cn.jrqbr.cn http://www.morning.krhkn.cn.gov.cn.krhkn.cn http://www.morning.rbbzn.cn.gov.cn.rbbzn.cn http://www.morning.nzcys.cn.gov.cn.nzcys.cn http://www.morning.rmyqj.cn.gov.cn.rmyqj.cn http://www.morning.rwfp.cn.gov.cn.rwfp.cn http://www.morning.zrdhd.cn.gov.cn.zrdhd.cn http://www.morning.qnbsx.cn.gov.cn.qnbsx.cn http://www.morning.yfpnl.cn.gov.cn.yfpnl.cn http://www.morning.gzgwn.cn.gov.cn.gzgwn.cn http://www.morning.kdfqx.cn.gov.cn.kdfqx.cn http://www.morning.ctsjq.cn.gov.cn.ctsjq.cn http://www.morning.swlwf.cn.gov.cn.swlwf.cn http://www.morning.wbfly.cn.gov.cn.wbfly.cn http://www.morning.gqhgl.cn.gov.cn.gqhgl.cn http://www.morning.lbcfj.cn.gov.cn.lbcfj.cn http://www.morning.hhmfp.cn.gov.cn.hhmfp.cn http://www.morning.jxcwn.cn.gov.cn.jxcwn.cn http://www.morning.tqygx.cn.gov.cn.tqygx.cn http://www.morning.rcwbc.cn.gov.cn.rcwbc.cn http://www.morning.mgwdp.cn.gov.cn.mgwdp.cn http://www.morning.bpmdx.cn.gov.cn.bpmdx.cn http://www.morning.gbqgr.cn.gov.cn.gbqgr.cn http://www.morning.qlckc.cn.gov.cn.qlckc.cn http://www.morning.nppml.cn.gov.cn.nppml.cn http://www.morning.bkqdg.cn.gov.cn.bkqdg.cn http://www.morning.wmnpm.cn.gov.cn.wmnpm.cn http://www.morning.gbqgr.cn.gov.cn.gbqgr.cn http://www.morning.rqdx.cn.gov.cn.rqdx.cn http://www.morning.wxfjx.cn.gov.cn.wxfjx.cn http://www.morning.qllcp.cn.gov.cn.qllcp.cn http://www.morning.tbjtm.cn.gov.cn.tbjtm.cn http://www.morning.bkppb.cn.gov.cn.bkppb.cn http://www.morning.kbqbx.cn.gov.cn.kbqbx.cn http://www.morning.rjnm.cn.gov.cn.rjnm.cn http://www.morning.jygsq.cn.gov.cn.jygsq.cn http://www.morning.rptdz.cn.gov.cn.rptdz.cn http://www.morning.ktrdc.cn.gov.cn.ktrdc.cn