Skip to content

Commit

Permalink
feat: tagged logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-north committed Aug 2, 2018
1 parent 5cb8600 commit 8fba2b3
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 22 deletions.
53 changes: 52 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export type ILoggerInstance = Printer & {
[K in keyof typeof COLORS]: ILoggerInstance
} & {
txt(str: string): ILoggerInstance
pushPrefix(str: string): ILoggerInstance
popPrefix(): ILoggerInstance
};
// tslint:disable-next-line:interface-name
export interface ILogger {
Expand All @@ -49,6 +51,9 @@ class Logger {
// An array of current styles
private stylesInProgress: string[] = [];

// An array of prefixes that go at the beginning of each message
private prefixesAndStyles: Array<[string, string]> = [];

// An array of message & their associated styles
private msgsAndStyles: Array<[string, string]> = [];

Expand All @@ -63,6 +68,48 @@ class Logger {
this.setupStyles();
}

/**
* Adds a prefix that will be applied to all messages.
* This is useful for indicating the context in which a message is logged,
* without forcing developers to "re-state" that context over and over.
*
* @example
* ```ts
* const logger = new Logger();
*
* function fooFunction() {
* logger.pushPrefix('foo');
* ...
* logger.log('hello');
* logger.log('world');
* ...
* logger.popPrefix();
* }
*
* logger.log('begin'); // "begin"
* fooFunction(); // "[foo]: hello"
* // "[foo]: world"
* logger.log('end'); // "end"
*
* ```
* @param name the name of the tag
*/
pushPrefix(name: string) {
this.prefixesAndStyles.push([name, this.stylesInProgress.join('')]);
this.stylesInProgress = [];
return this;
}

/**
* Remove the most recently added prefix from the logger
*
* @see {Logger.pushPrefix}
*/
popPrefix() {
this.prefixesAndStyles.pop();
return this;
}

css(style: string) {
this.stylesInProgress.push(style);
return this;
Expand Down Expand Up @@ -138,11 +185,15 @@ class Logger {
let logFunction = this.printer[functionName];
let allMsgs = '';
let allStyles: string[] = [];
for (let [msg, style] of this.prefixesAndStyles) {
allMsgs += `%c[${msg}]`;
allStyles.push(style);
}
if (allMsgs.length > 0) allMsgs += ': ';
for (let [msg, style] of this.msgsAndStyles) {
allMsgs += `%c ${msg}`;
allStyles.push(style);
}

logFunction(allMsgs, ...allStyles);
this.msgsAndStyles = [];
}
Expand Down
34 changes: 34 additions & 0 deletions test/tagged-logging-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Logger from 'bite-log';
import { logCountAssert, makeTestPrinter } from './test-helpers';

QUnit.module('Tagged logging');

QUnit.test(
'Pushing a tag results in the log mesages being prefixed',
assert => {
const printer = makeTestPrinter();
const logger = new Logger(4, printer);
logger.log('hello'); // "hello"
logger.log('world'); // "world"
logger.pushPrefix('foo');
logger.log('goodbye'); // "[foo]: goodbye"
logger.log('mars'); // "[foo]: mars"
logger.pushPrefix('bar');
logger.log('(and saturn!)'); // "[foo][bar]: (and saturn)!"
logger.popPrefix();
logger.popPrefix();
logger.log('but not pluto'); // "but not pluto"
logCountAssert(
{ message: 'after logging four things', assert, printer },
{ l: 6 }
);
const logs = printer.messages.log;
assert.ok(logs[0].join('').indexOf('foo') < 0, 'First message has no "foo" tag');
assert.ok(logs[1].join('').indexOf('foo') < 0, 'Second message has no "foo" tag');
assert.ok(logs[2].join('').indexOf('foo') >= 0, 'Third message has a "foo" tag');
assert.ok(logs[3].join('').indexOf('foo') >= 0, 'Fourth message has a "foo" tag');
assert.ok(logs[4].join('').indexOf('foo') >= 0, 'Fifth message has a "foo" tag');
assert.ok(logs[4].join('').indexOf('bar') >= 0, 'Fifth message has a "bar" tag');
assert.ok(logs[5].join('').indexOf('foo') < 0, 'Sixth message has no "foo" tag');
}
);
42 changes: 21 additions & 21 deletions test/test-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,35 @@ export function logCountAssert(
message,
assert,
printer
}: { message: string; assert: Assert; printer: Printer },
}: { message: string; assert: Assert; printer: Printer; },
{ e, w, l, d }: { e?: number; w?: number; l?: number; d?: number }
) {
if (typeof e !== 'undefined') {
assert.equal(
(printer as any).messages.error.length,
e,
`${message}: ${e} error(s) were logged`
);
assert.equal(
(printer as any).messages.error.length,
e,
`${message}: ${e} error(s) were logged`
);
}
if (typeof w !== 'undefined') {
assert.equal(
(printer as any).messages.warn.length,
w,
`${message}: ${w} warning(s) was logged`
);
assert.equal(
(printer as any).messages.warn.length,
w,
`${message}: ${w} warning(s) was logged`
);
}
if (typeof d !== 'undefined') {
assert.equal(
(printer as any).messages.debug.length,
d,
`${message}: ${d} debug(s) were logged`
);
assert.equal(
(printer as any).messages.debug.length,
d,
`${message}: ${d} debug(s) were logged`
);
}
if (typeof l !== 'undefined') {
assert.equal(
(printer as any).messages.log.length,
l,
`${message}: ${l} log(s) were logged`
);
assert.equal(
(printer as any).messages.log.length,
l,
`${message}: ${l} log(s) were logged`
);
}
}

0 comments on commit 8fba2b3

Please sign in to comment.