Skip to content

fix: handle invalid logger in getLogger during runtime#1100

Closed
Mallikarjun-0 wants to merge 1 commit intochimurai:masterfrom
Mallikarjun-0:validate-logger
Closed

fix: handle invalid logger in getLogger during runtime#1100
Mallikarjun-0 wants to merge 1 commit intochimurai:masterfrom
Mallikarjun-0:validate-logger

Conversation

@Mallikarjun-0
Copy link
Copy Markdown

Description

This PR adds runtime validation for the logger option passed to getLogger. If the logger does not implement the required logging methods, the function now logs a warning and falls back to the noopLogger. It can be changed to throw an error, which might lead to a "Breaking change".

Motivation and Context

  • The current implementation of getLogger assumes that if a logger is passed, it always matches the expected Logger interface unless the logger option is undefined or its value is null where the original code return (options.logger as Logger) || noopLogger; returns noopLogger.
  • This can lead to subtle bugs or runtime errors if the logger is a number, string, or any other object
    Example:
const logger = getLogger({ logger: 500 });
console.log(logger)     // prints 500

How has this been tested?

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

@chimurai chimurai mentioned this pull request Apr 16, 2025
3 tasks
@chimurai
Copy link
Copy Markdown
Owner

Thanks for the PR @Mallikarjun-0

The real issue is caused by a bug in the types, resulting in Type any:

logger?: Logger | any;

Created a PR to fix the Logger type: https://github.com/chimurai/http-proxy-middleware/pull/1104/files

You should get a TypeScript error now when you provide an invalid logger

Could you verify if this fix works for you? You can try the fix with:

npm i https://pkg.pr.new/http-proxy-middleware@1104

^ this preview is published from: #1104 (comment)

@Mallikarjun-0
Copy link
Copy Markdown
Author

@chimurai, yeah the fix in the #1104 works.

@chimurai
Copy link
Copy Markdown
Owner

Thanks for raising the issue 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants