msw icon indicating copy to clipboard operation
msw copied to clipboard

Predicate for Logs

Open lukesmurray opened this issue 5 years ago • 4 comments

First off thank you for the excellent library. It is amazingly helpful for mocking endpoints.

I have certain requests which are fired over and over again (for example an autosave endpoint, or polling the server for some information). I find logging very useful but I would like a way to filter out some of these more common requests. I saw the implementation of quiet in https://github.com/mswjs/msw/issues/111. Would it be possible to extend that concept to use a function which returns True for requests which should be logged and False for requests which should not be logged? The alternative I can think of is to override console.log which is probably less desirable.

lukesmurray avatar Oct 09 '20 22:10 lukesmurray

Hey, @lukesmurray. Thank you for the feedback!

I see no harm in us extending the quiet option to support a predicate function as a value.

worker.start({
  quiet(req, res) {
    // Conditionally suppress logs for certain requests.
    return req.url.href.includes('/foo')
  }
})

Would this suit your usage? If so, are you interested in making a pull request with these changes? I'll support you through the process to make sure we land an awesome extension.

kettanaito avatar Oct 10 '20 10:10 kettanaito

Can pick this up if @lukesmurray isn't. :)

balavishnuvj avatar Oct 12 '20 09:10 balavishnuvj

That is exactly what I imagined. Happy to let @balavishnuvj take this one.

lukesmurray avatar Oct 12 '20 11:10 lukesmurray