国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

nodejs 日志模塊winston的使用方法

2019-11-19 13:55:49
字體:
供稿:網(wǎng)友

winston 日志模塊

在使用 nodejs winston 模塊中,加上相關(guān)的兩個(gè)模塊,事倍功半。

  1. express-winston
  2. winston-daily-rotate-file

express-winston

是 express-winston 的 winston 的增加版, 是作為 express 的中間件來打印日志,不僅有請求頭信息,并且有響應(yīng)時(shí)間。
作為中間件, 為什么會有響應(yīng)時(shí)間呢? 因?yàn)?express-winston 改寫了 express 的 res.end 辦法, 是請求結(jié)束后再打的日志。

代碼片段

var end = res.end;res.end = function(chunk, encoding) { res.responseTime = (new Date) - req._startTime; res.end = end; res.end(chunk, encoding); ... }

express-winston 沒有修改或者擴(kuò)展 winston 的transport, 而 winston-daily-rotate-file 正是增強(qiáng)了 winston 的transport 辦法

winston-daily-rotate-file

winston-daily-rotate-file 是 winston 擴(kuò)展, 增加了 transport 的辦法,使 winston 有滾動日志的能力。

結(jié)合使用

我們來一個(gè)需求: 如何讓 express-winston 打印日志的時(shí)候,也打印出接口 /api 的請求參數(shù)和響應(yīng)數(shù)據(jù)?

  1. 該日志中間件應(yīng)該在調(diào)用鏈 api 后面, api/* 業(yè)務(wù)處理之前。 like: app.use('/api', apiRequestLogger, apiHandler)
  2. 要獲取到響應(yīng)數(shù)據(jù), 就要在業(yè)務(wù)處理完后 send 出來后才能捕獲到,express 所有的請求響應(yīng)最后都是走 res.send 我們可以從這里入手捕獲響應(yīng)數(shù)據(jù)

代碼如下

import winston from 'winston'import expressWinston from 'express-winston'import 'winston-daily-rotate-file'import path from 'path'export let DailyRotateFileTransport = (fileName) => { return new (winston.transports.DailyRotateFile)({ filename: path.join(process.env.LOGPATH, `${fileName}-%DATE%.log`), datePattern: 'YYYY-MM-DD-HH', // maxSize: '20m', maxFiles: '7d', timestamp: () => new Date().format('yyyy-MM-dd hh:mm:ss.S') })}export let pageRequestLogger = expressWinston.logger({ transports: [ DailyRotateFileTransport('page-request') ], meta: true, // optional: control whether you want to log the meta data about the request (default to true) msg: 'HTTP {{req.method}} {{req.url}}', // optional: customize the default logging message. E.g. "{{res.statusCode}} {{req.method}} {{res.responseTime}}ms {{req.url}}" expressFormat: true, // Use the default Express/morgan request formatting. Enabling this will override any msg if true. Will only output colors with colorize set to true colorize: false, // Color the text and status code, using the Express/morgan color palette (text: gray, status: default green, 3XX cyan, 4XX yellow, 5XX red). ignoreRoute: function (req, res) { // 只打印頁面請求信息 let notPageRequest = false let ignoreArr = ['/api', '.js', '.css', '.png', '.jpg', '.gif'] ignoreArr.forEach(item => {  if (req.url.indexOf(item) > -1) notPageRequest = true }) return notPageRequest } // optional: allows to skip some log messages based on request and/or response})export let apiRequestLogger = (req, res, next) => { let send = res.send let content = '' let query = req.query || {} let body = req.body || {} res.send = function () { content = arguments[0] send.apply(res, arguments) } expressWinston.logger({ transports: [  DailyRotateFileTransport('api-request') ], meta: true, // optional: control whether you want to log the meta data about the request (default to true) msg () {  return `HTTP ${req.method} ${req.url} query ${JSON.stringify(query)} body ${JSON.stringify(body)} resData ${content} ` }, colorize: true, // Color the text and status code, using the Express/morgan color palette (text: gray, status: default green, 3XX cyan, 4XX yellow, 5XX red). ignoreRoute: function (req, res) {  if (req.headers.self) return true  return false } // optional: allows to skip some log messages based on request and/or response })(req, res, next)}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 新巴尔虎左旗| 西藏| 闽侯县| 高陵县| 施甸县| 亚东县| 长治县| 大渡口区| 山东省| 馆陶县| 温州市| 陆川县| 曲周县| 泾阳县| 崇左市| 上思县| 新疆| 赣州市| 河南省| 文成县| 香格里拉县| 旅游| 文昌市| 淳化县| 根河市| 勐海县| 太原市| 建湖县| 陆良县| 文水县| 长寿区| 龙海市| 亳州市| 原阳县| 公安县| 清原| 西乌珠穆沁旗| 盐边县| 新昌县| 休宁县| 石嘴山市|