New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get request body with customize token? #5

Open
fancyoung opened this Issue Nov 11, 2016 · 6 comments

Comments

Projects
None yet
2 participants
@fancyoung

fancyoung commented Nov 11, 2016

The code support custom_tokens, but if the replacement is something like ctx.request.body,
I can't pass the option just like:

app.use(log4js.koaLogger(log4js.getLogger("http"), { level: 'auto',
    tokens: [
        { token: ':body', replacement: ctx.request.body}
    ],
    format: ':remote-addr - -' +
    ' ":method :url HTTP/:http-version"' +
    ' :status :content-length ":referrer"' +
    ' ":user-agent"' +
    ' :body'
}))

because ctx is not defined here.

I can use a wrapper, but is there any simple way to solve this problem?

@dominhhai

This comment has been minimized.

Show comment
Hide comment
@dominhhai

dominhhai Nov 15, 2016

Owner

You can pass the replacement as a replace function.

tokens: [
  { token: ':body', replacement: function() { return ctx.request.body } }
]
Owner

dominhhai commented Nov 15, 2016

You can pass the replacement as a replace function.

tokens: [
  { token: ':body', replacement: function() { return ctx.request.body } }
]
@fancyoung

This comment has been minimized.

Show comment
Hide comment
@fancyoung

fancyoung Nov 21, 2016

But it seems can't use as Koa-middleware way,
such as

app.use(log4js.koaLogger(log4js.getLogger('http'), { level: 'auto', tokens: [
  { token: ':body', replacement: function() { return ctx.request.body } }
] }))

because ctx is not defined here.

fancyoung commented Nov 21, 2016

But it seems can't use as Koa-middleware way,
such as

app.use(log4js.koaLogger(log4js.getLogger('http'), { level: 'auto', tokens: [
  { token: ':body', replacement: function() { return ctx.request.body } }
] }))

because ctx is not defined here.

@dominhhai

This comment has been minimized.

Show comment
Hide comment
@dominhhai

dominhhai Nov 24, 2016

Owner

I have just added a new syntax to allow replacing token with ctx.

tokens: [
  { token: ':body', content: function(ctx) { return ctx.request.body } }
]

You can upgrade your package to v1.1.0 (for Koa v1) or v2.1.0 (for Koa v2).

Owner

dominhhai commented Nov 24, 2016

I have just added a new syntax to allow replacing token with ctx.

tokens: [
  { token: ':body', content: function(ctx) { return ctx.request.body } }
]

You can upgrade your package to v1.1.0 (for Koa v1) or v2.1.0 (for Koa v2).

@fancyoung

This comment has been minimized.

Show comment
Hide comment
@fancyoung

fancyoung Nov 28, 2016

Wonderful.

Now I use { token: ':body', content: function(ctx) { return JSON.stringify(ctx.request.body) } } log the body'

fancyoung commented Nov 28, 2016

Wonderful.

Now I use { token: ':body', content: function(ctx) { return JSON.stringify(ctx.request.body) } } log the body'

@fancyoung fancyoung closed this Nov 28, 2016

@fancyoung

This comment has been minimized.

Show comment
Hide comment
@fancyoung

fancyoung Dec 4, 2016

It not work, it save the first time request param, and return it everytime

fancyoung commented Dec 4, 2016

It not work, it save the first time request param, and return it everytime

@fancyoung fancyoung reopened this Dec 4, 2016

@dominhhai

This comment has been minimized.

Show comment
Hide comment
@dominhhai

dominhhai Apr 20, 2017

Owner

Which should be fix by this #8 PR.

Owner

dominhhai commented Apr 20, 2017

Which should be fix by this #8 PR.

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