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

Add maxStringLength option for util.inspect #27042

Closed
noam3127 opened this issue Apr 1, 2019 · 4 comments
Closed

Add maxStringLength option for util.inspect #27042

noam3127 opened this issue Apr 1, 2019 · 4 comments
Labels
feature request Issues that request new features to be added to Node.js. util Issues and PRs related to the built-in util module.

Comments

@noam3127
Copy link

noam3127 commented Apr 1, 2019

Problem
When using util.inspect(), it would be nice to have the ability to truncate string values over a certain length wherever they exist in the object under inspection. This would allow us to avoid logging out entire strings in situations where they can be potentially huge and they add too much space in the logs.

Proposed solution
Add a maxStringLength option to util.inspect, similar to maxArrayLength option that currently exists.

@BridgeAR BridgeAR added util Issues and PRs related to the built-in util module. feature request Issues that request new features to be added to Node.js. labels Apr 1, 2019
@BridgeAR
Copy link
Member

BridgeAR commented Apr 1, 2019

Adding another option specifically for this use case does not seem ideal to me.

What could be used instead is the stylize function which receives the formatted string including the type it corresponds to. This is currently not documented and will only work if colors are set to false, so there's no support for it right now and it could change at any point of time. I am considering to either making this API public or to add an alternative that works similar but would be even more powerful (more likely).

util.inspect('a '.repeat(1e5), {
  stylize(str, type) {
    if (type === 'string' && str.length > 1e3) {
      return str.slice(0, 1e3)
    }
    return str
  }
})

@ghost
Copy link

ghost commented Apr 10, 2020

How is this not an important use case? E.g. I have property values that encode pictures in base64 and are thus multi kb long. Calling util.inspect on such an object is useless. The same rationale which prompted addition of maxArrayLength user option perfectly applies to long strings (or Buffers) as well. It appears to me that maxStringLength will serve the very purpose of util.inspect and the latter is half-useful without it.

@cjihrig
Copy link
Contributor

cjihrig commented Apr 10, 2020

This actually just landed in #32392. I think this can be closed as "fixed." 😄

@cjihrig cjihrig closed this as completed Apr 10, 2020
@konser80
Copy link

konser80 commented Nov 30, 2023

maxStringLength works great on strings.

But whats about objects?

console.log(util.inspect({ orderid: 123, paid: true }, { maxStringLength: 4 }));

this does nothing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

No branches or pull requests

4 participants