Skip to content

Commit

Permalink
chore(logger): improve node logger output
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Feb 13, 2018
1 parent 5e73031 commit fbb0325
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 17 deletions.
43 changes: 26 additions & 17 deletions src/sys/node/node-logger.ts
Expand Up @@ -34,7 +34,7 @@ export class NodeLogger implements d.Logger {

info(...msg: any[]) {
if (this.shouldLog('info')) {
const lines = wordWrap(msg);
const lines = wordWrap(msg, getColumns());
this.infoPrefix(lines);
console.log(lines.join('\n'));
}
Expand All @@ -56,9 +56,9 @@ export class NodeLogger implements d.Logger {

warn(...msg: any[]) {
if (this.shouldLog('warn')) {
const lines = wordWrap(msg);
const lines = wordWrap(msg, getColumns());
this.warnPrefix(lines);
console.warn(lines.join('\n'));
console.warn('\n' + lines.join('\n') + '\n');
}
this.queueWriteLog('W', msg);
}
Expand All @@ -82,9 +82,9 @@ export class NodeLogger implements d.Logger {
}

if (this.shouldLog('error')) {
const lines = wordWrap(msg);
const lines = wordWrap(msg, getColumns());
this.errorPrefix(lines);
console.error(lines.join('\n'));
console.error('\n' + lines.join('\n') + '\n');
}
this.queueWriteLog('E', msg);
}
Expand All @@ -99,7 +99,7 @@ export class NodeLogger implements d.Logger {
debug(...msg: any[]) {
if (this.shouldLog('debug')) {
msg.push(this.dim(` MEM: ${(process.memoryUsage().rss / 1000000).toFixed(1)}MB`));
const lines = wordWrap(msg);
const lines = wordWrap(msg, getColumns());
this.debugPrefix(lines);
console.log(lines.join('\n'));
}
Expand All @@ -124,14 +124,14 @@ export class NodeLogger implements d.Logger {

if (debug) {
if (this.shouldLog('debug')) {
const lines = wordWrap(msg);
const lines = wordWrap(msg, getColumns());
this.debugPrefix(lines);
console.log(lines.join('\n'));
this.queueWriteLog('D', [`${startMsg} ...`]);
}

} else {
const lines = wordWrap(msg);
const lines = wordWrap(msg, getColumns());
this.infoPrefix(lines);
console.log(lines.join('\n'));
this.queueWriteLog('I', [`${startMsg} ...`]);
Expand All @@ -152,14 +152,14 @@ export class NodeLogger implements d.Logger {

if (debug) {
if (this.shouldLog('debug')) {
const lines = wordWrap([msg]);
const lines = wordWrap([msg], getColumns());
this.debugPrefix(lines);
console.log(lines.join('\n'));
}
this.queueWriteLog('D', [`${finishMsg} ${timeSuffix}`]);

} else {
const lines = wordWrap([msg]);
const lines = wordWrap([msg], getColumns());
this.infoPrefix(lines);
console.log(lines.join('\n'));
this.queueWriteLog('I', [`${finishMsg} ${timeSuffix}`]);
Expand Down Expand Up @@ -276,7 +276,7 @@ export class NodeLogger implements d.Logger {
}

printDiagnostic(d: d.Diagnostic) {
const outputLines = wordWrap([d.messageText]);
const outputLines = wordWrap([d.messageText], getColumns());

if (d.header && d.header !== 'build error' && d.header !== 'build warn') {
outputLines.unshift(INDENT + d.header);
Expand Down Expand Up @@ -336,7 +336,7 @@ export class NodeLogger implements d.Logger {

highlightError(errorLine: string, errorCharStart: number, errorLength: number) {
let rightSideChars = errorLine.length - errorCharStart + errorLength - 1;
while (errorLine.length + INDENT.length > MAX_LEN) {
while (errorLine.length + INDENT.length > MAX_COLUMNS) {
if (errorCharStart > (errorLine.length - errorCharStart + errorLength) && errorCharStart > 5) {
// larger on left side
errorLine = errorLine.substr(1);
Expand Down Expand Up @@ -454,7 +454,13 @@ class CmdTimeSpan {
const LOG_LEVELS = ['debug', 'info', 'warn', 'error'];


function wordWrap(msg: any[]) {
function getColumns() {
const terminalWidth = (process.stdout && (process.stdout as any).columns) || 80;
return Math.max(Math.min(MAX_COLUMNS, terminalWidth), MIN_COLUMNS);
}


export function wordWrap(msg: any[], columns: number) {
const lines: string[] = [];
const words: any[] = [];

Expand Down Expand Up @@ -503,15 +509,15 @@ function wordWrap(msg: any[]) {
lines.push(word());
line = INDENT;

} else if (INDENT.length + word.length > MAX_LEN) {
} else if (INDENT.length + word.length > columns - 1) {
// word is too long to play nice, just give it its own line
if (line.trim().length) {
lines.push(line);
}
lines.push(INDENT + word);
line = INDENT;

} else if ((word.length + line.length) > MAX_LEN) {
} else if ((word.length + line.length) > columns - 1) {
// this word would make the line too long
// print the line now, then start a new one
lines.push(line);
Expand All @@ -526,7 +532,9 @@ function wordWrap(msg: any[]) {
lines.push(line);
}

return lines;
return lines.map(line => {
return (line as any).trimRight();
});
}


Expand Down Expand Up @@ -593,4 +601,5 @@ const JS_KEYWORDS = [


const INDENT = ' ';
const MAX_LEN = 120;
const MIN_COLUMNS = 60;
const MAX_COLUMNS = 120;
69 changes: 69 additions & 0 deletions src/sys/node/test/node-logger.spec.ts
@@ -0,0 +1,69 @@
import { wordWrap } from '../node-logger';


describe('node-logger', () => {

describe('wordWrap', () => {

it('should wrap line longer than columns, with solid word at break that cant break', () => {
const columns = 40;
const msg = [
`abcd abcd abcd z123456789012345678901234567890z abcd abcd abcd `
];
const lines = wordWrap(msg, columns);

expect(lines[0]).toBe(' abcd abcd abcd');
expect(lines[0].length).toBeLessThanOrEqual(columns);

expect(lines[1]).toBe(' z123456789012345678901234567890z');

expect(lines[2]).toBe(' abcd abcd abcd');
expect(lines[2].length).toBeLessThanOrEqual(columns);
});

it('should wrap line longer than columns, with solid word at break that can break', () => {
const columns = 40;
const msg = [
`abcd abcd abcd abcd z12345678901234567890z abcd abcd abcd `
];
const lines = wordWrap(msg, columns);

expect(lines[0]).toBe(' abcd abcd abcd abcd');
expect(lines[0].length).toBeLessThanOrEqual(columns);

expect(lines[1]).toBe(' z12345678901234567890z abcd');
expect(lines[2].length).toBeLessThanOrEqual(columns);

expect(lines[2]).toBe(' abcd abcd');
expect(lines[2].length).toBeLessThanOrEqual(columns);
});

it('should wrap line longer than columns', () => {
const columns = 40;
const msg = [
`1234 1234 1234 1234 abcd wxyz 1234 1234 1234 1234 1234 1234 `
];
const lines = wordWrap(msg, columns);

expect(lines[0]).toBe(' 1234 1234 1234 1234 abcd');
expect(lines[0].length).toBeLessThanOrEqual(columns);

expect(lines[1]).toBe(' wxyz 1234 1234 1234 1234');
expect(lines[1].length).toBeLessThanOrEqual(columns);
});

it('should print one line thats less than max', () => {
const columns = 40;
const msg = [
`This is one line message`
];
const lines = wordWrap(msg, columns);

expect(lines).toHaveLength(1);
expect(lines[0]).toBe(' This is one line message');
expect(lines[0].length).toBeLessThanOrEqual(columns);
});

});

});

0 comments on commit fbb0325

Please sign in to comment.