Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow using non-lowercased custom levels #342

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function pinoLogger (opts, stream) {

function getValidLogLevel (level, defaultValue = 'info') {
if (level && typeof level === 'string') {
const logLevel = level.trim().toLowerCase()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose here is to compare names in a consistent manner.

Copy link
Contributor Author

@chernodub chernodub Jul 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jsumners, thanks for the response!

My reasoning for the change:

  1. Before the change, validLogLevels contained values that are not mapped to lowercased values, so the check would always fail for non-lowercased values. (like infoCustom example above).
  2. We could potentially prepare the validLogLevels by mapping each value to lowercase, so the check will result to true. After that, the getValidLogLevel will return a lowercased value, and the code of pino-http will try calling something like logger.infocustom which will also throw (because the valid call would be logger.infoCustom)

Pls let me know what you think, thanks

const logLevel = level.trim()
if (validLogLevels.includes(logLevel) === true) {
return logLevel
}
Expand Down
6 changes: 3 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ test('uses the log level passed in as an option, where the level is a custom one
const logger = pinoHttp(
{
customLevels: {
custom: 25
infoCustom: 25
},
useLevel: 'custom',
level: 'custom'
useLevel: 'infoCustom',
level: 'infoCustom'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If needed, I could duplicate the test for the lowercase scenario too. I'm assuming here that both cases are covered by this test now.

},
dest
)
Expand Down
Loading