内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

如何在項(xiàng)目中使用log4.js的方法步驟

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

成都創(chuàng)新互聯(lián)長(zhǎng)期為上千客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為松桃企業(yè)提供專業(yè)的網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì),松桃網(wǎng)站改版等技術(shù)服務(wù)。擁有10余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。

log4的配合

// config.js

let path = require('path');

// 日志根目錄
let baseLogPath = path.resolve(__dirname, '../../../logs');
// 請(qǐng)求日志目錄
let reqPath = '/request';
// 請(qǐng)求日志文件名
let reqFileName = 'request';
// 請(qǐng)求日志輸出完整路徑
let reqLogPath = baseLogPath + reqPath + '/' + reqFileName;


// 響應(yīng)日志目錄
let resPath = '/response';
// 響應(yīng)日志文件名
let resFileName = 'response';
// 響應(yīng)日志輸出完整路徑
let resLogPath = baseLogPath + resPath + '/' + resFileName;

// 錯(cuò)誤日志目錄
let errPath = '/error';
// 錯(cuò)誤日志文件名
let errFileName = 'error';
// 錯(cuò)誤日志輸出完整路徑
let errLogPath = baseLogPath + errPath + '/' + errFileName;


module.exports = {
  appenders: {
    // 所有的日志
    'console': {type: 'console'},
    // 請(qǐng)求日志
    'reqLogger': {
      type: 'dateFile', // 日志類型
      filename: reqLogPath, // 輸出文件名
      pattern: '-yyyy-MM-dd-hh.log', // 后綴
      alwaysIncludePattern: true, // 上面兩個(gè)參數(shù)是否合并
      encoding: 'utf-8', // 編碼格式
      maxLogSize: 1000, // 最大存儲(chǔ)內(nèi)容
    },
    // 響應(yīng)日志
    'resLogger': {
      type: 'dateFile',
      filename: resLogPath,
      pattern: '-yyyy-MM-dd-hh.log',
      alwaysIncludePattern: true,
      encoding: 'utf-8',
      maxLogSize: 1000,
    },
    // 錯(cuò)誤日志
    'errLogger': {
      type: 'dateFile',
      filename: errLogPath,
      pattern: '-yyyy-MM-dd-hh.log',
      alwaysIncludePattern: true,
      encoding: 'utf-8',
      maxLogSize: 1000,
    }
  },
  // 分類以及日志等級(jí)
  categories: {
    default: {
      appenders: ['console'],
      level: 'all'
    },
    reqLogger: {
      appenders: ['reqLogger'],
      level: 'info'
    },
    resLogger: {
      appenders: ['resLogger'],
      level: 'info'
    },
    errLogger: {
      appenders: ['errLogger'],
      level: 'error'
    }
  },
}

log4的日志封裝

這里是把log4封裝成一個(gè)中間件,在app.js中直接調(diào)用就可以了

// 先安裝log4js

// log4.js

const log4Config = require('./config')
const log4js = require('log4js')

// 調(diào)用配置文件
log4js.configure(log4Config)


class CommonHandle {
  constructor(){}
  // 格式化請(qǐng)求日志
  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
  }
  // 格式化相應(yīng)日志
  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
  }
  // 格式化錯(cuò)誤日志
  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()
  }

  // 請(qǐng)求日志
  static reqLogger(ctx){
    log4js.getLogger('reqLogger').info(this.formatReqLog(ctx))
  }

  // 相應(yīng)日志
  static resLogger(ctx, time){
    log4js.getLogger('resLogger').info(this.formatResLog(ctx,time))
  }

  // 錯(cuò)誤日志
  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{
      // 請(qǐng)求日志
      HandleLogger.reqLogger(ctx)
      await next()
      period = new Date() - startTime
      // 響應(yīng)日志
      HandleLogger.resLogger(ctx,period)
    }catch(err){
      period = new Date() - startTime
      // 錯(cuò)誤日志
      HandleLogger.errorLogger(ctx, err, period)
    }
  }
}

調(diào)用封裝好的日志函數(shù)

這里直接以中間件的形式調(diào)用就可以了

// app.js

const Koa = require('koa')
const app = new Koa()
const LogJS = require('./common/log/log4')

// log4.js引入
app.use(LogJS())

最后部署上線之后就能直接在根目錄下的logs文件夾下查看對(duì)應(yīng)的日志內(nèi)容。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)頁(yè)名稱:如何在項(xiàng)目中使用log4.js的方法步驟
標(biāo)題鏈接:http://www.rwnh.cn/article24/gjhije.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、ChatGPT、Google營(yíng)銷型網(wǎng)站建設(shè)、微信公眾號(hào)、建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司
定日县| 东源县| 云南省| 梁山县| 泗洪县| 平安县| 浙江省| 桂林市| 武义县| 巴东县| 泸水县| 石城县| 怀远县| 海阳市| 浮梁县| 安远县| 瓮安县| 望江县| 伊吾县| 宣威市| 洪洞县| 清流县| 桂平市| 金华市| 边坝县| 苗栗市| 遂川县| 弥渡县| 东方市| 榆树市| 根河市| 正镶白旗| 清丰县| 宜君县| 犍为县| 普宁市| 棋牌| 泰来县| 万宁市| 石首市| 微山县|