From 9e225f5daabd1727e2cb19d4d76e167cfa592842 Mon Sep 17 00:00:00 2001 From: undefined Date: Wed, 9 Sep 2020 23:17:55 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4koa-morgan?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/hydrooj/package.json | 4 +--- packages/hydrooj/src/service/server.ts | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/packages/hydrooj/package.json b/packages/hydrooj/package.json index f15e97758..8b831a3be 100644 --- a/packages/hydrooj/package.json +++ b/packages/hydrooj/package.json @@ -1,6 +1,6 @@ { "name": "hydrooj", - "version": "2.13.8", + "version": "2.13.9", "bin": "bin/hydrooj.js", "main": "dist/loader.js", "typings": "dist/loader.d.ts", @@ -19,7 +19,6 @@ "js-yaml": "^3.13.1", "koa": "^2.13.0", "koa-body": "^4.2.0", - "koa-morgan": "^1.0.1", "koa-router": "^9.4.0", "koa-static-cache": "^5.1.3", "lodash": "^4.17.20", @@ -40,7 +39,6 @@ "@types/fs-extra": "^9.0.1", "@types/js-yaml": "^3.12.5", "@types/koa": "^2.11.4", - "@types/koa-morgan": "^1.0.4", "@types/koa-router": "^7.4.1", "@types/koa-static-cache": "^5.1.0", "@types/lodash": "^4.14.161", diff --git a/packages/hydrooj/src/service/server.ts b/packages/hydrooj/src/service/server.ts index 93408a616..59729c4ab 100644 --- a/packages/hydrooj/src/service/server.ts +++ b/packages/hydrooj/src/service/server.ts @@ -7,8 +7,7 @@ import http from 'http'; import moment from 'moment-timezone'; import { isSafeInteger, Dictionary, filter } from 'lodash'; import { ObjectID } from 'mongodb'; -import Koa from 'koa'; -import morgan from 'koa-morgan'; +import Koa, { Context } from 'koa'; import Body from 'koa-body'; import Router from 'koa-router'; import cache from 'koa-static-cache'; @@ -806,9 +805,21 @@ export function Connection( export async function start() { const port = await system.get('server.port'); if (argv.debug) { - app.use(morgan(':method :url :status :res[content-length] - :response-time ms', { - skip: (req, res) => res.hasHeader('nolog'), - })); + app.use(async (ctx: Context, next: Function) => { + const startTime = new Date().getTime(); + await next(); + const endTime = new Date().getTime(); + if (ctx.response.headers.nolog) return; + ctx._remoteAddress = ctx.request.ip; + const status = ctx.response.status; + const color = status >= 500 ? 31 // red + : status >= 400 ? 33 // yellow + : status >= 300 ? 36 // cyan + : status >= 200 ? 32 // green + : 0; // no color + logger.debug(`\ +\x1b[0m${ctx.request.method} ${ctx.request.path} \x1b[${color}m${ctx.response.status}\x1b[0m ${endTime - startTime}ms ${ctx.response.length}\x1b[0m`); + }); } app.use(async (ctx, next) => { const xff = await system.get('server.xff');