|
@@ -20,14 +20,18 @@ export interface InterfaceLog {
|
|
|
mark: InterfaceLogFn
|
|
|
}
|
|
|
interface InterfaceLogFn {
|
|
|
- (message: string): void;
|
|
|
+ (message: string, options?: { [key: string]: string|number }): void;
|
|
|
}
|
|
|
interface InterfaceAppenders {
|
|
|
[key: string]: DateFileAppender | ConsoleAppender
|
|
|
}
|
|
|
|
|
|
// 输出格式化配置
|
|
|
-export const formatOutputHandle = (ctx: Context, message: string | null | undefined , commonInfo: InterfaceLogInfo): string => {
|
|
|
+export const formatOutputHandle = (
|
|
|
+ ctx: Context,
|
|
|
+ message: string | null | undefined ,
|
|
|
+ commonInfo: InterfaceLogInfo
|
|
|
+): string => {
|
|
|
const {
|
|
|
method, // 请求方式
|
|
|
headers, // 请求headers
|
|
@@ -39,24 +43,27 @@ export const formatOutputHandle = (ctx: Context, message: string | null | undefi
|
|
|
...newCommonInfo
|
|
|
} = commonInfo;
|
|
|
interface client {
|
|
|
- originalUrl: string,
|
|
|
- userAgent: string,
|
|
|
+ 'content-type'?: string
|
|
|
query?: string
|
|
|
}
|
|
|
- const client: client = {
|
|
|
- originalUrl,
|
|
|
- userAgent: headers['user-agent']
|
|
|
- };
|
|
|
+ const client: client = {};
|
|
|
+ if (headers['content-type']) {
|
|
|
+ client['content-type'] = headers['content-type'];
|
|
|
+ }
|
|
|
if (ctx.querystring) {
|
|
|
client.query = ctx.querystring;
|
|
|
}
|
|
|
let text = `[${method}] ${ctx.status} [${origin}${originalUrl}] ${time ? time + 'ms' : ''}
|
|
|
req: ${JSON.stringify(Object.assign(client, newCommonInfo))}`;
|
|
|
-
|
|
|
- if (['[object Object]', '[object Array]', '[object Error]'].includes(Object.prototype.toString.call(message))) {
|
|
|
+ if ([
|
|
|
+ '[object Object]',
|
|
|
+ '[object Array]'
|
|
|
+ ].includes(Object.prototype.toString.call(message))) {
|
|
|
message = JSON.stringify(message);
|
|
|
}
|
|
|
-
|
|
|
+ if (Object.prototype.toString.call(message) === '[object Error]') {
|
|
|
+ message = message?.toString();
|
|
|
+ }
|
|
|
if (message) {
|
|
|
text += '\n' + String(message);
|
|
|
}
|
|
@@ -166,10 +173,33 @@ export default class MyLogger {
|
|
|
mark: () => {}
|
|
|
};
|
|
|
methods.map(el => {
|
|
|
- $log[el] = (message) => {
|
|
|
- this.logger[el](formatOutputHandle(ctx, message, { ip: ctx.ip }));
|
|
|
+ $log[el] = (message, options?: { [key: string]: string }) => {
|
|
|
+ this.logger[el](formatOutputHandle(ctx, message, Object.assign({ ip: ctx.ip }, options)));
|
|
|
};
|
|
|
});
|
|
|
return $log;
|
|
|
}
|
|
|
+ formatLog(
|
|
|
+ this: MyLogger,
|
|
|
+ type: 'trace'|'debug'|'info'|'warn'|'error'|'fatal'|'mark',
|
|
|
+ message: string ,
|
|
|
+ ): void
|
|
|
+ formatLog(
|
|
|
+ this: MyLogger,
|
|
|
+ type: 'trace'|'debug'|'info'|'warn'|'error'|'fatal'|'mark',
|
|
|
+ message: string ,
|
|
|
+ ctx?: Context,
|
|
|
+ options?: { [key: string]: string|number }): void
|
|
|
+ formatLog(
|
|
|
+ this: MyLogger,
|
|
|
+ type: 'trace'|'debug'|'info'|'warn'|'error'|'fatal'|'mark',
|
|
|
+ message: string ,
|
|
|
+ ctx?: Context,
|
|
|
+ options?: { [key: string]: string|number }): void {
|
|
|
+ if (ctx) {
|
|
|
+ this.logger[type](formatOutputHandle(ctx, message, Object.assign({ ip: ctx.ip }, options)));
|
|
|
+ } else {
|
|
|
+ this.logger[type](message);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|