[Feature Request] Add option to change express middleware log function
See original GitHub issueIs 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:
- Created 2 years ago
- Comments:8 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I agree that this might as well be a feature.
As I see it, there are 4 distinct ways of implementing this:
app.use(expressMiddleware(console.info))
Pro: Clean, No API bloat Con: Breaking changeapp.use(expressMiddleware(console, {}, console.info))
Pro: None breaking Con: API bloat, First argument wont be used at allapp.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.app.use(expressMiddleware(console))
andapp.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 bloatI’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:2Relevant code:
https://github.com/Olian04/better-logging/blob/576a9f7a27cc68665b74ba3f72818462c7f23c02/src/express/index.ts#L7-L77
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))
andapp.use(expressMiddleware(console.info))
would be valid. However bear in mind thatapp.use(expressMiddleware((...args) => console.info(...args)))
would not be valid, this was done in order to not break #66