I have a logger object that formats log messages with a timestamp, sender, and formats lines to a maximum of 80 characters in length.
var paddedSender = message.Sender.padStart( 6 );
var string = `=-[ ${message.Timestamp} ][ ${paddedSender} ] `;
var remainingLength = this.CONSOLE_MAX_LENGTH - string.length - 3;
var lines = [];
var numLines = Math.ceil( message.Body.length / remainingLength );
for ( var i = 0, o = 0; i < numLines; i++ , o += remainingLength )
lines.push( message.Body.substr( o, remainingLength ) );
string += lines.shift().padEnd( remainingLength ) + ' -=';
while ( lines.length > 0 )
string += '\n'
+ '=-'
+ '-'.repeat( message.Timestamp.length + 4 )
+ '-'.repeat( paddedSender.length + 4 )
+ ` ${lines.shift().padEnd( remainingLength )} `
+ '-=';
return string;
This results an output similar to the following:
================================================================================
=-[ 06/08 11:30:36 ][ SYSTEM ] Initializing... -=
=-[ 06/08 11:30:36 ][ SYSTEM ] Logger Initialized -=
=-[ 06/08 11:30:37 ][ SYSTEM ] WebService Initialized -=
=-[ 06/08 11:30:38 ][ WEBCLI ] API Returned 400 Bad Request(Endpoint: undefin -=
=----------------------------- ed) (Err: Invalid id) -=
So far it acts as intended. However, I would like to have it word wrap by starting new lines between spaces instead of when the character limit is hit.
For instance, I would like this message:
=-[ 06/08 11:30:38 ][ WEBCLI ] API Returned 400 Bad Request(Endpoint: undefin -=
=----------------------------- ed) (Err: Invalid id) -=
To be formatted like so:
=-[ 06/08 11:30:38 ][ WEBCLI ] API Returned 400 Bad Request(Endpoint: -=
=----------------------------- undefined) (Err: Invalid id) -=
How can I achieve this?