question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature Request] Add option to change express middleware log function

See original GitHub issue

Is your feature request related to a problem? Please describe.

Currently, the Express logging middleware will output all logs as info, however this isn’t always wanted.

Describe the solution you’d like

Add a way to specify which log function to use. A third parameter in third place would be a possibility that avoids breaking changes; adding it as second parameter would be nicer though (no param behind a long and optional object).

Describe alternatives you’ve considered

Sure, I could write my own middleware. But what’s the point in shipping it with the package then ^^

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Olian04commented, Nov 9, 2021

I agree that this might as well be a feature.

As I see it, there are 4 distinct ways of implementing this:

  1. Change the first argument of the middleware from being the entire host obj, to just being the log method. app.use(expressMiddleware(console.info)) Pro: Clean, No API bloat Con: Breaking change
  2. Add another argument to the function signature. app.use(expressMiddleware(console, {}, console.info)) Pro: None breaking Con: API bloat, First argument wont be used at all
  3. Reuse config object, and add another property to it. app.use(expressMiddleware(console, { meta: { method: 'info' } }) Pro: None breaking Con: Might cause confusion since the config object has so far been used to configure WHAT is logged, not HOW it’s logged.
  4. Overload the first argument of the middleware. Allowing the user to either provide the entire host obj, or provide just the log method. app.use(expressMiddleware(console)) and app.use(expressMiddleware(console.info)) would both be valid Pro: Clean, Low API bloat, Allows for a clean deprecation of “providing a host obj” between major versions Con: Implementation bloat

I’m leaning towards nr:2 for the time being, but then include nr:1 in the next major release. I’m leaning towards nr:4, since it’s a merge between nr:1 & nr:2

Relevant code:

https://github.com/Olian04/better-logging/blob/576a9f7a27cc68665b74ba3f72818462c7f23c02/src/express/index.ts#L7-L77

0reactions
Olian04commented, May 25, 2022

This has been implemented in the latest release.

I decided to go with the overload approach. Overloading the first argument of the middleware. Allowing the user to either provide the entire host object, or provide just the specific log method. This means that both app.use(expressMiddleware(console)) and app.use(expressMiddleware(console.info)) would be valid. However bear in mind that app.use(expressMiddleware((...args) => console.info(...args))) would not be valid, this was done in order to not break #66

Read more comments on GitHub >

github_iconTop Results From Across the Web

Writing middleware for use in Express apps
Middleware functions can perform the following tasks: Execute any code. Make changes to the request and the response objects. End the request-response cycle....
Read more >
Complete Guide to Express Middleware - Reflectoring
Middleware functions take three arguments: the request object ... We call app.use() to add a middleware function to our Express application.
Read more >
Express middleware: A complete guide - LogRocket Blog
In this guide, you can explore the basics of using Express.js middleware and learn how to add middleware to a simple Express API....
Read more >
How To Create a Custom Middleware in Express.js
Learn how to write and use your own custom Express.js middleware by using res, req, and next.
Read more >
Modify request objects within middleware function #31188
I found a workaround, but not at all satisfying: Add the required data to the headers in the middleware, then when in your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found