Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 210e33f

Browse files
author
Michal Vlasák
committed
Add config for environments, simple tests, simplify logs
1 parent 494e1e6 commit 210e33f

6 files changed

Lines changed: 82 additions & 9 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
coverage

.lintstagedrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"ignore": ['**/coverage/**/*.js'],
23
"linters": {
34
"*.js": [
45
"npm run pretty",

index.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,33 @@ pretty.pipe(process.stdout);
1111
const prettyErr = pino.pretty();
1212
prettyErr.pipe(process.stderr);
1313

14-
const simpleStreams = [{ level: 'trace', stream: pretty }, { level: 'error', stream: prettyErr }];
14+
const loggerFactory = (moduleName, options = {}) => {
15+
const isProduction = process.env.NODE_ENV === 'production';
16+
const isTesting = process.env.NODE_ENV === 'test';
17+
let defaultLevel = 'debug';
1518

16-
const logger = pino({ level: 'trace' }, multistream(simpleStreams));
19+
if (isTesting) {
20+
defaultLevel = 'silent';
21+
}
22+
23+
if (options.defaultLevel) {
24+
defaultLevel = options.defaultLevel;
25+
}
1726

18-
logger.warning = logger.warn;
27+
let streams;
28+
if (isProduction) {
29+
streams = [{ level: defaultLevel, stream: process.stdout }, { level: 'warn', stream: process.stderr }];
30+
} else {
31+
// dev behavior = default
32+
streams = [{ level: defaultLevel, stream: pretty }, { level: 'warn', stream: prettyErr }];
33+
}
34+
35+
if (options.streams) {
36+
streams = options.streams;
37+
}
1938

20-
const loggerFactory = moduleName => {
39+
const logger = pino({ level: defaultLevel, timestamp: false, base: {} }, multistream(streams));
40+
logger.warning = logger.warn;
2141
if (!moduleName) {
2242
return logger;
2343
}

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"main": "index.js",
66
"scripts": {
77
"lint-staged": "lint-staged",
8-
"test": "jest",
9-
"test-watch": "jest --watch",
8+
"test": "jest --coverage",
9+
"test-watch": "jest --watch --coverage",
1010
"lint": "eslint .",
1111
"pretty": "prettier --write '*.js'",
1212
"precommit": "lint-staged && npm test"
@@ -20,9 +20,11 @@
2020
"devDependencies": {
2121
"eslint": "4.17.0",
2222
"eslint-config-ackee": "^0.8.1",
23+
"flush-write-stream": "^1.0.2",
2324
"husky": "^0.14.3",
2425
"jest": "^22.4.2",
2526
"lint-staged": "^7.0.0",
26-
"prettier": "^1.11.0"
27+
"prettier": "^1.11.0",
28+
"split2": "^2.2.0"
2729
}
2830
}

tests/helper.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const split = require('split2');
2+
const writer = require('flush-write-stream');
3+
4+
function redirect(func) {
5+
const result = split(JSON.parse);
6+
result.pipe(writer.obj(func));
7+
return result;
8+
}
9+
10+
exports.redirect = redirect;

tests/index.test.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,42 @@
1-
test('empty', () => {
2-
expect(true).toBe(true);
1+
const loggerFactory = require('..');
2+
const { redirect } = require('./helper');
3+
4+
test('can create default logger', () => {
5+
const logger = loggerFactory();
6+
expect(logger).toBeDefined();
7+
});
8+
9+
test('can create named logger', () => {
10+
const logger = loggerFactory('myApp');
11+
expect(logger).toBeDefined();
12+
});
13+
14+
test('can use custom stream', () => {
15+
const logger = loggerFactory('', {
16+
streams: [
17+
{
18+
stream: redirect((chunk, enc, cb) => {
19+
expect(chunk.msg).toBe('Hello');
20+
cb();
21+
}),
22+
},
23+
],
24+
});
25+
26+
logger.info('Hello');
27+
});
28+
29+
test('can use warning level', () => {
30+
const logger = loggerFactory('', {
31+
streams: [
32+
{
33+
stream: redirect((chunk, enc, cb) => {
34+
expect(chunk.msg).toBe('Hello');
35+
cb();
36+
}),
37+
},
38+
],
39+
});
40+
41+
logger.warning('Hello');
342
});

0 commit comments

Comments
 (0)