String::Snip - Shorten large substrings in your strings
use String::Snip; my $large_string = 'blabla , somethingelse=AStringThatIsMuchTooLongToBeDisplayedInALogEntryORAnywhereSensible'; print String::Snip::snip($large_string); # Prints 'blabla, somethingelse=AStringThat ..[SNIP 2345chars].. whereSensible'
Pure function. Returns the given string with its long substrings shortened.
Usage:
print String::Snip::snip($large_string); print String::Snip::snip($large_string, { max_length => 1000 });
Options:
Maximum length of a substring. After this length, it gets truncated to a string of short_length characters. Default to 2000. There is a hard bottom limit at 100.
short_length
Length of shortened substrings. Defaults to 100. There is a hard bottom limit at 50.
The regex that captures the substring of this string. Defaults to \S+, which will capture any consecutive non-space strings. If you want this to capture multiline large strings, you can use [\\S\\n]+.
\S+
[\\S\\n]+
Use that in your log outputs, reserving the full output when your log is in debug mode:
if( $something_is_debug ){ $log->debug("Full string:".$large_string); } $log->info(String::Snip::snip($large_string));
For instance if you deal with base64 encoded binary files, it's likely you will have your log filled with meaningless base64 giant strings. Use this to shorten them!
If you use that on structured data (like a JSON structure), this might render your data invalid. For instance if you have a large base64 string in your JSON, it will be broken by this. To avoid this being an issue, make sure you have a way to output the whole untouched thing should you need it.
String::Truncate
To install String::Snip, copy and paste the appropriate command in to your terminal.
cpanm
cpanm String::Snip
CPAN shell
perl -MCPAN -e shell install String::Snip
For more information on module installation, please visit the detailed CPAN module installation guide.