@subsquid/logger 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -6
- package/lib/demo.js +8 -1
- package/lib/demo.js.map +1 -1
- package/lib/sinks/pretty.d.ts +4 -3
- package/lib/sinks/pretty.d.ts.map +1 -1
- package/lib/sinks/pretty.js +46 -98
- package/lib/sinks/pretty.js.map +1 -1
- package/package.json +2 -2
- package/src/demo.ts +12 -1
- package/src/sinks/pretty.ts +53 -94
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ log.debug('message with severity debug')
|
|
|
15
15
|
log.info({foo: 1, bar: 2}, 'message and some additional attributes')
|
|
16
16
|
|
|
17
17
|
// info message consisting only of attributes
|
|
18
|
-
log.info({a: 1, b: 2, c: 3, array: [4, 5]})
|
|
18
|
+
log.info({a: 1, b: 2, c: 3, array: [4, 5], obj: {foo: 'foo', bar: "bar"}})
|
|
19
19
|
|
|
20
20
|
// pass an Error object inplace of attributes
|
|
21
21
|
log.warn(new Error('Some error occured'))
|
|
@@ -63,9 +63,9 @@ When `stderr` is connected to a terminal, log records will be pretty printed.
|
|
|
63
63
|
Otherwise, log records will be written as JSON lines.
|
|
64
64
|
|
|
65
65
|
```
|
|
66
|
-
{"level":2,"time":
|
|
67
|
-
{"level":2,"time":
|
|
68
|
-
{"level":2,"time":
|
|
69
|
-
{"level":3,"time":
|
|
70
|
-
{"level":4,"time":
|
|
66
|
+
{"level":2,"time":1669387525765,"ns":"sqd:demo","msg":"message with severity info"}
|
|
67
|
+
{"level":2,"time":1669387525766,"ns":"sqd:demo","msg":"message and some additional attributes","foo":1,"bar":2}
|
|
68
|
+
{"level":2,"time":1669387525766,"ns":"sqd:demo","a":1,"b":2,"c":3,"array":[4,5,6],"obj":{"foo":"foo","bar":"bar"}}
|
|
69
|
+
{"level":3,"time":1669387525766,"ns":"sqd:demo","err":{"stack":"Error: Some error occured\n at Object.<anonymous> (/Users/eldar/dev/squid/util/logger/lib/demo.js:11:10)\n at Module._compile (node:internal/modules/cjs/loader:1159:14)\n at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)\n at Module.load (node:internal/modules/cjs/loader:1037:32)\n at Module._load (node:internal/modules/cjs/loader:878:12)\n at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)\n at node:internal/main/run_main_module:23:47"}}
|
|
70
|
+
{"level":4,"time":1669387525766,"ns":"sqd:demo","msg":"weird","err":{"stack":"Error: Another error\n at Object.<anonymous> (/Users/eldar/dev/squid/util/logger/lib/demo.js:13:18)\n at Module._compile (node:internal/modules/cjs/loader:1159:14)\n at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)\n at Module.load (node:internal/modules/cjs/loader:1037:32)\n at Module._load (node:internal/modules/cjs/loader:878:12)\n at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)\n at node:internal/main/run_main_module:23:47"},"a":1,"b":2}
|
|
71
71
|
```
|
package/lib/demo.js
CHANGED
|
@@ -6,7 +6,7 @@ log.info('message with severity info');
|
|
|
6
6
|
log.debug('message with severity debug');
|
|
7
7
|
log.info({ foo: 1, bar: 2 }, 'message and some additional attributes');
|
|
8
8
|
// info message consisting only of attributes
|
|
9
|
-
log.info({ a: 1, b: 2, c: 3, array: [4, 5,
|
|
9
|
+
log.info({ a: 1, b: 2, c: 3, array: [4, 5, 6], obj: { foo: 'foo', bar: "bar" } });
|
|
10
10
|
// pass an Error object inplace of attributes
|
|
11
11
|
log.warn(new Error('Some error occured'));
|
|
12
12
|
// Error together with some other attributes and message
|
|
@@ -17,4 +17,11 @@ const sqlLog = log.child('sql', { req: 1 });
|
|
|
17
17
|
sqlLog.debug('connecting to database');
|
|
18
18
|
sqlLog.debug({ sql: 'SELECT max(id) FROM status' });
|
|
19
19
|
log.info(new Map([['a', 1], ['b', 2], ['c', 3]]));
|
|
20
|
+
const recursive = {};
|
|
21
|
+
recursive.self = recursive;
|
|
22
|
+
log.info({ recursive });
|
|
23
|
+
const err = new Error('Error with properties');
|
|
24
|
+
err.foo = 1;
|
|
25
|
+
err.bar = 2;
|
|
26
|
+
log.error(err);
|
|
20
27
|
//# sourceMappingURL=demo.js.map
|
package/lib/demo.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"demo.js","sourceRoot":"","sources":["../src/demo.ts"],"names":[],"mappings":";;AAAA,mCAAoC;AAEpC,MAAM,GAAG,GAAG,IAAA,oBAAY,EAAC,UAAU,CAAC,CAAA;AAEpC,GAAG,CAAC,IAAI,CAAC,4BAA4B,CAAC,CAAA;AACtC,GAAG,CAAC,KAAK,CAAC,6BAA6B,CAAC,CAAA;AAExC,GAAG,CAAC,IAAI,CAAC,EAAC,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAC,EAAE,wCAAwC,CAAC,CAAA;AAEpE,6CAA6C;AAC7C,GAAG,CAAC,IAAI,CAAC,EAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,
|
|
1
|
+
{"version":3,"file":"demo.js","sourceRoot":"","sources":["../src/demo.ts"],"names":[],"mappings":";;AAAA,mCAAoC;AAEpC,MAAM,GAAG,GAAG,IAAA,oBAAY,EAAC,UAAU,CAAC,CAAA;AAEpC,GAAG,CAAC,IAAI,CAAC,4BAA4B,CAAC,CAAA;AACtC,GAAG,CAAC,KAAK,CAAC,6BAA6B,CAAC,CAAA;AAExC,GAAG,CAAC,IAAI,CAAC,EAAC,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAC,EAAE,wCAAwC,CAAC,CAAA;AAEpE,6CAA6C;AAC7C,GAAG,CAAC,IAAI,CAAC,EAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE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
4
4
|
private hasColor;
|
|
5
5
|
private prefix?;
|
|
6
|
-
private
|
|
7
|
-
private style?;
|
|
8
|
-
private seenRecursion;
|
|
6
|
+
private color?;
|
|
9
7
|
constructor(out: (line: string) => void, hasColor: boolean);
|
|
8
|
+
private applyColor;
|
|
9
|
+
private style;
|
|
10
10
|
private line;
|
|
11
11
|
private text;
|
|
12
12
|
private begin;
|
|
13
13
|
private end;
|
|
14
14
|
private property;
|
|
15
15
|
print(rec: LogRecord): void;
|
|
16
|
+
private formatHead;
|
|
16
17
|
reset(): void;
|
|
17
18
|
}
|
|
18
19
|
export declare function prettyStderrSink(rec: LogRecord): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pretty.d.ts","sourceRoot":"","sources":["../../src/sinks/pretty.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"pretty.d.ts","sourceRoot":"","sources":["../../src/sinks/pretty.ts"],"names":[],"mappings":"AAKA,OAAO,EAAC,SAAS,EAAC,MAAM,WAAW,CAAA;AAGnC,qBAAa,OAAO;IAIJ,OAAO,CAAC,GAAG;IAA0B,OAAO,CAAC,QAAQ;IAHjE,OAAO,CAAC,MAAM,CAAC,CAAQ;IACvB,OAAO,CAAC,KAAK,CAAC,CAAQ;gBAEF,GAAG,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,EAAU,QAAQ,EAAE,OAAO;IAE1E,OAAO,CAAC,UAAU;IAQlB,OAAO,CAAC,KAAK;IAUb,OAAO,CAAC,IAAI;IASZ,OAAO,CAAC,IAAI;IAMZ,OAAO,CAAC,KAAK;IASb,OAAO,CAAC,GAAG;IAKX,OAAO,CAAC,QAAQ;IAmChB,KAAK,CAAC,GAAG,EAAE,SAAS;IA+BpB,OAAO,CAAC,UAAU;IAelB,KAAK,IAAI,IAAI;CAIhB;AAkED,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,SAAS,GAAG,IAAI,CAcrD"}
|
package/lib/sinks/pretty.js
CHANGED
|
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.prettyStderrSink = exports.Printer = void 0;
|
|
7
7
|
const util_internal_hex_1 = require("@subsquid/util-internal-hex");
|
|
8
|
+
const util_internal_json_1 = require("@subsquid/util-internal-json");
|
|
8
9
|
const assert_1 = __importDefault(require("assert"));
|
|
9
10
|
const supports_color_1 = require("supports-color");
|
|
10
11
|
const level_1 = require("../level");
|
|
@@ -12,13 +13,26 @@ class Printer {
|
|
|
12
13
|
constructor(out, hasColor) {
|
|
13
14
|
this.out = out;
|
|
14
15
|
this.hasColor = hasColor;
|
|
15
|
-
this.visited = new Set();
|
|
16
|
-
this.seenRecursion = false;
|
|
17
16
|
}
|
|
18
|
-
|
|
19
|
-
if (
|
|
20
|
-
|
|
17
|
+
applyColor(s) {
|
|
18
|
+
if (this.hasColor && this.color && s) {
|
|
19
|
+
return `\u001b[${this.color}m${s}\u001b[0m`;
|
|
21
20
|
}
|
|
21
|
+
else {
|
|
22
|
+
return s;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
style(style, s) {
|
|
26
|
+
if (this.hasColor && style && s) {
|
|
27
|
+
s = style + s + '\u001b[0m';
|
|
28
|
+
if (this.color) {
|
|
29
|
+
s = `\u001b[0m` + s + `\u001b[${this.color}m`;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return s;
|
|
33
|
+
}
|
|
34
|
+
line(s) {
|
|
35
|
+
s = this.applyColor(s);
|
|
22
36
|
if (this.prefix) {
|
|
23
37
|
this.out(this.prefix.prepend(s));
|
|
24
38
|
}
|
|
@@ -33,10 +47,7 @@ class Printer {
|
|
|
33
47
|
}
|
|
34
48
|
begin(prefix, width) {
|
|
35
49
|
width = width == null ? prefix.length : width;
|
|
36
|
-
|
|
37
|
-
prefix = this.style.open + prefix + this.style.close;
|
|
38
|
-
}
|
|
39
|
-
this.prefix = new Prefix(prefix, width, this.prefix);
|
|
50
|
+
this.prefix = new Prefix(this.applyColor(prefix), width, this.prefix);
|
|
40
51
|
}
|
|
41
52
|
end() {
|
|
42
53
|
(0, assert_1.default)(this.prefix != null);
|
|
@@ -65,81 +76,28 @@ class Printer {
|
|
|
65
76
|
else if (typeof val?.toJSON == 'function') {
|
|
66
77
|
this.property(prefix, val.toJSON());
|
|
67
78
|
}
|
|
68
|
-
else if (val instanceof Map) {
|
|
69
|
-
let entries = [];
|
|
70
|
-
for (let [k, v] of val.entries()) {
|
|
71
|
-
entries.push({ k, v });
|
|
72
|
-
}
|
|
73
|
-
this.property(prefix, { map: entries });
|
|
74
|
-
}
|
|
75
|
-
else if (val instanceof Set) {
|
|
76
|
-
this.property(prefix, { set: [...val] });
|
|
77
|
-
}
|
|
78
|
-
else if (Array.isArray(val)) {
|
|
79
|
-
if (val.length == 0) {
|
|
80
|
-
this.line(`${prefix} []`);
|
|
81
|
-
}
|
|
82
|
-
else {
|
|
83
|
-
if (this.visited.has(val)) {
|
|
84
|
-
this.seenRecursion = true;
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
else {
|
|
88
|
-
this.visited.add(val);
|
|
89
|
-
}
|
|
90
|
-
this.line(prefix);
|
|
91
|
-
for (let item of val) {
|
|
92
|
-
this.property(' -', item);
|
|
93
|
-
}
|
|
94
|
-
this.visited.delete(val);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
79
|
else if (val == null) {
|
|
98
80
|
this.line(`${prefix} null`);
|
|
99
81
|
}
|
|
100
82
|
else {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
}
|
|
105
|
-
else {
|
|
106
|
-
this.visited.add(val);
|
|
107
|
-
}
|
|
108
|
-
let props;
|
|
109
|
-
if (val instanceof Error) {
|
|
110
|
-
props = { ...val, stack: val.stack || val.toString() };
|
|
111
|
-
}
|
|
112
|
-
else {
|
|
113
|
-
props = val;
|
|
83
|
+
let text;
|
|
84
|
+
try {
|
|
85
|
+
text = JSON.stringify((0, util_internal_json_1.toJSON)(val));
|
|
114
86
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
if (!has) {
|
|
118
|
-
if (prefix == ' -') {
|
|
119
|
-
this.begin(prefix);
|
|
120
|
-
}
|
|
121
|
-
else {
|
|
122
|
-
this.line(prefix);
|
|
123
|
-
this.begin(' ');
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
has = true;
|
|
127
|
-
this.property(key + ':', props[key]);
|
|
128
|
-
}
|
|
129
|
-
if (has) {
|
|
130
|
-
this.end();
|
|
87
|
+
catch (e) {
|
|
88
|
+
text = this.style('\u001b[31m', `failed to serialize logged value: ${e}`);
|
|
131
89
|
}
|
|
132
|
-
this.
|
|
90
|
+
this.line(`${prefix} ${text}`);
|
|
133
91
|
}
|
|
134
92
|
break;
|
|
135
93
|
}
|
|
136
94
|
}
|
|
137
95
|
print(rec) {
|
|
138
|
-
this.begin(formatHead(rec
|
|
96
|
+
this.begin(this.formatHead(rec), 14 + (rec.ns ? rec.ns.length + 1 : 0));
|
|
139
97
|
if (rec.msg) {
|
|
140
98
|
this.text(rec.msg);
|
|
141
99
|
}
|
|
142
|
-
this.
|
|
100
|
+
this.color = '2'; // dim
|
|
143
101
|
if (rec.err instanceof Error) {
|
|
144
102
|
this.text(rec.err.stack || rec.err.toString());
|
|
145
103
|
}
|
|
@@ -153,8 +111,10 @@ class Printer {
|
|
|
153
111
|
default:
|
|
154
112
|
if (key == 'err' && rec.err instanceof Error) {
|
|
155
113
|
// already printed the stack trace above
|
|
156
|
-
// print only the rest of props
|
|
157
|
-
|
|
114
|
+
// print only the rest of props and indented like the stack trace
|
|
115
|
+
for (let k in rec.err) {
|
|
116
|
+
this.property(` ${k}:`, rec.err[k]);
|
|
117
|
+
}
|
|
158
118
|
}
|
|
159
119
|
else {
|
|
160
120
|
this.property(key + ':', rec[key]);
|
|
@@ -162,39 +122,27 @@ class Printer {
|
|
|
162
122
|
}
|
|
163
123
|
}
|
|
164
124
|
this.end();
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
});
|
|
125
|
+
}
|
|
126
|
+
formatHead(rec) {
|
|
127
|
+
let time = formatTime(rec.time);
|
|
128
|
+
let level = level_1.LogLevel[rec.level].padEnd(5, ' ');
|
|
129
|
+
let ns = rec.ns;
|
|
130
|
+
if (this.hasColor) {
|
|
131
|
+
level = `\u001b[1m\u001b[${getLevelColor(rec.level)}m${level}\u001b[0m`;
|
|
132
|
+
ns = `\u001b[1m\u001b[34m${ns}\u001b[0m`;
|
|
174
133
|
}
|
|
134
|
+
let head = time + ' ' + level;
|
|
135
|
+
if (rec.ns) {
|
|
136
|
+
head += ' ' + ns;
|
|
137
|
+
}
|
|
138
|
+
return head;
|
|
175
139
|
}
|
|
176
140
|
reset() {
|
|
177
|
-
this.visited.clear();
|
|
178
141
|
this.prefix = undefined;
|
|
179
|
-
this.
|
|
180
|
-
this.seenRecursion = false;
|
|
142
|
+
this.color = undefined;
|
|
181
143
|
}
|
|
182
144
|
}
|
|
183
145
|
exports.Printer = Printer;
|
|
184
|
-
function formatHead(rec, withColor) {
|
|
185
|
-
let time = formatTime(rec.time);
|
|
186
|
-
let level = level_1.LogLevel[rec.level].padEnd(5, ' ');
|
|
187
|
-
let ns = rec.ns;
|
|
188
|
-
if (withColor) {
|
|
189
|
-
level = `\u001b[1m\u001b[${getLevelColor(rec.level)}m${level}\u001b[0m`;
|
|
190
|
-
ns = `\u001b[1m\u001b[34m${ns}\u001b[0m`;
|
|
191
|
-
}
|
|
192
|
-
let head = time + ' ' + level;
|
|
193
|
-
if (rec.ns) {
|
|
194
|
-
head += ' ' + ns;
|
|
195
|
-
}
|
|
196
|
-
return head;
|
|
197
|
-
}
|
|
198
146
|
function getLevelColor(level) {
|
|
199
147
|
switch (level) {
|
|
200
148
|
case level_1.LogLevel.TRACE:
|
package/lib/sinks/pretty.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pretty.js","sourceRoot":"","sources":["../../src/sinks/pretty.ts"],"names":[],"mappings":";;;;;;AAAA,mEAAiD;AACjD,oDAA2B;AAC3B,mDAAoD;AACpD,oCAAiC;AAIjC,MAAa,OAAO;
|
|
1
|
+
{"version":3,"file":"pretty.js","sourceRoot":"","sources":["../../src/sinks/pretty.ts"],"names":[],"mappings":";;;;;;AAAA,mEAAiD;AACjD,qEAAmD;AACnD,oDAA2B;AAC3B,mDAAoD;AACpD,oCAAiC;AAIjC,MAAa,OAAO;IAIhB,YAAoB,GAA2B,EAAU,QAAiB;QAAtD,QAAG,GAAH,GAAG,CAAwB;QAAU,aAAQ,GAAR,QAAQ,CAAS;IAAG,CAAC;IAEtE,UAAU,CAAC,CAAS;QACxB,IAAI,IAAI,CAAC,QAAQ,IAAI,IAAI,CAAC,KAAK,IAAI,CAAC,EAAE;YAClC,OAAO,UAAU,IAAI,CAAC,KAAK,IAAI,CAAC,WAAW,CAAA;SAC9C;aAAM;YACH,OAAO,CAAC,CAAA;SACX;IACL,CAAC;IAEO,KAAK,CAAC,KAAa,EAAE,CAAS;QAClC,IAAI,IAAI,CAAC,QAAQ,IAAI,KAAK,IAAI,CAAC,EAAE;YAC7B,CAAC,GAAG,KAAK,GAAG,CAAC,GAAG,WAAW,CAAA;YAC3B,IAAI,IAAI,CAAC,KAAK,EAAE;gBACZ,CAAC,GAAG,WAAW,GAAG,CAAC,GAAG,UAAU,IAAI,CAAC,KAAK,GAAG,CAAA;aAChD;SACJ;QACD,OAAO,CAAC,CAAA;IACZ,CAAC;IAEO,IAAI,CAAC,CAAS;QAClB,CAAC,GAAG,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAA;QACtB,IAAI,IAAI,CAAC,MAAM,EAAE;YACb,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAA;S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IAAI,EAAE,IAAI,CAAC,GAAG,EAAE;YAChB,GAAG,EAAE,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,QAAQ,EAAE;SAC/B,CAAC,CAAA;KACL;YAAS;QACN,OAAO,CAAC,KAAK,EAAE,CAAA;KAClB;AACL,CAAC;AAdD,4CAcC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@subsquid/logger",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Lightweight library for structured logging",
|
|
5
5
|
"license": "GPL-3.0-or-later",
|
|
6
6
|
"repository": "git@github.com:subsquid/squid.git",
|
|
@@ -29,5 +29,5 @@
|
|
|
29
29
|
"build": "rm -rf lib && tsc",
|
|
30
30
|
"test": "mocha lib/**/*.test.js"
|
|
31
31
|
},
|
|
32
|
-
"readme": "# @subsquid/logger\n\nLightweight structured logger for squid framework.\n\n## Usage\n\n```typescript\nimport {createLogger} from \"@subsquid/logger\"\n\nconst log = createLogger('sqd:demo')\n\nlog.info('message with severity info')\nlog.debug('message with severity debug')\n\nlog.info({foo: 1, bar: 2}, 'message and some additional attributes')\n\n// info message consisting only of attributes\nlog.info({a: 1, b: 2, c: 3, array: [4, 5]}) \n\n// pass an Error object inplace of attributes\nlog.warn(new Error('Some error occured'))\n\n// Error together with some other attributes and message\nlog.error({err: new Error('Another error'), a: 1, b: 2}, 'weird')\n\n// create a child logger instance with namespace `sqd:demo:sql` \n// and `req: 1` attribute attached to every log record\nconst sqlLog = log.child('sql', {req: 1})\nsqlLog.debug('connecting to database')\nsqlLog.debug({sql: 'SELECT max(id) FROM status'})\n```\n\n## Configuration\n\nThere are 6 log levels available: `TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR`, `FATAL`.\n\nThe log level of each logger instance is solely determined by its namespace and specified\nvia set of `SQD_TRACE`, ..., `SQD_FATAL` environment variables.\nThe default log level is `INFO`. \n\nUsers override the default log level by setting in appropriate \nenvironment variable a pattern which matches the logger's namespace.\n\n* `SQD_DEBUG=*` - sets the log level to `DEBUG` for all loggers.\n* `SQD_DEBUG=foo` - loggers `foo`, `foo:bar`, `foo:a:b`, etc will have a `DEBUG` level.\n* `SQD_DEBUG=a:b*:c,d` - loggers `a:b:c`, `a:baz:c`, `a:baz:c:foo`, `d`, etc will have a `DEBUG` level, logger `a:z:c` will not.\n\nWhen logger is matched by multiple `SQD_*` variables, the match with the highest specificity wins.\nWhen specificities are equal, the most verbose matched log level will be effective.\n\nSpecificity is computed as a number of namespace characters \nwhich are not matched by wildcards and which are not part of a child namespace.\nFor example, given a pattern `foo*bar`, the specificity of `foo:bar:baz:qux`, `foobar` and `foobazbar` is `6`.\n\n## Output\n\nThe logger always writes to `stderr`. \n\nWhen `stderr` is connected to a terminal, log records will be pretty printed.\n\n\n\nOtherwise, log records will be written as JSON lines.\n\n```\n{\"level\":2,\"time\":
|
|
32
|
+
"readme": "# @subsquid/logger\n\nLightweight structured logger for squid framework.\n\n## Usage\n\n```typescript\nimport {createLogger} from \"@subsquid/logger\"\n\nconst log = createLogger('sqd:demo')\n\nlog.info('message with severity info')\nlog.debug('message with severity debug')\n\nlog.info({foo: 1, bar: 2}, 'message and some additional attributes')\n\n// info message consisting only of attributes\nlog.info({a: 1, b: 2, c: 3, array: [4, 5], obj: {foo: 'foo', bar: \"bar\"}}) \n\n// pass an Error object inplace of attributes\nlog.warn(new Error('Some error occured'))\n\n// Error together with some other attributes and message\nlog.error({err: new Error('Another error'), a: 1, b: 2}, 'weird')\n\n// create a child logger instance with namespace `sqd:demo:sql` \n// and `req: 1` attribute attached to every log record\nconst sqlLog = log.child('sql', {req: 1})\nsqlLog.debug('connecting to database')\nsqlLog.debug({sql: 'SELECT max(id) FROM status'})\n```\n\n## Configuration\n\nThere are 6 log levels available: `TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR`, `FATAL`.\n\nThe log level of each logger instance is solely determined by its namespace and specified\nvia set of `SQD_TRACE`, ..., `SQD_FATAL` environment variables.\nThe default log level is `INFO`. \n\nUsers override the default log level by setting in appropriate \nenvironment variable a pattern which matches the logger's namespace.\n\n* `SQD_DEBUG=*` - sets the log level to `DEBUG` for all loggers.\n* `SQD_DEBUG=foo` - loggers `foo`, `foo:bar`, `foo:a:b`, etc will have a `DEBUG` level.\n* `SQD_DEBUG=a:b*:c,d` - loggers `a:b:c`, `a:baz:c`, `a:baz:c:foo`, `d`, etc will have a `DEBUG` level, logger `a:z:c` will not.\n\nWhen logger is matched by multiple `SQD_*` variables, the match with the highest specificity wins.\nWhen specificities are equal, the most verbose matched log level will be effective.\n\nSpecificity is computed as a number of namespace characters \nwhich are not matched by wildcards and which are not part of a child namespace.\nFor example, given a pattern `foo*bar`, the specificity of `foo:bar:baz:qux`, `foobar` and `foobazbar` is `6`.\n\n## Output\n\nThe logger always writes to `stderr`. \n\nWhen `stderr` is connected to a terminal, log records will be pretty printed.\n\n\n\nOtherwise, log records will be written as JSON lines.\n\n```\n{\"level\":2,\"time\":1669387525765,\"ns\":\"sqd:demo\",\"msg\":\"message with severity info\"}\n{\"level\":2,\"time\":1669387525766,\"ns\":\"sqd:demo\",\"msg\":\"message and some additional attributes\",\"foo\":1,\"bar\":2}\n{\"level\":2,\"time\":1669387525766,\"ns\":\"sqd:demo\",\"a\":1,\"b\":2,\"c\":3,\"array\":[4,5,6],\"obj\":{\"foo\":\"foo\",\"bar\":\"bar\"}}\n{\"level\":3,\"time\":1669387525766,\"ns\":\"sqd:demo\",\"err\":{\"stack\":\"Error: Some error occured\\n at Object.<anonymous> (/Users/eldar/dev/squid/util/logger/lib/demo.js:11:10)\\n at Module._compile (node:internal/modules/cjs/loader:1159:14)\\n at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)\\n at Module.load (node:internal/modules/cjs/loader:1037:32)\\n at Module._load (node:internal/modules/cjs/loader:878:12)\\n at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)\\n at node:internal/main/run_main_module:23:47\"}}\n{\"level\":4,\"time\":1669387525766,\"ns\":\"sqd:demo\",\"msg\":\"weird\",\"err\":{\"stack\":\"Error: Another error\\n at Object.<anonymous> (/Users/eldar/dev/squid/util/logger/lib/demo.js:13:18)\\n at Module._compile (node:internal/modules/cjs/loader:1159:14)\\n at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)\\n at Module.load (node:internal/modules/cjs/loader:1037:32)\\n at Module._load (node:internal/modules/cjs/loader:878:12)\\n at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)\\n at node:internal/main/run_main_module:23:47\"},\"a\":1,\"b\":2}\n```\n"
|
|
33
33
|
}
|
package/src/demo.ts
CHANGED
|
@@ -8,7 +8,7 @@ log.debug('message with severity debug')
|
|
|
8
8
|
log.info({foo: 1, bar: 2}, 'message and some additional attributes')
|
|
9
9
|
|
|
10
10
|
// info message consisting only of attributes
|
|
11
|
-
log.info({a: 1, b: 2, c: 3, array: [4, 5,
|
|
11
|
+
log.info({a: 1, b: 2, c: 3, array: [4, 5, 6], obj: {foo: 'foo', bar: "bar"}})
|
|
12
12
|
|
|
13
13
|
// pass an Error object inplace of attributes
|
|
14
14
|
log.warn(new Error('Some error occured'))
|
|
@@ -23,3 +23,14 @@ sqlLog.debug('connecting to database')
|
|
|
23
23
|
sqlLog.debug({sql: 'SELECT max(id) FROM status'})
|
|
24
24
|
|
|
25
25
|
log.info(new Map([['a', 1], ['b', 2], ['c', 3]]))
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
const recursive: any = {}
|
|
29
|
+
recursive.self = recursive
|
|
30
|
+
|
|
31
|
+
log.info({recursive})
|
|
32
|
+
|
|
33
|
+
const err: any = new Error('Error with properties')
|
|
34
|
+
err.foo = 1
|
|
35
|
+
err.bar = 2
|
|
36
|
+
log.error(err)
|
package/src/sinks/pretty.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {toHex} from "@subsquid/util-internal-hex"
|
|
2
|
+
import {toJSON} from '@subsquid/util-internal-json'
|
|
2
3
|
import assert from "assert"
|
|
3
4
|
import {stderr as stderrColor} from "supports-color"
|
|
4
5
|
import {LogLevel} from "../level"
|
|
@@ -7,16 +8,30 @@ import {LogRecord} from "../logger"
|
|
|
7
8
|
|
|
8
9
|
export class Printer {
|
|
9
10
|
private prefix?: Prefix
|
|
10
|
-
private
|
|
11
|
-
private style?: {open: string, close: string}
|
|
12
|
-
private seenRecursion = false
|
|
11
|
+
private color?: string
|
|
13
12
|
|
|
14
13
|
constructor(private out: (line: string) => void, private hasColor: boolean) {}
|
|
15
14
|
|
|
16
|
-
private
|
|
17
|
-
if (
|
|
18
|
-
|
|
15
|
+
private applyColor(s: string): string {
|
|
16
|
+
if (this.hasColor && this.color && s) {
|
|
17
|
+
return `\u001b[${this.color}m${s}\u001b[0m`
|
|
18
|
+
} else {
|
|
19
|
+
return s
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
private style(style: string, s: string): string {
|
|
24
|
+
if (this.hasColor && style && s) {
|
|
25
|
+
s = style + s + '\u001b[0m'
|
|
26
|
+
if (this.color) {
|
|
27
|
+
s = `\u001b[0m` + s + `\u001b[${this.color}m`
|
|
28
|
+
}
|
|
19
29
|
}
|
|
30
|
+
return s
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
private line(s: string): void {
|
|
34
|
+
s = this.applyColor(s)
|
|
20
35
|
if (this.prefix) {
|
|
21
36
|
this.out(this.prefix.prepend(s))
|
|
22
37
|
} else {
|
|
@@ -32,10 +47,11 @@ export class Printer {
|
|
|
32
47
|
|
|
33
48
|
private begin(prefix: string, width?: number): void {
|
|
34
49
|
width = width == null ? prefix.length : width
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
50
|
+
this.prefix = new Prefix(
|
|
51
|
+
this.applyColor(prefix),
|
|
52
|
+
width,
|
|
53
|
+
this.prefix
|
|
54
|
+
)
|
|
39
55
|
}
|
|
40
56
|
|
|
41
57
|
private end(): void {
|
|
@@ -63,73 +79,27 @@ export class Printer {
|
|
|
63
79
|
this.line(`${prefix} ${val}`)
|
|
64
80
|
} else if (typeof (val as any)?.toJSON == 'function') {
|
|
65
81
|
this.property(prefix, (val as any).toJSON())
|
|
66
|
-
} else if (val instanceof Map) {
|
|
67
|
-
let entries: {k: unknown, v: unknown}[] = []
|
|
68
|
-
for (let [k, v] of val.entries()) {
|
|
69
|
-
entries.push({k, v})
|
|
70
|
-
}
|
|
71
|
-
this.property(prefix, {map: entries})
|
|
72
|
-
} else if (val instanceof Set) {
|
|
73
|
-
this.property(prefix, {set: [...val]})
|
|
74
|
-
} else if (Array.isArray(val)) {
|
|
75
|
-
if (val.length == 0) {
|
|
76
|
-
this.line(`${prefix} []`)
|
|
77
|
-
} else {
|
|
78
|
-
if (this.visited.has(val)) {
|
|
79
|
-
this.seenRecursion = true
|
|
80
|
-
return
|
|
81
|
-
} else {
|
|
82
|
-
this.visited.add(val)
|
|
83
|
-
}
|
|
84
|
-
this.line(prefix)
|
|
85
|
-
for (let item of val) {
|
|
86
|
-
this.property(' -', item)
|
|
87
|
-
}
|
|
88
|
-
this.visited.delete(val)
|
|
89
|
-
}
|
|
90
82
|
} else if (val == null) {
|
|
91
83
|
this.line(`${prefix} null`)
|
|
92
84
|
} else {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
97
|
-
this.
|
|
98
|
-
}
|
|
99
|
-
let props: any
|
|
100
|
-
if (val instanceof Error) {
|
|
101
|
-
props = {...val, stack: val.stack || val.toString()}
|
|
102
|
-
} else {
|
|
103
|
-
props = val
|
|
104
|
-
}
|
|
105
|
-
let has = false
|
|
106
|
-
for (let key in props) {
|
|
107
|
-
if (!has) {
|
|
108
|
-
if (prefix == ' -') {
|
|
109
|
-
this.begin(prefix)
|
|
110
|
-
} else {
|
|
111
|
-
this.line(prefix)
|
|
112
|
-
this.begin(' ')
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
has = true
|
|
116
|
-
this.property(key + ':', props[key])
|
|
117
|
-
}
|
|
118
|
-
if (has) {
|
|
119
|
-
this.end()
|
|
85
|
+
let text
|
|
86
|
+
try {
|
|
87
|
+
text = JSON.stringify(toJSON(val))
|
|
88
|
+
} catch(e: any) {
|
|
89
|
+
text = this.style('\u001b[31m', `failed to serialize logged value: ${e}`)
|
|
120
90
|
}
|
|
121
|
-
this.
|
|
91
|
+
this.line(`${prefix} ${text}`)
|
|
122
92
|
}
|
|
123
93
|
break
|
|
124
94
|
}
|
|
125
95
|
}
|
|
126
96
|
|
|
127
97
|
print(rec: LogRecord) {
|
|
128
|
-
this.begin(formatHead(rec
|
|
98
|
+
this.begin(this.formatHead(rec), 14 + (rec.ns ? rec.ns.length + 1 : 0))
|
|
129
99
|
if (rec.msg) {
|
|
130
100
|
this.text(rec.msg)
|
|
131
101
|
}
|
|
132
|
-
this.
|
|
102
|
+
this.color = '2' // dim
|
|
133
103
|
if (rec.err instanceof Error) {
|
|
134
104
|
this.text(rec.err.stack || rec.err.toString())
|
|
135
105
|
}
|
|
@@ -143,48 +113,37 @@ export class Printer {
|
|
|
143
113
|
default:
|
|
144
114
|
if (key == 'err' && rec.err instanceof Error) {
|
|
145
115
|
// already printed the stack trace above
|
|
146
|
-
// print only the rest of props
|
|
147
|
-
|
|
116
|
+
// print only the rest of props and indented like the stack trace
|
|
117
|
+
for (let k in rec.err) {
|
|
118
|
+
this.property(` ${k}:`, (rec.err as any)[k])
|
|
119
|
+
}
|
|
148
120
|
} else {
|
|
149
121
|
this.property(key + ':', (rec as any)[key])
|
|
150
122
|
}
|
|
151
123
|
}
|
|
152
124
|
}
|
|
153
125
|
this.end()
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
private formatHead(rec: LogRecord): string {
|
|
129
|
+
let time = formatTime(rec.time)
|
|
130
|
+
let level = LogLevel[rec.level].padEnd(5, ' ')
|
|
131
|
+
let ns = rec.ns
|
|
132
|
+
if (this.hasColor) {
|
|
133
|
+
level = `\u001b[1m\u001b[${getLevelColor(rec.level)}m${level}\u001b[0m`
|
|
134
|
+
ns = `\u001b[1m\u001b[34m${ns}\u001b[0m`
|
|
135
|
+
}
|
|
136
|
+
let head = time + ' ' + level
|
|
137
|
+
if (rec.ns) {
|
|
138
|
+
head += ' ' + ns
|
|
163
139
|
}
|
|
140
|
+
return head
|
|
164
141
|
}
|
|
165
142
|
|
|
166
143
|
reset(): void {
|
|
167
|
-
this.visited.clear()
|
|
168
144
|
this.prefix = undefined
|
|
169
|
-
this.
|
|
170
|
-
this.seenRecursion = false
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
function formatHead(rec: LogRecord, withColor?: boolean): string {
|
|
176
|
-
let time = formatTime(rec.time)
|
|
177
|
-
let level = LogLevel[rec.level].padEnd(5, ' ')
|
|
178
|
-
let ns = rec.ns
|
|
179
|
-
if (withColor) {
|
|
180
|
-
level = `\u001b[1m\u001b[${getLevelColor(rec.level)}m${level}\u001b[0m`
|
|
181
|
-
ns = `\u001b[1m\u001b[34m${ns}\u001b[0m`
|
|
182
|
-
}
|
|
183
|
-
let head = time + ' ' + level
|
|
184
|
-
if (rec.ns) {
|
|
185
|
-
head += ' ' + ns
|
|
145
|
+
this.color = undefined
|
|
186
146
|
}
|
|
187
|
-
return head
|
|
188
147
|
}
|
|
189
148
|
|
|
190
149
|
|