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.
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
}
})
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.
This actually just landed in https://github.com/nodejs/node/pull/32392. I think this can be closed as "fixed." 馃槃
Most helpful comment
This actually just landed in https://github.com/nodejs/node/pull/32392. I think this can be closed as "fixed." 馃槃