Skip to content

Commit 1659b66

Browse files
committed
chore: update logger
1 parent 2c74281 commit 1659b66

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

examples/server.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ const proxyTable = {
1212
'/octocat': 'https://api.github.com/users',
1313
'/api': {
1414
target: 'http://localhost:8111',
15+
logs: true,
1516
headers: {
1617
'X_HOST_S': 'google.com'
1718
}
1819
},
1920
'/tenant/:id': params => {
20-
console.log(params)
2121
return {
2222
target: 'http://localhost:8111',
23+
logs: true,
2324
headers: {
2425
'X_HOST_S': 'google.com'
2526
}
@@ -48,9 +49,9 @@ const proxyTable = {
4849
const app = new Koa()
4950

5051
app.use(async (ctx, next) => {
51-
console.log('-----------------')
52-
console.log(ctx.request.headers)
53-
console.log('-----------------')
52+
// console.log('-----------------')
53+
// console.log(ctx.request.headers)
54+
// console.log('-----------------')
5455
await next()
5556
})
5657

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ module.exports = (context, options) => (ctx, next) => {
3434
const { logs, rewrite, events } = opts
3535

3636
return new Promise((resolve, reject) => {
37-
if (logs) logger(ctx)
37+
ctx.req.oldPath = ctx.req.url
3838

3939
if (typeof rewrite === 'function') {
4040
ctx.req.url = rewrite(ctx.req.url)
4141
}
4242

43+
if (logs) logger(ctx)
44+
4345
if (events && typeof events === 'object') {
4446
Object.entries(events).forEach(([event, handler]) => {
4547
proxy.on(event, handler)
@@ -62,5 +64,5 @@ module.exports = (context, options) => (ctx, next) => {
6264
}
6365

6466
function logger (ctx) {
65-
console.log('%s - %s %s', new Date().toISOString(), ctx.req.method, ctx.req.url)
67+
console.log('%s - %s %s proxy to -> %s', new Date().toISOString(), ctx.req.method, ctx.req.oldPath, ctx.req.url)
6668
}

0 commit comments

Comments
 (0)