1

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?

1 Answer 1

1

You could take the text and take a wanted size and check if the position of the end of the part string is an space, then split the string and push it to an array with an adjustment for the length.

var string = 'API Returned 400 Bad Request(Endpoint: undefined) (Err: Invalid id) Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
    size = 46,
    parts = [],
    start = 0,
    end;

while (start <= string.length) {
    end = start + size;
    if (end < string.length) while (string[end] !== ' ') --end;
    parts.push(string.slice(start, end).padEnd(size, ' '));
    start = end + 1;
}

console.log(parts);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.