|
@@ -2,15 +2,23 @@ import log4js from 'log4js';
|
|
|
import path from 'path';
|
|
|
import { Context } from 'koa';
|
|
|
import config from '@config';
|
|
|
-
|
|
|
+interface commonInfo {
|
|
|
+ serverIp: string,
|
|
|
+ params?: any,
|
|
|
+ time?: number
|
|
|
+}
|
|
|
// 输出配置
|
|
|
-const output = (ctx, message = '', commonInfo = {}) => {
|
|
|
+const output = (ctx, message: string | null | undefined , commonInfo: commonInfo) => {
|
|
|
const {
|
|
|
method, // 请求方式
|
|
|
headers, // 请求headers
|
|
|
origin, // 获取URL的来源,包括 protocol 和 host。
|
|
|
originalUrl // 获取请求原始URL。
|
|
|
} = ctx.request;
|
|
|
+ const {
|
|
|
+ time,
|
|
|
+ ...newCommonInfo
|
|
|
+ } = commonInfo
|
|
|
interface client {
|
|
|
originalUrl: string,
|
|
|
userAgent: string,
|
|
@@ -23,8 +31,8 @@ const output = (ctx, message = '', commonInfo = {}) => {
|
|
|
if (ctx.querystring) {
|
|
|
client.query = ctx.querystring;
|
|
|
};
|
|
|
- let text = `[${method}] ${ctx.status} [${origin}${originalUrl}]
|
|
|
-req: ${JSON.stringify(Object.assign(client, commonInfo))}`;
|
|
|
+ let text = `[${method}] ${ctx.status} [${origin}${originalUrl}] ${time}ms
|
|
|
+req: ${JSON.stringify(Object.assign(client, newCommonInfo))}`;
|
|
|
|
|
|
if (Object.prototype.toString.call(message) === '[object Object]'
|
|
|
|| Object.prototype.toString.call(message) === '[object Error]') {
|
|
@@ -43,7 +51,7 @@ const methods: string[] = ['trace', 'debug', 'info', 'warn', 'error', 'fatal', '
|
|
|
const defaultConfig = {
|
|
|
logLevel: 'debug', // 日志级别
|
|
|
dir: path.resolve(__dirname, '../log'), // 指定日志存放目录名
|
|
|
- env: 'dev', // 指定当前环境,当开发时控制台也输出
|
|
|
+ env: 'development', // 指定当前环境,当开发时控制台也输出
|
|
|
serverIp: '127.0.0.1'
|
|
|
};
|
|
|
|
|
@@ -158,19 +166,18 @@ export const middlewareLog = () => {
|
|
|
};
|
|
|
});
|
|
|
ctx.$log = contextLogger;
|
|
|
+ const a = Date.now()
|
|
|
await next();
|
|
|
+ const b = Date.now()
|
|
|
if (ctx.response && ctx.status < 400) {
|
|
|
- interface commonInfo {
|
|
|
- serverIp: string,
|
|
|
- params?: any
|
|
|
- }
|
|
|
const commonInfo: commonInfo = {
|
|
|
serverIp,
|
|
|
+ time: b - a
|
|
|
};
|
|
|
if (ctx.request.body) {
|
|
|
commonInfo.params = ctx.request.body;
|
|
|
};
|
|
|
- // logger.info(output(ctx, null, commonInfo))
|
|
|
+ logger.info(output(ctx, null, commonInfo))
|
|
|
}
|
|
|
else {
|
|
|
ctx.throw(ctx.status, ctx.response);
|