Skip to content

Commit f058247

Browse files
author
Lee Richmond
committed
Enhance logging
1 parent aa6b6db commit f058247

File tree

7 files changed

+108
-10
lines changed

7 files changed

+108
-10
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
"tslint-loader": "^3.3.0",
3939
"typescript": "^2.1.4",
4040
"webpack": "^1.14.0",
41-
"webpack-stream": "^3.2.0"
41+
"webpack-stream": "^3.2.0",
42+
"winston": "^2.3.0"
4243
},
4344
"dependencies": {
4445
"es6-promise": "^4.0.5",

src/configuration.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import Model from './model';
44
import Attribute from './attribute';
5+
import Logger from './logger';
56

67
export default class Config {
78
static models: Array<typeof Model> = [];
89
static typeMapping: Object = {};
10+
static logger: Logger = new Logger();
911

1012
static bootstrap() : void {
1113
for (let model of this.models) {

src/logger.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
export default class Logger {
2+
private _level = 2;
3+
private LEVELS = {
4+
debug: 1,
5+
info: 2,
6+
warn: 3,
7+
error: 4
8+
}
9+
10+
get level() {
11+
for (let key in this.LEVELS) {
12+
let val = this.LEVELS[key];
13+
if (val === this._level) return key;
14+
}
15+
}
16+
17+
set level(value : string) {
18+
let lvlValue = this.LEVELS[value];
19+
20+
if (lvlValue) {
21+
this._level = lvlValue;
22+
} else {
23+
throw(`Log level must be one of ${Object.keys(this.LEVELS).join(', ')}`)
24+
}
25+
}
26+
27+
debug(stmt) {
28+
if (this._level <= this.LEVELS.debug) {
29+
console.debug(stmt);
30+
}
31+
}
32+
33+
info(stmt) {
34+
if (this._level <= this.LEVELS.info) {
35+
console.info(stmt);
36+
}
37+
}
38+
39+
warn(stmt) {
40+
if (this._level <= this.LEVELS.warn) {
41+
console.warn(stmt);
42+
}
43+
}
44+
45+
error(stmt) {
46+
if (this._level <= this.LEVELS.warn) {
47+
console.error(stmt);
48+
}
49+
}
50+
}

src/request.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Config from './configuration';
2+
import colorize from './util/colorize';
3+
4+
export default class Request {
5+
get(url : string) : Promise<japiDoc> {
6+
Config.logger.info(colorize('cyan', 'GET: ') + colorize('magenta', url));
7+
8+
return new Promise((resolve, reject) => {
9+
fetch(url).then((response) => {
10+
response.json().then((json) => {
11+
Config.logger.debug(colorize('bold', JSON.stringify(json, null, 4)));
12+
resolve(json);
13+
});
14+
});
15+
});
16+
}
17+
}

src/scope.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import Model from './model';
22
import Config from './configuration';
33
import parameterize from './util/parameterize';
44
import IncludeDirective from './util/include-directive';
5+
import Request from './request';
6+
import colorize from './util/colorize';
57

68
export default class Scope {
79
model: typeof Model;
@@ -125,15 +127,9 @@ export default class Scope {
125127
private _fetch(url : string) : Promise<Object> {
126128
let qp = this.toQueryParams();
127129
if (qp) {
128-
url = `${url}?${qp}`
130+
url = `${url}?${qp}`;
129131
}
130-
131-
return new Promise((resolve, reject) => {
132-
fetch(url).then((response) => {
133-
response.json().then((json) => {
134-
resolve(json);
135-
});
136-
});
137-
});
132+
let request = new Request();
133+
return request.get(url);
138134
}
139135
}

src/util/colorize.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const COLORS = {
2+
green: [32, 39],
3+
cyan: [36, 39],
4+
magenta: [35, 39],
5+
bold: [1, 22],
6+
}
7+
8+
export default function colorize(color : string, text: string) : string {
9+
if (supportsColor()) {
10+
let map = COLORS[color];
11+
return `\u001b[${map[0]}m${text}\u001b[${map[1]}m`;
12+
} else {
13+
return text;
14+
}
15+
}
16+
17+
function supportsColor() : boolean {
18+
if (/^screen|^xterm|^vt100|color|ansi|cygwin|linux/i.test(process.env.TERM)) {
19+
return true;
20+
} else {
21+
return false
22+
}
23+
}
24+
25+

test/test-helper.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/// <reference path="../index.d.ts" />
22

3+
let winston = require('winston');
4+
35
import * as es6Promise from 'es6-promise';
46
import 'isomorphic-fetch';
57
import * as sinon from 'sinon';
@@ -8,6 +10,11 @@ es6Promise.polyfill();
810
import * as chai from 'chai';
911
import * as chaiAsPromised from 'chai-as-promised';
1012
import * as chaiThings from 'chai-things';
13+
import Config from '../src/configuration';
14+
import Logger from '../src/logger';
15+
16+
winston.level = 'warn';
17+
Config.logger = winston;
1118

1219
// MUST be in this order
1320
chai.use(chaiThings);

0 commit comments

Comments
 (0)