yidinghan / koa2-winston

koa2 version winston logger like express-winston
MIT License
38 stars 5 forks source link

Feature request: support logging meta data #16

Open mpvosseller opened 4 years ago

mpvosseller commented 4 years ago

I'd like to log additional information such as ctx.state.user.id. Is this currently possible?

In express-winston I could do this by passing a dynamicMeta option which takes a function and returns the extra meta data to be logged dynamically (at log time). That could work here too I think.

Another option here might be to treat the koa context much like req and res. So add options like ctxKeys, ctxSelect, and ctxUnselect to allow the user to choose which context properties are logged.

Would you accept a PR for one of these approaches?

mnebuerquo commented 3 years ago

I also would like to see this happen. My preference would be for a dynamicMeta function option. It is more powerful than an array of keys, and the array of keys is too convenient for me. We should all be careful with what we log from a koa context, and writing a function to do it makes it more deliberate.

The dynamicMeta function should get the context as its argument and should be called at the very end of the chain, just before the log message is written, in case properties get added to the context by other middleware.