|
@@ -1,14 +1,14 @@
|
|
|
-import log4js from 'log4js';
|
|
|
+import log4js, { DateFileAppender, ConsoleAppender } from 'log4js';
|
|
|
import path from 'path';
|
|
|
import { Context } from 'koa';
|
|
|
import config from '@config';
|
|
|
-interface commonInfo {
|
|
|
- serverIp: string,
|
|
|
+export interface commonInfo {
|
|
|
+ ip: string,
|
|
|
params?: any,
|
|
|
time?: number
|
|
|
}
|
|
|
// 输出配置
|
|
|
-const output = (ctx, message: string | null | undefined , commonInfo: commonInfo) => {
|
|
|
+export const output = (ctx, message: string | null | undefined , commonInfo: commonInfo) => {
|
|
|
const {
|
|
|
method, // 请求方式
|
|
|
headers, // 请求headers
|
|
@@ -31,7 +31,7 @@ const output = (ctx, message: string | null | undefined , commonInfo: commonInfo
|
|
|
if (ctx.querystring) {
|
|
|
client.query = ctx.querystring;
|
|
|
};
|
|
|
- let text = `[${method}] ${ctx.status} [${origin}${originalUrl}] ${time}ms
|
|
|
+ let text = `[${method}] ${ctx.status} [${origin}${originalUrl}] ${time ? time + 'ms' : ''}
|
|
|
req: ${JSON.stringify(Object.assign(client, newCommonInfo))}`;
|
|
|
|
|
|
if (Object.prototype.toString.call(message) === '[object Object]'
|
|
@@ -51,28 +51,30 @@ const methods: string[] = ['trace', 'debug', 'info', 'warn', 'error', 'fatal', '
|
|
|
const defaultConfig = {
|
|
|
logLevel: 'debug', // 日志级别
|
|
|
dir: path.resolve(__dirname, '../log'), // 指定日志存放目录名
|
|
|
- env: 'development', // 指定当前环境,当开发时控制台也输出
|
|
|
- serverIp: '127.0.0.1'
|
|
|
+ env: 'development' // 指定当前环境,当开发时控制台也输出
|
|
|
};
|
|
|
|
|
|
// log配置
|
|
|
const opt = Object.assign({}, defaultConfig, config.logs);
|
|
|
-const { env, appLogLevel, dir, serverIp } = opt;
|
|
|
+const { env, appLogLevel, dir } = opt;
|
|
|
/**
|
|
|
* 记录日志的方式
|
|
|
* 指定要记录的日志分类 log
|
|
|
* 展示方式为文件类型 dateFile
|
|
|
* 日志输出的文件名 s-yyyy-MM-dd.log
|
|
|
*/
|
|
|
+// interface appenders {
|
|
|
+// [key: string]: {
|
|
|
+// type: string,
|
|
|
+// filename?: string,
|
|
|
+// pattern?: string,
|
|
|
+// alwaysIncludePattern?: boolean,
|
|
|
+// ip?: string,
|
|
|
+// layout: object
|
|
|
+// }
|
|
|
+// }
|
|
|
interface appenders {
|
|
|
- [log: string]: {
|
|
|
- type: string,
|
|
|
- filename?: string,
|
|
|
- pattern?: string,
|
|
|
- alwaysIncludePattern?: boolean,
|
|
|
- serverIp?: string,
|
|
|
- layout: object
|
|
|
- }
|
|
|
+ [key: string]: DateFileAppender | ConsoleAppender
|
|
|
}
|
|
|
const appenders: appenders = {
|
|
|
log: {
|
|
@@ -82,7 +84,6 @@ const appenders: appenders = {
|
|
|
filename: `${dir}/z`,
|
|
|
pattern: 'yyyy-MM-dd.log',
|
|
|
alwaysIncludePattern: true,
|
|
|
- serverIp,
|
|
|
// 日志输出格式
|
|
|
layout: {
|
|
|
type: 'pattern',
|
|
@@ -127,7 +128,7 @@ const logger = log4js.getLogger('log');
|
|
|
interface logFn {
|
|
|
(message: any, ...args: any[]): void;
|
|
|
}
|
|
|
-interface interfaceLog {
|
|
|
+export interface InterfaceLog {
|
|
|
trace: logFn
|
|
|
debug: logFn
|
|
|
info: logFn
|
|
@@ -137,8 +138,12 @@ interface interfaceLog {
|
|
|
mark: logFn
|
|
|
}
|
|
|
|
|
|
-export const loggerInstance = (ctx: Context): interfaceLog => {
|
|
|
- const $log: interfaceLog = {
|
|
|
+export interface InterfaceLogger {
|
|
|
+ (ctx: Context): InterfaceLog
|
|
|
+}
|
|
|
+
|
|
|
+export const loggerInstance: InterfaceLogger = (ctx: Context): InterfaceLog => {
|
|
|
+ const $log: InterfaceLog = {
|
|
|
trace: () => {},
|
|
|
debug: () => {},
|
|
|
info: () => {},
|
|
@@ -149,38 +154,8 @@ export const loggerInstance = (ctx: Context): interfaceLog => {
|
|
|
};
|
|
|
methods.map(el => {
|
|
|
$log[el] = (message) => {
|
|
|
- logger[el](output(ctx, message, { serverIp }));
|
|
|
+ logger[el](output(ctx, message, { ip: ctx.ip }));
|
|
|
};
|
|
|
});
|
|
|
return $log;
|
|
|
}
|
|
|
-
|
|
|
-export const middlewareLog = () => {
|
|
|
- // koa上下文存放的log输出方法
|
|
|
- const contextLogger = {};
|
|
|
-
|
|
|
- return async(ctx, next) => {
|
|
|
- methods.map(el => {
|
|
|
- contextLogger[el] = (message) => {
|
|
|
- logger[el](output(ctx, message, { serverIp }));
|
|
|
- };
|
|
|
- });
|
|
|
- ctx.$log = contextLogger;
|
|
|
- const a = Date.now()
|
|
|
- await next();
|
|
|
- const b = Date.now()
|
|
|
- if (ctx.response && ctx.status < 400) {
|
|
|
- const commonInfo: commonInfo = {
|
|
|
- serverIp,
|
|
|
- time: b - a
|
|
|
- };
|
|
|
- if (ctx.request.body) {
|
|
|
- commonInfo.params = ctx.request.body;
|
|
|
- };
|
|
|
- logger.info(output(ctx, null, commonInfo))
|
|
|
- }
|
|
|
- else {
|
|
|
- ctx.throw(ctx.status, ctx.response);
|
|
|
- }
|
|
|
- }
|
|
|
-}
|