Skip to content

Commit

Permalink
Added ability to set custom aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
btoll committed Dec 1, 2016
1 parent 0613c85 commit 65a69dd
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 18 deletions.
2 changes: 1 addition & 1 deletion jasmine.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"spec_dir": "spec",
"spec_files": [
"**/*.js"
"**/*spec.js"
],
"helpers": [
"helpers/**/*.js"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"coveralls": "npm run coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage",
"lint": "eslint spec src",
"specs": "JASMINE_CONFIG_PATH=jasmine.json jasmine && npm run suites",
"suites": "dump_describes -t spec/logger.js -d spec -v --html && dump_describes -t spec/logger.js -d spec -v --md",
"suites": "dump_describes -t spec/logger-spec.js -d spec -v --html && dump_describes -t spec/logger-spec.js -d spec -v --md",
"test": "npm run clean && npm run lint && npm run specs"
}
}
Expand Down
26 changes: 21 additions & 5 deletions spec/logger.js → spec/logger-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const stdoutLog = './stdout.log';
let logger = require('../src/index');

describe('logger', () => {

const fn = flag =>
function () {
fs.writeFileSync(stdoutLog, Array.from(arguments).join(' '), {
Expand All @@ -34,7 +33,24 @@ describe('logger', () => {
expect(logger.getColor()).toBe(color);
});

describe('TODO: aliases', () => {
describe('aliases', () => {
it('should not create aliases by default', () => {
logger = logger.setLogger(makeLogger());
expect(() => logger.debug()).toThrow();
});

it('should create aliases if opting-in', () => {
logger = logger.setLogger(makeLogger(), true);
expect(() => logger.debug()).not.toThrow();
});

it('should accept an object of custom aliases', () => {
logger = logger.setLogger(makeLogger(), {
foo: 'log'
});

expect(() => logger.foo()).not.toThrow();
});
});

describe('log level', () => {
Expand All @@ -59,7 +75,7 @@ describe('logger', () => {
describe('throttling the log level', () => {
beforeAll(() => {
// We want to append the logs for these tests.
logger = logger.setLogger(makeLogger('a'));
logger = logger.setLogger(makeLogger('a'), true);
logger.disableColor();
});

Expand Down Expand Up @@ -113,7 +129,7 @@ describe('logger', () => {
logger.setLogLevel(255);

myConsole = makeLogger();
logger = logger.setLogger(myConsole);
logger = logger.setLogger(myConsole, true);

// Always overwrite previous contents.
fs.writeFileSync(stdoutLog, '');
Expand Down Expand Up @@ -167,7 +183,7 @@ describe('logger', () => {
error: fn(),
info: fn(),
log: fn()
}));
}), true);

expect(() => logger.warn('foobar')).toThrow();
});
Expand Down
19 changes: 17 additions & 2 deletions spec/logger_suite.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,23 @@ <h3>Test suite 'logger'</h3>
<span class="describe">
(<a href="#">describe</a>)
</span>
<span>'TODO: aliases'</span>
</p><div></div></div><div style=""><p class="stripe">
<span>'aliases'</span>
</p><div><div style="padding-left: 50px;"><p class="">
<span class="it">
it ->
</span>
<span>'should not create aliases by default'</span>
</p></div><div style="padding-left: 50px;"><p class="">
<span class="it">
it ->
</span>
<span>'should create aliases if opting-in'</span>
</p></div><div style="padding-left: 50px;"><p class="">
<span class="it">
it ->
</span>
<span>'should accept an object of custom aliases'</span>
</p></div></div></div><div style=""><p class="stripe">
<span class="describe">
(<a href="#">describe</a>)
</span>
Expand Down
5 changes: 4 additions & 1 deletion spec/logger_suite.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
- it -> 'should allow access to the underyling wrapped logger object'
- it -> 'should allow access to the underyling color package'

###(describe) 'TODO: aliases'
###(describe) 'aliases'
it -> 'should not create aliases by default'
it -> 'should create aliases if opting-in'
it -> 'should accept an object of custom aliases'

###(describe) 'log level'

Expand Down
32 changes: 24 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ const logLevels = {
const preprocess = () => '';
const postprocess = () => '';

const aliases = {
const defaultAliases = {
debug: 'info',
fatal: 'error',
raw: 'log',
success: 'log'
};

let aliases = null;
// Default to logging everything.
let logLevel = 255;
let logger = {};
Expand All @@ -60,10 +61,10 @@ const getLogLevel = () =>
logLevel;

const normalizeMethodName = methodName =>
aliases[methodName] || methodName;
(aliases && aliases[methodName]) || methodName;

/**
* level === Number or a String.
* @param {Number/String} level
*
* For those who know what they're doing, they can simply pass the bit value as a Number.
*
Expand All @@ -86,7 +87,15 @@ const setLogLevel = level =>
logLevels[level] :
level;

const setLogger = target => {
/**
* @param {Object} target
* @param {Boolean/Object} useAliases
*
* Must opt-in to use aliases.
* `true` to use default aliases.
* Else pass an object of aliases.
*/
const setLogger = (target, useAliases) => {
// Create a new object and its delegate every time a new logger is set.
logger = Object.setPrototypeOf({}, proto);

Expand All @@ -97,8 +106,15 @@ const setLogger = target => {
logger[methodName] = wrap(methodName);
}

for (const alias of Object.keys(aliases)) {
logger[alias] = wrap(alias);
if (useAliases) {
// Check if `useAliases` is an object of custom aliases.
aliases = (useAliases !== true) ?
useAliases :
defaultAliases;

for (const alias of Object.keys(aliases)) {
logger[alias] = wrap(alias);
}
}

return logger;
Expand Down Expand Up @@ -150,8 +166,8 @@ const proto = {

let wrapped = {};

// Defaults to the global console.
setLogger(console);
// Defaults to the global console (opt-in for aliases).
setLogger(console, true);

// TODO: Allow a format to be set at runtime?
// const __setFormat = f =>
Expand Down

0 comments on commit 65a69dd

Please sign in to comment.