- 前言
- 源码
前言
如果安装了别的日志中间件,可以先删除:
日志中间件主要用于在Koa中输出请求的相关信息,从消息类型看可以分为正常日志信息和异常日志信息,日志的内容取决于其他中间件往上下文ctx
中丢了什么(例如请求响应时间、访客信息等)。
为了顾及两类信息,需要使用try...catch
结构捕获异常,然后再做处理:
1 2 3 4 5
| try { await next(); } catch (e) { logger.error(e); }
|
使用用例如下:
1 2 3 4 5 6 7 8
| const KoaLogger = require('logger.js'); const Koa = require('Koa'); const app = new Koa();
app.use(KoaLogger(console));
app.listen(3000);
|
源码
logger.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| const chalk = require('chalk');
if (!String.prototype.fade) { String.prototype.fade = function() { return this.toString().replace(/\x1B\[[0-9]?[0-9]?m/g, '') + '\r\n'; } }
const KoaLogger = (logger) => { return async(ctx, next) => {
const logArr = []; const req = ctx.request; const { ip, url } = req;
logArr.push(chalk.bold(req.method));
if (!logger || !logger.isInitiatial()) throw new Error('日志未初始化');
try { await next(); } catch (e) { logger.error(e); }
const { status } = ctx.response; if (status >= 200 && status < 300) { logArr.push(chalk.bold(chalk.green(status))); } else if (status >= 400) { logArr.push(chalk.bold(chalk.red(status))); } else { logArr.push(chalk.bold(chalk.yellow(status))); }
if (ctx.response.get('X-Response-Time')) { logArr.push(ctx.response.get('X-Response-Time')); }
logArr.push(chalk.magenta(url));
logArr.push(chalk.cyan(ip));
if (ctx.request.visitor) { const v = ctx.request.visitor;
let str = ''; str += `from ${v.from}`; if (v.from === 'internet') { str += ` (${v.country}/${v.region}/${v.city})`; if (v.isNew) { str += (' ' + chalk.yellow('新访客')); } else { str += (' ' + chalk.yellow(`访问次数:${v.visit_count}, 上次访问时间:${v.last_visit_time}`)); } }
logArr.push(chalk.gray(str)); }
if (ctx.body && typeof ctx.body === 'object' && ctx.request.method !== 'GET') { logArr.push(chalk.gray(JSON.stringify(ctx.body))); }
logger.info(logArr.join(' - '));
if (ctx.request.body && Object.keys(ctx.request.body).length !== 0) { let str = ''; if (ctx.request.encBody) { str += chalk.yellow('Succeed Decrypt! '); } str += 'Request Body: ' + chalk.gray(JSON.stringify(ctx.request.body)) logger.info(str); }
if (ctx.request.header.cookie) { logger.info('Cookies: ' + chalk.gray(ctx.request.header.cookie)); } } }
module.exports = KoaLogger;
|