Skip to content

Commit

Permalink
replace logger. TODO: slack/line integration
Browse files Browse the repository at this point in the history
  • Loading branch information
bitrinjani committed Dec 23, 2017
1 parent 311aaa2 commit 5fe4ce4
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 22 deletions.
9 changes: 8 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 ",
"start": "node ./dist/index.js | node ./dist/logger/transport.js",
"test": "jest --maxWorkers=1 --coverage && cat ./coverage/lcov.info | coveralls",
"testfast": "jest",
"coverage": "jest --coverage",
Expand All @@ -17,17 +17,23 @@
},
"dependencies": {
"@bitr/castable": "^1.0.1",
"@types/chalk": "^2.2.0",
"@types/decimal.js": "^0.0.31",
"@types/i18next": "^8.4.2",
"@types/jsonwebtoken": "^7.2.3",
"@types/pino": "^4.7.0",
"chalk": "^2.3.0",
"date-fns": "^1.28.5",
"decimal.js": "^7.3.0",
"fast-json-parse": "^1.0.3",
"i18next": "^10.0.1",
"inversify": "^4.3.0",
"jsonwebtoken": "^8.1.0",
"lodash": "^4.17.4",
"node-fetch": "^1.7.3",
"pino": "^4.10.2",
"reflect-metadata": "^0.1.10",
"split2": "^2.2.0",
"uuid": "^3.1.0",
"winston": "^3.0.0-rc1"
},
Expand All @@ -37,6 +43,7 @@
"@types/nock": "^8.2.1",
"@types/node": "^8.0.53",
"@types/node-fetch": "^1.6.7",
"@types/split2": "^2.1.6",
"@types/uuid": "^3.4.2",
"coveralls": "^3.0.0",
"cpy-cli": "^1.0.1",
Expand Down
6 changes: 3 additions & 3 deletions src/BrokerAdapterRouterImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ export default class BrokerAdapterRouterImpl implements BrokerAdapterRouter {
}

async send(order: Order): Promise<void> {
this.log.debug(order);
this.log.debug(order.toString());
await this.brokerAdapterMap[order.broker].send(order);
}

async cancel(order: Order): Promise<void> {
this.log.debug(order);
this.log.debug(order.toString());
await this.brokerAdapterMap[order.broker].cancel(order);
}

async refresh(order: Order): Promise<void> {
this.log.debug(order);
this.log.debug(order.toString());
await this.brokerAdapterMap[order.broker].refresh(order);
}

Expand Down
2 changes: 1 addition & 1 deletion src/PositionServiceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default class PositionServiceImpl implements PositionService {
this.log.info(hr(21) + 'POSITION' + hr(21));
this.log.info(`Net Exposure: ${_.round(this.netExposure, 3)} BTC`);
_.each(this.positionMap, (position: BrokerPosition) =>
this.log.info(position)
this.log.info(position.toString())
);
this.log.info(hr(50));
this.log.debug(JSON.stringify(this.positionMap));
Expand Down
22 changes: 18 additions & 4 deletions src/logger/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import LoggerFactory, { Logger } from './LoggerFactory';
import * as _ from 'lodash';
import * as pino from 'pino';
import * as util from 'util';

const factory = new LoggerFactory();
const logger = pino({ level: 'debug' });
const cache = new Map();

export function getLogger(name: string): Logger {
return factory.create(_.trimEnd(name, 'Impl'));
export function getLogger(name: string) {
const label = _.trimEnd(name, 'Impl');
if (cache.has(label)) {
return cache.get(label);
}
const childLogger = logger.child({ label });
const normalized = {
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;
}
63 changes: 63 additions & 0 deletions src/logger/pretty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { EOL } from 'os';
import * as split from 'split2';
const Parse = require('fast-json-parse');
const chalk = require('chalk');
import { format as formatDate } from 'date-fns';

interface LogObject {
level: number;
msg: string;
time: number;
label: string;
}

const dateFormat = 'YYYY-MM-DD HH:mm:ss.SSS';

const levels = {
default: 'USERLVL',
60: 'FATAL',
50: 'ERROR',
40: 'WARN',
30: 'INFO',
20: 'DEBUG',
10: 'TRACE'
};

export default function pretty(opts: {colorize: boolean, withLabel: boolean, debug: boolean }) {
const { colorize, withLabel, debug } = opts;
const stream = split(mapLine);
let ctx;
let levelColors;
const pipe = stream.pipe;
stream.pipe = (dest: any) => {
ctx = new chalk.constructor({
enabled: !!(chalk.supportsColor && colorize)
});
levelColors = {
default: ctx.white,
60: ctx.bgRed,
50: ctx.red,
40: ctx.yellow,
30: ctx.green,
20: ctx.blue,
10: ctx.grey
};
return pipe.call(stream, dest);
};
return stream;

function mapLine(json: string): string {
const parsed = new Parse(json);
const logObj: LogObject = parsed.value;
if (parsed.err) {
return json + EOL;
}
if (!debug && logObj.level <= 20) {
return '';
}
const dateString = formatDate(new Date(logObj.time), dateFormat);
const levelString = levelColors[logObj.level](levels[logObj.level]);
const labelString = withLabel ? `[${logObj.label}] ` : '';
return `${dateString} ${levelString} ${labelString}${logObj.msg}${EOL}`;
}
}
8 changes: 8 additions & 0 deletions src/logger/transport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pretty from './pretty';
import * as fs from 'fs';

const infoFile = fs.createWriteStream('logs/info.log', { flags: 'a' });
const debugFile = fs.createWriteStream('logs/debug.log', { flags: 'a' });
process.stdin.pipe(pretty({ colorize: true, withLabel: false, debug: false })).pipe(process.stdout);
process.stdin.pipe(pretty({ colorize: false, withLabel: true, debug: false })).pipe(infoFile);
process.stdin.pipe(pretty({ colorize: false, withLabel: true, debug: true })).pipe(debugFile);
100 changes: 87 additions & 13 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@
js-tokens "^3.0.0"

"@bitr/castable@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@bitr/castable/-/castable-1.0.1.tgz#ddfa38580e35668ea3f270b250bc99022cb2bdf9"
version "1.0.2"
resolved "https://registry.yarnpkg.com/@bitr/castable/-/castable-1.0.2.tgz#c41f7067f0afe815a8de78ff1d03045f6f8edf9a"
dependencies:
reflect-metadata "^0.1.10"

"@types/chalk@^2.2.0":
version "2.2.0"
resolved "https://registry.yarnpkg.com/@types/chalk/-/chalk-2.2.0.tgz#b7f6e446f4511029ee8e3f43075fb5b73fbaa0ba"
dependencies:
chalk "*"

"@types/decimal.js@^0.0.31":
version "0.0.31"
resolved "https://registry.yarnpkg.com/@types/decimal.js/-/decimal.js-0.0.31.tgz#cb2a797ccb0cbb0ead0b91134bd6b02429db931f"
Expand Down Expand Up @@ -58,6 +64,18 @@
version "8.0.53"
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.53.tgz#396b35af826fa66aad472c8cb7b8d5e277f4e6d8"

"@types/pino@^4.7.0":
version "4.7.0"
resolved "https://registry.yarnpkg.com/@types/pino/-/pino-4.7.0.tgz#c95790496a82766a1b41050df9b322fe9639a129"
dependencies:
"@types/node" "*"

"@types/split2@^2.1.6":
version "2.1.6"
resolved "https://registry.yarnpkg.com/@types/split2/-/split2-2.1.6.tgz#b095c9e064853824b22c67993d99b066777402b1"
dependencies:
"@types/node" "*"

"@types/uuid@^3.4.2":
version "3.4.3"
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-3.4.3.tgz#121ace265f5569ce40f4f6d0ff78a338c732a754"
Expand Down Expand Up @@ -547,6 +565,14 @@ center-align@^0.1.1:
deep-eql "^0.1.3"
type-detect "^1.0.0"

chalk@*, chalk@^2.0.0, chalk@^2.0.1, chalk@^2.1.0, chalk@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.3.0.tgz#b5ea48efc9c1793dccc9b4767c93914d3f2d52ba"
dependencies:
ansi-styles "^3.1.0"
escape-string-regexp "^1.0.5"
supports-color "^4.0.0"

chalk@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-1.1.3.tgz#a8115c55e4a702fe4d150abd3872822a7e09fc98"
Expand All @@ -557,14 +583,6 @@ chalk@^1.1.3:
strip-ansi "^3.0.0"
supports-color "^2.0.0"

chalk@^2.0.0, chalk@^2.0.1, chalk@^2.1.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.3.0.tgz#b5ea48efc9c1793dccc9b4767c93914d3f2d52ba"
dependencies:
ansi-styles "^3.1.0"
escape-string-regexp "^1.0.5"
supports-color "^4.0.0"

chokidar@^1.6.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-1.7.0.tgz#798e689778151c8076b4b360e5edd28cda2bb468"
Expand Down Expand Up @@ -915,6 +933,12 @@ encoding@^0.1.11:
dependencies:
iconv-lite "~0.4.13"

end-of-stream@^1.1.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/end-of-stream/-/end-of-stream-1.4.0.tgz#7a90d833efda6cfa6eac0f4949dbb0fad3a63206"
dependencies:
once "^1.4.0"

env-variable@0.0.x:
version "0.0.3"
resolved "https://registry.yarnpkg.com/env-variable/-/env-variable-0.0.3.tgz#b86c1641be5610267d506f18071ea76d707097cb"
Expand Down Expand Up @@ -1041,6 +1065,10 @@ fast-deep-equal@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-1.0.0.tgz#96256a3bc975595eb36d82e9929d060d893439ff"

fast-json-parse@^1.0.0, fast-json-parse@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/fast-json-parse/-/fast-json-parse-1.0.3.tgz#43e5c61ee4efa9265633046b770fb682a7577c4d"

fast-json-stable-stringify@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz#d5142c0caee6b1189f87d3a76111064f86c8bbf2"
Expand All @@ -1049,6 +1077,10 @@ fast-levenshtein@~2.0.4:
version "2.0.6"
resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917"

fast-safe-stringify@^1.0.8, fast-safe-stringify@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/fast-safe-stringify/-/fast-safe-stringify-1.2.1.tgz#c4b2477dd585de4488aa6665d4acfae41462d999"

fb-watchman@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/fb-watchman/-/fb-watchman-2.0.0.tgz#54e9abf7dfa2f26cd9b1636c588c1afc05de5d58"
Expand Down Expand Up @@ -1093,6 +1125,10 @@ find-up@^2.1.0:
dependencies:
locate-path "^2.0.0"

flatstr@^1.0.4:
version "1.0.5"
resolved "https://registry.yarnpkg.com/flatstr/-/flatstr-1.0.5.tgz#5b451b08cbd48e2eac54a2bbe0bf46165aa14be3"

for-in@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/for-in/-/for-in-1.0.2.tgz#81068d295a8142ec0ac726c6e2200c30fb6d5e80"
Expand Down Expand Up @@ -2394,7 +2430,7 @@ object.omit@^2.0.0:
for-own "^0.1.4"
is-extendable "^0.1.1"

once@^1.3.0, once@^1.3.3, once@^1.4.0:
once@^1.3.0, once@^1.3.1, once@^1.3.3, once@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
dependencies:
Expand Down Expand Up @@ -2536,6 +2572,18 @@ pinkie@^2.0.0:
version "2.0.4"
resolved "https://registry.yarnpkg.com/pinkie/-/pinkie-2.0.4.tgz#72556b80cfa0d48a974e80e77248e80ed4f7f870"

pino@^4.10.2:
version "4.10.2"
resolved "https://registry.yarnpkg.com/pino/-/pino-4.10.2.tgz#77e93cdfa1cdb58f688cbb0abaebe67eb2f315f4"
dependencies:
chalk "^2.3.0"
fast-json-parse "^1.0.0"
fast-safe-stringify "^1.2.1"
flatstr "^1.0.4"
pump "^1.0.3"
quick-format-unescaped "^1.1.1"
split2 "^2.2.0"

pkg-dir@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/pkg-dir/-/pkg-dir-2.0.0.tgz#f6d5d1109e19d63edf428e0bd57e12777615334b"
Expand Down Expand Up @@ -2577,6 +2625,13 @@ pseudomap@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3"

pump@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/pump/-/pump-1.0.3.tgz#5dfe8311c33bbf6fc18261f9f34702c47c08a954"
dependencies:
end-of-stream "^1.1.0"
once "^1.3.1"

punycode@^1.4.1:
version "1.4.1"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.4.1.tgz#c0d5a63b2718800ad8e1eb0fa5269c84dd41845e"
Expand All @@ -2593,6 +2648,12 @@ qs@~6.4.0:
version "6.4.0"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.4.0.tgz#13e26d28ad6b0ffaa91312cd3bf708ed351e7233"

quick-format-unescaped@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/quick-format-unescaped/-/quick-format-unescaped-1.1.1.tgz#e77555ef3e66e105d4039e13ef79201284fee916"
dependencies:
fast-safe-stringify "^1.0.8"

randomatic@^1.1.3:
version "1.1.7"
resolved "https://registry.yarnpkg.com/randomatic/-/randomatic-1.1.7.tgz#c7abe9cc8b87c0baa876b19fde83fd464797e38c"
Expand Down Expand Up @@ -2624,7 +2685,7 @@ read-pkg@^1.0.0:
normalize-package-data "^2.3.2"
path-type "^1.0.0"

readable-stream@^2.0.1, readable-stream@^2.0.2, readable-stream@^2.0.6, readable-stream@^2.1.4:
readable-stream@^2.0.1, readable-stream@^2.0.2, readable-stream@^2.0.6, readable-stream@^2.1.4, readable-stream@^2.1.5:
version "2.3.3"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.3.tgz#368f2512d79f9d46fdfc71349ae7878bbc1eb95c"
dependencies:
Expand Down Expand Up @@ -2905,6 +2966,12 @@ spdx-license-ids@^1.0.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/spdx-license-ids/-/spdx-license-ids-1.2.2.tgz#c9df7a3424594ade6bd11900d596696dc06bac57"

split2@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/split2/-/split2-2.2.0.tgz#186b2575bcf83e85b7d18465756238ee4ee42493"
dependencies:
through2 "^2.0.2"

sprintf-js@~1.0.2:
version "1.0.3"
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
Expand Down Expand Up @@ -3068,6 +3135,13 @@ throat@^4.0.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/throat/-/throat-4.1.0.tgz#89037cbc92c56ab18926e6ba4cbb200e15672a6a"

through2@^2.0.2:
version "2.0.3"
resolved "https://registry.yarnpkg.com/through2/-/through2-2.0.3.tgz#0004569b37c7c74ba39c43f3ced78d1ad94140be"
dependencies:
readable-stream "^2.1.5"
xtend "~4.0.1"

tmpl@1.0.x:
version "1.0.4"
resolved "https://registry.yarnpkg.com/tmpl/-/tmpl-1.0.4.tgz#23640dd7b42d00433911140820e5cf440e521dd1"
Expand Down Expand Up @@ -3397,7 +3471,7 @@ xml-name-validator@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/xml-name-validator/-/xml-name-validator-2.0.1.tgz#4d8b8f1eccd3419aa362061becef515e1e559635"

xtend@^4.0.1:
xtend@^4.0.1, xtend@~4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af"

Expand Down

0 comments on commit 5fe4ce4

Please sign in to comment.