Skip to content

Commit

Permalink
feat: Basic node support with non-colorize logging, with accompanying…
Browse files Browse the repository at this point in the history
… tests
  • Loading branch information
lisaychuang committed Aug 6, 2018
1 parent 49f18e3 commit 73998e8
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 8 deletions.
44 changes: 38 additions & 6 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class Logger {
// printerOrConfig is a Logger.Config
const config: Logger.Config = printerOrConfig as Logger.Config;
this.printer = config.printer;
this.env = config.env || isBrowser() ? 'browser' : 'node';
this.env = config.env || (isBrowser() ? 'browser' : 'node');
}
this.setupStyles();
}
Expand Down Expand Up @@ -271,12 +271,9 @@ export class Logger {
private printMessage(level: number) {
if (this.env === 'browser') {
return this.printBrowserMessage(level);
} else {
return this.printNodeMessage(level);
}
throw new Error(
'[env = ' +
this.env +
']Only browser environments are currently supported'
);
}

private printBrowserMessage(level: number) {
Expand Down Expand Up @@ -333,6 +330,41 @@ export class Logger {
}
this.nonStringsToLog = [];
}

private printNodeMessage(level: number) {
if (level <= this.level) {
let functionName = CONSOLE_FUNCTIONS[level];
let logFunction = this.printer[functionName];
let allMsgs = '';

/** Flush all prefix and styles into msgsAndStyles
* Note: there may not be styles associated with a message or prefix!
*/

// prefix styles
for (let [msg] of this.prefixesAndStyles) {
// all node messages are unstyled... for now
allMsgs += `[${msg}]`;
}
// white space between Prefix and Message
if (allMsgs.length > 0) {
allMsgs += ' '; // space between prefixes and rest of logged message
}
// message styles
for (let [msg] of this.msgsAndStyles) {
allMsgs += `${msg}`;
}
logFunction(allMsgs);
this.msgsAndStyles = [];
}
// in printMessage, we need to deal with that array (i.e., printer.dir([1, 2, 3]) )
// and set the array of non-strings back to empty
// log.debug([1, 2, 3]);
for (let nonString of this.nonStringsToLog) {
this.printer.dir(nonString);
}
this.nonStringsToLog = [];
}
}

export type LoggerWithStyles = Logger &
Expand Down
4 changes: 2 additions & 2 deletions test/formatting-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ QUnit.test(
logger
} = makeTestLogger(Level.debug);
logger.bgAliceBlue.pushPrefix('AAA').pushPrefix('bbb');
logger.red.txt('should be red').log('should be "clear');
logger.red.txt('should be red').log('should be clear');

assert.ok(
log[0].indexOf(
Expand All @@ -107,7 +107,7 @@ QUnit.test(
log[0],

[
'%c[AAA]%c[bbb]%c %cshould be red%cshould be "clear',
'%c[AAA]%c[bbb]%c %cshould be red%cshould be clear',
'background-color: aliceBlue;',
'color: inherit; background-color: transparent;',
'color: inherit; background-color: transparent;',
Expand Down
65 changes: 65 additions & 0 deletions test/simple-node-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import Logger, { Level } from '../src/index';
import { makeTestLogger } from './test-helpers';

QUnit.module('Node.js simple logging cases');

QUnit.test('Logging with 1 style per message', assert => {
const {
printer: { messages },
logger
} = makeTestLogger(Level.debug, 'node');
logger.red.warn('a warning');
logger.bgYellow.debug('a debug');
logger.darkOrchid.log('a log');
logger.bgLavenderBlush.error('an error');

// Make sure each of the message's color styling are not applied in Node
assert.deepEqual(messages, {
warn: [['a warning']],
debug: [['a debug']],
log: [['a log']],
error: [['an error']]
});
});

QUnit.test('Prefixes are styled correctly', assert => {
const {
logger,
printer: {
messages: { error }
}
} = makeTestLogger(Level.warn, 'node');
logger.pushPrefix('prefix');
logger.error('Prefix this error');

// Make sure Prefix is styled [prefix]
assert.deepEqual(error[0][0], '[prefix] Prefix this error');
assert.equal(
error[0].length,
1,
'There are no style applied to the "blank space"'
);
});

QUnit.test('All style are removed from multi-segment messages', assert => {
const {
printer: {
messages: { log }
},
logger
} = makeTestLogger(Level.debug, 'node');
logger.bgAliceBlue.pushPrefix('AAA').pushPrefix('bbb');
logger.red.txt('should have no style').log('should have no styles either');

assert.equal(
log[0].length,
1,
'There are no style applied to the "blank space"'
);
assert.deepEqual(
log[0],

['[AAA][bbb] should have no styleshould have no styles either'],
'Log message in node has no styling'
);
});

0 comments on commit 73998e8

Please sign in to comment.