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

首頁 > 開發 > JS > 正文

如何在項目中使用log4.js的方法步驟

2024-05-06 16:53:20
字體:
來源:轉載
供稿:網友

pm2中自帶的日志內容是不能滿足日常的需求的,因此需要在項目中加上日志管理,這里研究了下log4的使用方法,效果挺好的,想要查看的都可以找到,記錄下簡單的使用步驟

log4的配合

// config.jslet path = require('path');// 日志根目錄let baseLogPath = path.resolve(__dirname, '../../../logs');// 請求日志目錄let reqPath = '/request';// 請求日志文件名let reqFileName = 'request';// 請求日志輸出完整路徑let reqLogPath = baseLogPath + reqPath + '/' + reqFileName;// 響應日志目錄let resPath = '/response';// 響應日志文件名let resFileName = 'response';// 響應日志輸出完整路徑let resLogPath = baseLogPath + resPath + '/' + resFileName;// 錯誤日志目錄let errPath = '/error';// 錯誤日志文件名let errFileName = 'error';// 錯誤日志輸出完整路徑let errLogPath = baseLogPath + errPath + '/' + errFileName;module.exports = {  appenders: {    // 所有的日志    'console': {type: 'console'},    // 請求日志    'reqLogger': {      type: 'dateFile', // 日志類型      filename: reqLogPath, // 輸出文件名      pattern: '-yyyy-MM-dd-hh.log', // 后綴      alwaysIncludePattern: true, // 上面兩個參數是否合并      encoding: 'utf-8', // 編碼格式      maxLogSize: 1000, // 最大存儲內容    },    // 響應日志    'resLogger': {      type: 'dateFile',      filename: resLogPath,      pattern: '-yyyy-MM-dd-hh.log',      alwaysIncludePattern: true,      encoding: 'utf-8',      maxLogSize: 1000,    },    // 錯誤日志    'errLogger': {      type: 'dateFile',      filename: errLogPath,      pattern: '-yyyy-MM-dd-hh.log',      alwaysIncludePattern: true,      encoding: 'utf-8',      maxLogSize: 1000,    }  },  // 分類以及日志等級  categories: {    default: {      appenders: ['console'],      level: 'all'    },    reqLogger: {      appenders: ['reqLogger'],      level: 'info'    },    resLogger: {      appenders: ['resLogger'],      level: 'info'    },    errLogger: {      appenders: ['errLogger'],      level: 'error'    }  },}

log4的日志封裝

這里是把log4封裝成一個中間件,在app.js中直接調用就可以了

// 先安裝log4js// log4.jsconst log4Config = require('./config')const log4js = require('log4js')// 調用配置文件log4js.configure(log4Config)class CommonHandle {  constructor(){}  // 格式化請求日志  static formatReqLog(ctx, time){    let text = '------------request start------------'    let method = ctx.method    text += `request method: ${method} /n request url: ${ctx.originalUrl } /n`    if(method = 'GET'){      text += `request data: ${JSON.stringify(ctx.query)} /n`    }else{      text += `request data: ${JSON.stringify(ctx.body)} /n`    }    text += `ctx all: ${JSON.stringify(ctx)}`    return text  }  // 格式化相應日志  static formatResLog(ctx,time){    let text = '------------response start------------'    text += `response result: ${JSON.stringify(ctx.response.body)} /n`    text += `response all: ${JSON.stringify(ctx)} /n`    text += `response time: ${time} /n`    return text  }  // 格式化錯誤日志  static formatErrorLog(ctx,error,time){    let text = '------------error start------------'    text += this.formatResLog(ctx,time)    text += `error content: ${JSON.stringify(error)}`    return text  }}class HandleLogger extends CommonHandle{  constructor(){    super()  }  // 請求日志  static reqLogger(ctx){    log4js.getLogger('reqLogger').info(this.formatReqLog(ctx))  }  // 相應日志  static resLogger(ctx, time){    log4js.getLogger('resLogger').info(this.formatResLog(ctx,time))  }  // 錯誤日志  static errorLogger(ctx, error, time){    log4js.getLogger('errLogger').info(this.formatErrorLog(ctx,error,time))  }}module.exports = (options) => {  return async (ctx,next) => {    const startTime = new Date()    let period;    try{      // 請求日志      HandleLogger.reqLogger(ctx)      await next()      period = new Date() - startTime      // 響應日志      HandleLogger.resLogger(ctx,period)    }catch(err){      period = new Date() - startTime      // 錯誤日志      HandleLogger.errorLogger(ctx, err, period)    }  }}

調用封裝好的日志函數

這里直接以中間件的形式調用就可以了

// app.jsconst Koa = require('koa')const app = new Koa()const LogJS = require('./common/log/log4')// log4.js引入app.use(LogJS())

最后部署上線之后就能直接在根目錄下的logs文件夾下查看對應的日志內容。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 磐石市| 从化市| 德惠市| 随州市| 安图县| 勃利县| 建瓯市| 静海县| 德昌县| 张家川| 伊春市| 三穗县| 永济市| 广德县| 富平县| 措美县| 泾阳县| 什邡市| 凯里市| 灵山县| 灵川县| 伊川县| 商都县| 德钦县| 鹰潭市| 连山| 曲靖市| 孝义市| 萨嘎县| 东城区| 阳高县| 监利县| 桃江县| 昌都县| 阳西县| 黑河市| 金门县| 碌曲县| 卢氏县| 礼泉县| 工布江达县|