chimurai / http-proxy-middleware

:zap: The one-liner node.js http-proxy middleware for connect, express, next.js and more
MIT License
10.6k stars 828 forks source link

How to reduce logger to only errors? #997

Closed elawad closed 2 months ago

elawad commented 2 months ago

Checks

Describe the bug (be clear and concise)

Before v3 we could use logLevel: 'error' to only log errors. With v3 we now use logger: console but this output lots of info logs.

What's the best way to filter only error logs, keeping HPM's log format?

Step-by-step reproduction instructions

1. ...
2. ...

Expected behavior (be clear and concise)

A way to reduce logs to only errors.

How is http-proxy-middleware used in your project?

=> Found "http-proxy-middleware@3.0.0"
info Has been hoisted to "http-proxy-middleware"
info This module exists because it's specified in "dependencies".
info Disk size without dependencies: "376KB"
info Disk size with unique dependencies: "896KB"
info Disk size with transitive dependencies: "2.21MB"
info Number of shared dependencies: 7

What http-proxy-middleware configuration are you using?

app.use('/api', createProxyMiddleware({
  target: 'https://example.com/api',
  changeOrigin: true,
  logger: console,
}));

What OS/version and node/version are you seeing the problem?

MacOS 14.4.1 and Node 20.12.0

Additional context (optional)

No response

elawad commented 2 months ago

Is the solution to use the on error event? https://github.com/chimurai/http-proxy-middleware/blob/32a5b233a1ba488083821cfd1c356deb56792d27/src/plugins/default/logger-plugin.ts#L22-L31

chimurai commented 2 months ago

You can use a logger library and set the log level there: https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/logger.md

Or create a custom logger object to only log errors:

app.use('/api', createProxyMiddleware({
  target: 'https://example.com/api',
  changeOrigin: true,
  logger: {
    info: () => {},
    warn: () => {},
    error: console.error,
  },
}));
elawad commented 2 months ago

That's exactly what I wanted, thank you!