Skip to content

Pass ctx to transporter#83

Open
roark wants to merge 1 commit into
koajs:masterfrom
roark:master
Open

Pass ctx to transporter#83
roark wants to merge 1 commit into
koajs:masterfrom
roark:master

Conversation

@roark
Copy link
Copy Markdown

@roark roark commented Jan 5, 2021

Enables more verbose Transporter to log other ctx details. Fixes issues #77 and #82.

ctx is passed as the 3rd argument to transporter as to not disrupt any existing code. That said, it could easily be the first argument if desired.

app.use(logger((str, args, ctx) => {
  console.log(str, ctx.request.ip); // ctx.request.header['user-agent'], etc...
}))

Copy link
Copy Markdown
Member

@3imed-jaberi 3imed-jaberi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sgtm

@s4kr4
Copy link
Copy Markdown

s4kr4 commented Jul 22, 2021

When does it ship?

@libook
Copy link
Copy Markdown

libook commented Oct 21, 2021

Anyone who waiting for this feature may do this temporarily:

import koaLogger from "koa-logger";

app.use(async (ctx, next) => {
    const koaLoggerInstance = koaLogger(logString => {
        // Do anything with ctx
    });
    await koaLoggerInstance(ctx, next);
});

@jalada
Copy link
Copy Markdown

jalada commented Jun 12, 2025

Anyone who waiting for this feature may do this temporarily:

import koaLogger from "koa-logger";

app.use(async (ctx, next) => {
    const koaLoggerInstance = koaLogger(logString => {
        // Do anything with ctx
    });
    await koaLoggerInstance(ctx, next);
});

Doing this instantiates a brand new instance of koa-logger for every request. This might not be a problem for your use case, but could have memory & performance implications in high-throughput scenarios.

@3imed-jaberi
Copy link
Copy Markdown
Member

Thanks @roark for your contribution, could you please add test cases here!

@mahnunchik
Copy link
Copy Markdown

@3imed-jaberi could it be merged? It is a very useful improvement!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

is there a way to log user agent? Is there a reason not to pass ctx on Custom Transporter?

6 participants