koa-program/template/extend/logger.ts

48 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

2021-02-10 03:40:48 +08:00
import { lucky } from "./lucky";
import { Context, Next } from "koa";
/**
*
*
* @param app {Context | string}
* @param next {Next | function}
* @return {Promise<void>}
*/
async function logger(app: Context, next: Next): Promise<void> {
// @ts-ignore
let start: Date = new Date();
await next();
// @ts-ignore
let ms: Date = new Date() - start;
console.log(`${setMethodImage(app)} ${lucky.info(app.method)} ${lucky.second(app.url)} ${lucky.primary('['+ ms + "ms]")}`);
}
/**
*
* Application
* @param app {Application.Context}
* @return methodImage {String}
*/
function setMethodImage(app: Context) {
let method = app.method;
let methodImage: string;
switch (method) {
case "GET":
methodImage = "=>";
break;
case "POST":
methodImage = "<= ";
break;
default:
methodImage = "<=>";
break;
}
return methodImage;
}
// 导出
export { logger };