Skip to content

Commit

Permalink
feat: gateway request log
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Oct 29, 2019
1 parent 587bd95 commit 891168b
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/gateway/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,19 @@ export const createHttpServer = () => {
});

app.use(async (ctx, next) => {
const {
url,
method,
} = ctx;
const clientIP = ctx.get('x-real-ip') || '-';

try {
await next();

console.log('[request] [%s] %s %s %s "%s"', clientIP, method, ctx.status, url, ctx.get('user-agent') || '-');
} catch (err) {
console.log('[request] [%s] %s %s %s "%s"', clientIP, method, ctx.status, url, ctx.get('user-agent') || '-');

ctx.status = err.status || 500;
ctx.body = `
<h1>Error</h1>
Expand Down

0 comments on commit 891168b

Please sign in to comment.