最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

如何在项目中使用log4.js的方法步骤

来源:懂视网 责编:小采 时间:2020-11-27 21:53:47
文档

如何在项目中使用log4.js的方法步骤

如何在项目中使用log4.js的方法步骤:pm2中自带的日志内容是不能满足日常的需求的,因此需要在项目中加上日志管理,这里研究了下log4的使用方法,效果挺好的,想要查看的都可以找到,记录下简单的使用步骤 log4的配合 // config.js let path = require('path'); // 日志根目
推荐度:
导读如何在项目中使用log4.js的方法步骤:pm2中自带的日志内容是不能满足日常的需求的,因此需要在项目中加上日志管理,这里研究了下log4的使用方法,效果挺好的,想要查看的都可以找到,记录下简单的使用步骤 log4的配合 // config.js let path = require('path'); // 日志根目

pm2中自带的日志内容是不能满足日常的需求的,因此需要在项目中加上日志管理,这里研究了下log4的使用方法,效果挺好的,想要查看的都可以找到,记录下简单的使用步骤

log4的配合

// config.js

let 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.js

const 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.js

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

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

最后部署上线之后就能直接在根目录下的logs文件夹下查看对应的日志内容。

文档

如何在项目中使用log4.js的方法步骤

如何在项目中使用log4.js的方法步骤:pm2中自带的日志内容是不能满足日常的需求的,因此需要在项目中加上日志管理,这里研究了下log4的使用方法,效果挺好的,想要查看的都可以找到,记录下简单的使用步骤 log4的配合 // config.js let path = require('path'); // 日志根目
推荐度:
标签: 使用 过程 js
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top