Skip to content

Commit fe77601

Browse files
author
Gareth Jones
committed
feat(initial): first version
1 parent 030318a commit fe77601

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

lib/index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
class Logger {
3+
4+
log() {}
5+
6+
isLevelEnabled() {
7+
return false;
8+
}
9+
10+
addContext() {}
11+
removeContext() {}
12+
clearContext() {}
13+
}
14+
15+
[ 'Trace', 'Debug', 'Info', 'Warn', 'Error', 'Fatal', 'Mark' ].forEach((level) => {
16+
17+
Logger.prototype[level.toLowerCase()] = () => {};
18+
Logger.prototype[`is${level}Enabled`] = () => false;
19+
20+
});
21+
22+
const checkForLog4js = () => {
23+
try {
24+
return require('log4js');
25+
} catch (e) {
26+
return null;
27+
}
28+
};
29+
30+
const log4js = checkForLog4js();
31+
const loggerFn = log4js ? log4js.getLogger : () => new Logger();
32+
33+
module.exports = {
34+
getLogger: loggerFn
35+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"maxSubjectLength": 72,
4949
"subjectPattern": ".+",
5050
"subjectPatternErrorMsg": "subject does not match subject pattern!",
51-
"helpMessage": "\n# allowed type: feat, fix, docs, style, refactor, example, perf, test, chore, revert\n# subject no more than 50 chars\n# a body line no more than 72 chars"
51+
"helpMessage": "\n# allowed type: feat, fix, docs, style, refactor, example, perf, test, chore, revert\n# subject no more than 72 chars\n# a body line no more than 72 chars"
5252
}
5353
},
5454
"nyc": {

test/index.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const test = require('tap').test;
2+
const sandbox = require('sandboxed-module');
3+
4+
test('../lib/index.js', (batch) => {
5+
6+
batch.test('should provide a getLogger function', (t) => {
7+
const log4js = require('../lib');
8+
9+
t.isA(log4js.getLogger, 'function');
10+
t.end();
11+
});
12+
13+
batch.test('when log4js is not available', (t) => {
14+
const log4js = require('../lib');
15+
const logger = log4js.getLogger();
16+
17+
t.test('logger should provide dummy functions for context', (assert) => {
18+
assert.isA(logger.addContext, 'function');
19+
assert.isA(logger.removeContext, 'function');
20+
assert.isA(logger.clearContext, 'function');
21+
assert.end();
22+
});
23+
24+
t.test('logger should always say that log levels are not enabled', (assert) => {
25+
['Trace','Debug','Info','Warn','Error','Fatal'].forEach((level) => {
26+
assert.false(logger.isLevelEnabled(level));
27+
assert.false(logger[`is${level}Enabled`]());
28+
});
29+
assert.end();
30+
});
31+
32+
t.test('logger should provide logging functions', (assert) => {
33+
assert.isA(logger.log, 'function');
34+
['trace','debug','info','warn','error', 'fatal'].forEach((level) => {
35+
assert.isA(logger[level], 'function');
36+
});
37+
assert.end();
38+
});
39+
40+
t.end();
41+
});
42+
43+
batch.test('when log4js is available', (t) => {
44+
const log4js = sandbox.require('../lib', {
45+
requires: {
46+
log4js: {
47+
getLogger: () => 'cheese'
48+
}
49+
},
50+
singleOnly: true
51+
});
52+
const logger = log4js.getLogger();
53+
54+
t.equal(logger, 'cheese');
55+
t.end();
56+
});
57+
58+
batch.end();
59+
});

0 commit comments

Comments
 (0)