Skip to content

Commit

Permalink
finalize logger migration
Browse files Browse the repository at this point in the history
  • Loading branch information
bitrinjani committed Dec 24, 2017
1 parent 5fe4ce4 commit b5ebb55
Show file tree
Hide file tree
Showing 19 changed files with 225 additions and 203 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"scripts": {
"prestart": "rimraf dist && tsc -p tsconfig.json && cpy src/config.json dist ",
"start": "node ./dist/index.js | node ./dist/logger/transport.js",
"start": "node ./dist | node ./dist/transport",
"test": "jest --maxWorkers=1 --coverage && cat ./coverage/lcov.info | coveralls",
"testfast": "jest",
"coverage": "jest --coverage",
Expand Down Expand Up @@ -72,6 +72,7 @@
"collectCoverageFrom": [
"**/*.{ts,tsx}",
"!src/index.ts",
"!src/transport/index.ts",
"!**/__tests__/**",
"!**/node_modules/**",
"!**/vendor/**",
Expand Down
31 changes: 0 additions & 31 deletions src/__tests__/logger/LoggerFactory.test.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import LineIntegration from '../../logger/LineIntegration';
import LineIntegration from '../../transport/LineIntegration';
import { LineConfig } from '../../types';
import * as nock from 'nock';
import { delay } from '../../util';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import SlackIntegration from '../../logger/SlackIntegration';
import SlackIntegration from '../../transport/SlackIntegration';
import { SlackConfig } from '../../types';
import * as nock from 'nock';

Expand Down
29 changes: 29 additions & 0 deletions src/__tests__/transport/pretty.mock.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
jest.mock('date-fns', () => ({
format: jest
.fn()
.mockImplementationOnce(() => {
throw new Error();
})
.mockImplementationOnce(() => {
throw new Error('test');
})
}));

import pretty from '../../transport/pretty';
import { Readable } from 'stream';

test('pretty split callback throws', () => {
const result = pretty({ colorize: true, withLabel: true, debug: true });
const stream = new Readable();
stream.push('{ "msg": "Test message", "time": 1514074545477, "level": 40, "label": "TestStream" }');
stream.push(null);
stream.pipe(result).pipe(process.stdout);
});

test('pretty split callback throws 2', () => {
const result = pretty({ colorize: true, withLabel: true, debug: true });
const stream = new Readable();
stream.push('{ "msg": "Test message", "time": 1514074545477, "level": 40, "label": "TestStream" }');
stream.push(null);
stream.pipe(result).pipe(process.stdout);
});
58 changes: 58 additions & 0 deletions src/__tests__/transport/pretty.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import pretty from '../../transport/pretty';
import { Readable } from 'stream';

test('pretty', () => {
const result = pretty({ colorize: true, withLabel: true, debug: true });
const stream = new Readable();
stream.push('{ "msg": "Test message", "time": 1514074545477, "level": 40, "label": "TestStream" }');
stream.push(null);
stream.pipe(result).pipe(process.stdout);
});

test('pretty no label', () => {
const result = pretty({ colorize: true, withLabel: false, debug: true });
const stream = new Readable();
stream.push('{ "msg": "Test message", "time": 1514074545477, "level": 40, "label": "TestStream" }');
stream.push(null);
stream.pipe(result).pipe(process.stdout);
});

test('pretty debug', () => {
const result = pretty({ colorize: true, withLabel: true, debug: true });
const stream = new Readable();
stream.push('{ "msg": "Test message", "time": 1514074545477, "level": 20, "label": "TestStream" }');
stream.push(null);
stream.pipe(result).pipe(process.stdout);
});

test('pretty no debug', () => {
const result = pretty({ colorize: true, withLabel: true, debug: false });
const stream = new Readable();
stream.push('{ "msg": "Test message", "time": 1514074545477, "level": 20, "label": "TestStream" }');
stream.push(null);
stream.pipe(result).pipe(process.stdout);
});

test('pretty no label/debug', () => {
const result = pretty({ colorize: true, withLabel: false, debug: false });
const stream = new Readable();
stream.push('{ "msg": "Test message", "time": 1514074545477, "level": 20, "label": "TestStream" }');
stream.push(null);
stream.pipe(result).pipe(process.stdout);
});

test('pretty no color/label/debug', () => {
const result = pretty({ colorize: false, withLabel: false, debug: false });
const stream = new Readable();
stream.push('{ "msg": "Test message", "time": 1514074545477, "level": 20, "label": "TestStream" }');
stream.push(null);
stream.pipe(result).pipe(process.stdout);
});

test('pretty invalid json', () => {
const result = pretty({ colorize: true, withLabel: true, debug: true });
const stream = new Readable();
stream.push('{ "msg": Test message", "time": 1514074545477, "level": 40, "label": "TestStream" }');
stream.push(null);
stream.pipe(result).pipe(process.stdout);
});
82 changes: 0 additions & 82 deletions src/logger/LoggerFactory.ts

This file was deleted.

19 changes: 16 additions & 3 deletions src/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,34 @@ import * as _ from 'lodash';
import * as pino from 'pino';
import * as util from 'util';

export const options = { enabled: true };

const logger = pino({ level: 'debug' });
const cache = new Map();
const noop = () => {
return;
};

export function getLogger(name: string) {
if (!options.enabled) {
return {
debug: noop,
info: noop,
warn: noop,
error: noop
};
}
const label = _.trimEnd(name, 'Impl');
if (cache.has(label)) {
return cache.get(label);
}
const childLogger = logger.child({ label });
const normalized = {
const wrappedLogger = {
debug: (s: string, ...args) => childLogger.debug(util.format(s, ...args)),
info: (s: string, ...args) => childLogger.info(util.format(s, ...args)),
warn: (s: string, ...args) => childLogger.warn(util.format(s, ...args)),
error: (s: string, ...args) => childLogger.error(util.format(s, ...args))
};
cache.set(label, normalized);
return normalized;
cache.set(label, wrappedLogger);
return wrappedLogger;
}
13 changes: 0 additions & 13 deletions src/logger/lessSplat.ts

This file was deleted.

63 changes: 0 additions & 63 deletions src/logger/pretty.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/logger/transport.ts

This file was deleted.

File renamed without changes.
File renamed without changes.
Loading

0 comments on commit b5ebb55

Please sign in to comment.