/** * 核心日志中间件 * logger * @author ranyunlong<549510622@qq.com> * license MIT */ import { TKServer } from "../Server"; import chalk from "chalk"; export default async (ctx: TKServer.Context, next: { (): Promise }) => { ctx.res.on('finish', () => { let code: any = ctx.res.statusCode; if (code > 0 && code < 300) { code = chalk.greenBright(code) } else if (code >= 300 && code < 400) { code = chalk.yellowBright(code) } else { code = chalk.redBright(code) } console.log(`[${chalk.gray(new Date().toLocaleString())}] ${code} ${ctx.method} ${ctx.url}`) }) await next() }