Comments (5)
@zianwar -- You can start with the canned middleware: https://github.com/FormidableLabs/express-winston-middleware/blob/master/examples/server.js#L22-L25 and then augment custom middleware like in https://github.com/FormidableLabs/express-winston-middleware/blob/master/examples/server.js#L32-L37 to add extra metadata.
So something like
// Middleware automatically adds request logging at finish of request.
app.use(winMid.request(logOpts, {
type: "per-request-log"
}));
// Also inject extra metadata into the log object.
app.use(function (req, res, next) {
// Add anything you want right here!!!
res.locals._log.addMeta({
// Req object already exists. This object will get deep-merged
req: {
body: req.body
}
});
next();
});
The above pattern of later addMeta
calls works because the actual request log event isn't issued until the response is actually sent. So you can repeatedly addMeta()
in any handler!
Also, I should note that res.locals._log
in this context is a per request instantiated log object. So any metadata added in addMeta
calls will get logged out for any other manual calls like res.locals._log.info()
in your handlers.
from express-winston-middleware.
thank you @ryan-roemer this worked as expected!
I still have 2 questions,
- How can I log the response body?
- Is it possible to remove some meta from the request? maybe some function like
removeMeta
?
from express-winston-middleware.
Hi @zianwar
-
In my example you'll note you have access to the
res
object. You can add anything you like from that too. -
For removing meta, there isn't something straight up.
a. But if you look at the source of
addMeta
(https://github.com/FormidableLabs/express-winston-middleware/blob/master/index.js#L298-L310) you'll note there is a_.merge()
of the new object you provide. This means if you pass in something like{ req: null }
it should should nuke that entire field. This isn't a complete solution obviously as you may want just some of an existing field. But it works right now...
b. or you could directly access the private member variableres.locals._log._meta
which may break in the future, but works in a pinch now.
c. I would accept a PR for this lib with something liketransformMeta(function (oldMeta) { return newMeta; })
(or maybe an async version.
Hope that helps.
from express-winston-middleware.
Hey @ryan-roemer, thank you for this detailed reply.
Although I've managed to edit meta, I'd like to improve the library by submitting a PR as you said in (c).
What do you think of something simple like this?
Log.prototype.transformMeta = function (transformer) {
this._meta = transformer(this._meta);
return this;
};
from express-winston-middleware.
@zianwar -- Looks good! Let's name the transform function fn
per our collection of project usual conventions.
Thanks!
from express-winston-middleware.
Related Issues (8)
- Add unit tests.
- Problems trying to add additional request headers HOT 2
- Point homepage at http://stack.formidable.com/express-winston-middleware/ HOT 1
- wish: actually show the middleware in action HOT 2
- BUG: Request logger instances hit maxListeners when highly concurrent. HOT 12
- Problems with the example code: app.use(logging.createMiddle(requestLogger({ HOT 1
- 404 crashes with a simple express app HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from express-winston-middleware.