Skip to content

Commit 4f5576a

Browse files
author
pedro
committed
feat:更换原来的logger实现
1 parent 71420cd commit 4f5576a

2 files changed

Lines changed: 50 additions & 26 deletions

File tree

lib/extend.ts

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,16 @@ import {
77
FileExtensionException,
88
FileTooManyException
99
} from './exception';
10-
import consola from 'consola';
1110
import { toLine, unsets } from './util';
1211
import { config } from './config';
1312
import { get, set, cloneDeep } from 'lodash';
1413
import parse from 'co-busboy';
1514
import sendToWormhole from 'stream-wormhole';
1615
import { extname } from 'path';
1716

18-
import { Logger, FileTransport, ConsoleTransport } from 'egg-logger';
19-
20-
// const Logger = require('egg-logger').Logger;
21-
// const FileTransport = require('egg-logger').FileTransport;
22-
// const ConsoleTransport = require('egg-logger').ConsoleTransport;
17+
import { Logger } from 'egg-logger';
18+
import { FileTransport } from './logger/file';
19+
import { ConsoleTransport } from './logger/console';
2320

2421
/**
2522
* json序列化扩展
@@ -96,22 +93,32 @@ export const success = (app: Application) => {
9693
* @param app app实例
9794
*/
9895
export const logging = (app: Application) => {
99-
// TODO: 提供配置项
100-
// const logger = new Logger();
101-
// logger.set(
102-
// 'file',
103-
// new FileTransport({
104-
// file: '/path/to/file',
105-
// level: 'INFO'
106-
// })
107-
// );
108-
// logger.set(
109-
// 'console',
110-
// new ConsoleTransport({
111-
// level: 'DEBUG'
112-
// })
113-
// );
114-
app.context.logger = consola;
96+
let options = {
97+
level: 'INFO',
98+
dir: 'logs',
99+
sizeLimit: 1024 * 1024 * 5
100+
};
101+
const logConf = config.getItem('log');
102+
options = { ...options, ...logConf };
103+
104+
const logger = new Logger({});
105+
106+
logger.set(
107+
'file',
108+
new FileTransport({
109+
dir: options.dir,
110+
sizeLimit: options.sizeLimit,
111+
level: options.level
112+
})
113+
);
114+
logger.set(
115+
'console',
116+
new ConsoleTransport({
117+
level: options.level
118+
})
119+
);
120+
121+
app.context.logger = logger;
115122
};
116123

117124
export interface MulOpts {

lib/middleware.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { HttpException, NotFound, MethodNotAllowed } from './exception';
22
import { Context } from 'koa';
3+
const levels = require('egg-logger/lib/level');
4+
import { config } from './config';
35

46
/**
57
* 全局异常处理中间件
@@ -32,10 +34,25 @@ export const log = async (ctx: Context, next: () => Promise<any>) => {
3234
await next();
3335
const ms = Date.now() - start;
3436
ctx.set('X-Response-Time', `${ms}ms`);
35-
ctx.logger.info(
36-
`[${ctx.method}] -> [${ctx.url}] from: ${ctx.ip} costs: ${ms}ms`
37-
);
38-
37+
const requestLog: boolean = config.getItem('log.requestLog');
38+
const level: string = config.getItem('log.level');
39+
if (requestLog) {
40+
if (levels[level] >= levels['DEBUG']) {
41+
const data = {
42+
param: ctx.request.query,
43+
body: ctx.request.body
44+
};
45+
ctx.logger.debug(
46+
`[${ctx.method}] -> [${ctx.url}] from: ${
47+
ctx.ip
48+
} costs: ${ms}ms data:${JSON.stringify(data, null, 4)}`
49+
);
50+
} else {
51+
ctx.logger.info(
52+
`[${ctx.method}] -> [${ctx.url}] from: ${ctx.ip} costs: ${ms}ms`
53+
);
54+
}
55+
}
3956
if (ctx.status === 404) {
4057
ctx.app.emit('error', new NotFound(), ctx);
4158
} else if (ctx.status === 405) {

0 commit comments

Comments
 (0)