Giter VIP home page Giter VIP logo

Comments (5)

ryan-roemer avatar ryan-roemer commented on June 3, 2024

@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.

zianwar avatar zianwar commented on June 3, 2024

thank you @ryan-roemer this worked as expected!
I still have 2 questions,

  1. How can I log the response body?
  2. Is it possible to remove some meta from the request? maybe some function like removeMeta ?

from express-winston-middleware.

ryan-roemer avatar ryan-roemer commented on June 3, 2024

Hi @zianwar

  1. In my example you'll note you have access to the res object. You can add anything you like from that too.

  2. 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 variable res.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 like transformMeta(function (oldMeta) { return newMeta; }) (or maybe an async version.

Hope that helps.

from express-winston-middleware.

zianwar avatar zianwar commented on June 3, 2024

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.

ryan-roemer avatar ryan-roemer commented on June 3, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.