@percy/logger 1.0.7 → 1.1.1
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/dist/index.js +16 -11
- package/dist/logger.js +7 -124
- package/package.json +3 -16
- package/test/helpers.js +14 -16
- package/dist/browser.js +0 -19
- package/dist/bundle.js +0 -445
- package/test/client.js +0 -177
package/dist/index.js
CHANGED
|
@@ -1,23 +1,28 @@
|
|
|
1
|
-
import Logger from '
|
|
1
|
+
import Logger from './logger.js';
|
|
2
2
|
export function logger(name) {
|
|
3
3
|
return new Logger().group(name);
|
|
4
4
|
}
|
|
5
|
-
Object.assign(logger, {
|
|
6
|
-
format: (...args) => new Logger().format(...args),
|
|
7
|
-
query: (...args) => new Logger().query(...args),
|
|
8
|
-
connect: (...args) => new Logger().connect(...args),
|
|
9
|
-
remote: (...args) => new Logger().remote(...args),
|
|
10
|
-
loglevel: (...args) => new Logger().loglevel(...args)
|
|
11
|
-
});
|
|
12
5
|
Object.defineProperties(logger, {
|
|
13
|
-
Logger: {
|
|
14
|
-
get: () => Logger
|
|
15
|
-
},
|
|
16
6
|
stdout: {
|
|
17
7
|
get: () => Logger.stdout
|
|
18
8
|
},
|
|
19
9
|
stderr: {
|
|
20
10
|
get: () => Logger.stderr
|
|
11
|
+
},
|
|
12
|
+
constructor: {
|
|
13
|
+
get: () => Logger
|
|
14
|
+
},
|
|
15
|
+
instance: {
|
|
16
|
+
get: () => new Logger()
|
|
17
|
+
},
|
|
18
|
+
query: {
|
|
19
|
+
value: (...args) => logger.instance.query(...args)
|
|
20
|
+
},
|
|
21
|
+
format: {
|
|
22
|
+
value: (...args) => logger.instance.format(...args)
|
|
23
|
+
},
|
|
24
|
+
loglevel: {
|
|
25
|
+
value: (...args) => logger.instance.loglevel(...args)
|
|
21
26
|
}
|
|
22
27
|
});
|
|
23
28
|
export default logger;
|
package/dist/logger.js
CHANGED
|
@@ -158,40 +158,17 @@ export class PercyLogger {
|
|
|
158
158
|
if (this.deprecations.has(message)) return;
|
|
159
159
|
this.deprecations.add(message);
|
|
160
160
|
this.log(debug, 'warn', `Warning: ${message}`, meta);
|
|
161
|
-
} // Returns true if a socket is present and ready
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
get isRemote() {
|
|
165
|
-
var _this$socket;
|
|
166
|
-
|
|
167
|
-
return ((_this$socket = this.socket) === null || _this$socket === void 0 ? void 0 : _this$socket.readyState) === 1;
|
|
168
161
|
} // Generic log method accepts a debug group, log level, log message, and optional meta
|
|
169
162
|
// information to store with the message and other info
|
|
170
163
|
|
|
171
164
|
|
|
172
165
|
log(debug, level, message, meta = {}) {
|
|
173
|
-
// message might be an error object
|
|
174
|
-
let
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
if (this.isRemote) {
|
|
178
|
-
// serialize error messages
|
|
179
|
-
message = isError && 'stack' in error ? {
|
|
180
|
-
message: error.message,
|
|
181
|
-
stack: error.stack
|
|
182
|
-
} : message;
|
|
183
|
-
return this.socket.send(JSON.stringify({
|
|
184
|
-
log: [debug, level, message, {
|
|
185
|
-
remote: true,
|
|
186
|
-
...meta
|
|
187
|
-
}]
|
|
188
|
-
}));
|
|
189
|
-
} // ensure the message is a string
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
message = isError && message.stack || message.message || message.toString(); // timestamp each log
|
|
166
|
+
// message might be an error-like object
|
|
167
|
+
let err = typeof message !== 'string' && (level === 'debug' || level === 'error');
|
|
168
|
+
err && (err = message.message ? Error.prototype.toString.call(message) : message.toString()); // save log entries
|
|
193
169
|
|
|
194
170
|
let timestamp = Date.now();
|
|
171
|
+
message = err ? message.stack || err : message.toString();
|
|
195
172
|
let entry = {
|
|
196
173
|
debug,
|
|
197
174
|
level,
|
|
@@ -202,9 +179,9 @@ export class PercyLogger {
|
|
|
202
179
|
this.messages.add(entry); // maybe write the message to stdio
|
|
203
180
|
|
|
204
181
|
if (this.shouldLog(debug, level)) {
|
|
182
|
+
if (err && this.level !== 'debug') message = err;
|
|
205
183
|
let elapsed = timestamp - (this.lastlog || timestamp);
|
|
206
|
-
|
|
207
|
-
this.write(level, this.format(debug, error ? 'error' : level, message, elapsed));
|
|
184
|
+
this.write(level, this.format(debug, err ? 'error' : level, message, elapsed));
|
|
208
185
|
this.lastlog = timestamp;
|
|
209
186
|
}
|
|
210
187
|
} // Writes a message to stdio based on the loglevel
|
|
@@ -218,103 +195,9 @@ export class PercyLogger {
|
|
|
218
195
|
stderr
|
|
219
196
|
} = this.constructor;
|
|
220
197
|
let progress = stdout.isTTY && this._progress;
|
|
221
|
-
|
|
222
|
-
if (progress) {
|
|
223
|
-
stdout.cursorTo(0);
|
|
224
|
-
stdout.clearLine();
|
|
225
|
-
}
|
|
226
|
-
|
|
198
|
+
if (progress) stdout.cursorTo(0).clearLine();
|
|
227
199
|
(level === 'info' ? stdout : stderr).write(message + '\n');
|
|
228
200
|
if (!((_this$_progress = this._progress) !== null && _this$_progress !== void 0 && _this$_progress.persist)) delete this._progress;else if (progress) stdout.write(progress.message);
|
|
229
|
-
} // Opens a socket logging connection
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
connect(socket) {
|
|
233
|
-
// send logging environment info
|
|
234
|
-
let PERCY_DEBUG = process.env.PERCY_DEBUG;
|
|
235
|
-
let PERCY_LOGLEVEL = process.env.PERCY_LOGLEVEL || this.loglevel();
|
|
236
|
-
socket.send(JSON.stringify({
|
|
237
|
-
env: {
|
|
238
|
-
PERCY_DEBUG,
|
|
239
|
-
PERCY_LOGLEVEL
|
|
240
|
-
}
|
|
241
|
-
})); // attach remote logging handler
|
|
242
|
-
|
|
243
|
-
socket.onmessage = ({
|
|
244
|
-
data
|
|
245
|
-
}) => {
|
|
246
|
-
let {
|
|
247
|
-
log,
|
|
248
|
-
logAll
|
|
249
|
-
} = JSON.parse(data);
|
|
250
|
-
if (logAll) logAll.forEach(e => this.messages.add(e));
|
|
251
|
-
if (log) this.log(...log);
|
|
252
|
-
}; // return a cleanup function
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
return () => {
|
|
256
|
-
socket.onmessage = null;
|
|
257
|
-
};
|
|
258
|
-
} // Connects to a remote logger
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
async remote(createSocket, timeout = 1000) {
|
|
262
|
-
if (this.isRemote) return; // if not already connected, wait until the timeout
|
|
263
|
-
|
|
264
|
-
let err = await new Promise(resolve => {
|
|
265
|
-
let done = event => {
|
|
266
|
-
if (timeoutid == null) return;
|
|
267
|
-
timeoutid = clearTimeout(timeoutid);
|
|
268
|
-
if (this.socket) this.socket.onopen = this.socket.onerror = null;
|
|
269
|
-
resolve((event === null || event === void 0 ? void 0 : event.error) || (event === null || event === void 0 ? void 0 : event.type) === 'error' && 'Error: Socket connection failed');
|
|
270
|
-
};
|
|
271
|
-
|
|
272
|
-
let timeoutid = setTimeout(done, timeout, {
|
|
273
|
-
error: 'Error: Socket connection timed out'
|
|
274
|
-
});
|
|
275
|
-
Promise.resolve().then(async () => {
|
|
276
|
-
this.socket = await createSocket();
|
|
277
|
-
if (this.isRemote) return done();
|
|
278
|
-
this.socket.onopen = this.socket.onerror = done;
|
|
279
|
-
}).catch(error => done({
|
|
280
|
-
error
|
|
281
|
-
}));
|
|
282
|
-
}); // there was an error connecting, will fallback to normal logging
|
|
283
|
-
|
|
284
|
-
if (err) {
|
|
285
|
-
this.log('logger', 'debug', 'Unable to connect to remote logger');
|
|
286
|
-
this.log('logger', 'debug', err);
|
|
287
|
-
return;
|
|
288
|
-
} // send any messages already logged in this environment
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
if (this.messages.size) {
|
|
292
|
-
this.socket.send(JSON.stringify({
|
|
293
|
-
logAll: Array.from(this.messages).map(entry => ({ ...entry,
|
|
294
|
-
meta: {
|
|
295
|
-
remote: true,
|
|
296
|
-
...entry.meta
|
|
297
|
-
}
|
|
298
|
-
}))
|
|
299
|
-
}));
|
|
300
|
-
} // attach an incoming message handler
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
this.socket.onmessage = ({
|
|
304
|
-
data
|
|
305
|
-
}) => {
|
|
306
|
-
let {
|
|
307
|
-
env
|
|
308
|
-
} = JSON.parse(data); // update local environment info
|
|
309
|
-
|
|
310
|
-
if (env) Object.assign(process.env, env);
|
|
311
|
-
}; // return a cleanup function
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
return () => {
|
|
315
|
-
this.socket.onmessage = null;
|
|
316
|
-
this.socket = null;
|
|
317
|
-
};
|
|
318
201
|
}
|
|
319
202
|
|
|
320
203
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@percy/logger",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -15,11 +15,9 @@
|
|
|
15
15
|
},
|
|
16
16
|
"files": [
|
|
17
17
|
"dist",
|
|
18
|
-
"test/helpers.js"
|
|
19
|
-
"test/client.js"
|
|
18
|
+
"test/helpers.js"
|
|
20
19
|
],
|
|
21
20
|
"main": "./dist/index.js",
|
|
22
|
-
"browser": "./dist/bundle.js",
|
|
23
21
|
"type": "module",
|
|
24
22
|
"exports": {
|
|
25
23
|
".": "./dist/index.js",
|
|
@@ -27,22 +25,11 @@
|
|
|
27
25
|
"./test/helpers": "./test/helpers.js",
|
|
28
26
|
"./test/client": "./test/client.js"
|
|
29
27
|
},
|
|
30
|
-
"imports": {
|
|
31
|
-
"#logger": {
|
|
32
|
-
"node": "./dist/logger.js",
|
|
33
|
-
"default": "./dist/browser.js"
|
|
34
|
-
}
|
|
35
|
-
},
|
|
36
28
|
"scripts": {
|
|
37
29
|
"build": "node ../../scripts/build",
|
|
38
30
|
"lint": "eslint --ignore-path ../../.gitignore .",
|
|
39
31
|
"test": "node ../../scripts/test",
|
|
40
32
|
"test:coverage": "yarn test --coverage"
|
|
41
33
|
},
|
|
42
|
-
"
|
|
43
|
-
"output": {
|
|
44
|
-
"name": "PercyLogger"
|
|
45
|
-
}
|
|
46
|
-
},
|
|
47
|
-
"gitHead": "efd495c0343b8b82d3e6c236e858cdf2cb5e437d"
|
|
34
|
+
"gitHead": "d74af6a294f89fcb23c4ec598bb68a884ce47907"
|
|
48
35
|
}
|
package/test/helpers.js
CHANGED
|
@@ -33,25 +33,24 @@ function spy(object, method, func) {
|
|
|
33
33
|
return spy;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
const {
|
|
37
|
-
Logger,
|
|
38
|
-
loglevel
|
|
39
|
-
} = logger;
|
|
40
|
-
|
|
41
36
|
const helpers = {
|
|
42
37
|
stdout: [],
|
|
43
38
|
stderr: [],
|
|
44
|
-
loglevel,
|
|
39
|
+
loglevel: logger.loglevel,
|
|
40
|
+
|
|
41
|
+
get instance() {
|
|
42
|
+
return logger.instance;
|
|
43
|
+
},
|
|
45
44
|
|
|
46
45
|
async mock(options = {}) {
|
|
47
46
|
helpers.reset();
|
|
48
47
|
|
|
49
48
|
if (options.level) {
|
|
50
|
-
loglevel(options.level);
|
|
49
|
+
logger.loglevel(options.level);
|
|
51
50
|
}
|
|
52
51
|
|
|
53
52
|
if (process.env.__PERCY_BROWSERIFIED__) {
|
|
54
|
-
spy(
|
|
53
|
+
spy(logger.constructor.prototype, 'write', function(lvl, msg) {
|
|
55
54
|
let stdio = lvl === 'info' ? 'stdout' : 'stderr';
|
|
56
55
|
helpers[stdio].push(sanitizeLog(msg, options));
|
|
57
56
|
return this.write.originalValue.call(this, lvl, msg);
|
|
@@ -64,12 +63,11 @@ const helpers = {
|
|
|
64
63
|
let { Writable } = await import('stream');
|
|
65
64
|
|
|
66
65
|
for (let stdio of ['stdout', 'stderr']) {
|
|
67
|
-
|
|
68
|
-
columns: options.isTTY ? 100 : null,
|
|
66
|
+
logger.constructor[stdio] = Object.assign(new Writable(), {
|
|
69
67
|
isTTY: options.isTTY,
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
68
|
+
columns: options.isTTY ? 100 : null,
|
|
69
|
+
cursorTo() { return this; },
|
|
70
|
+
clearLine() { return this; },
|
|
73
71
|
_write(chunk, encoding, callback) {
|
|
74
72
|
helpers[stdio].push(sanitizeLog(chunk.toString(), options));
|
|
75
73
|
callback();
|
|
@@ -80,8 +78,8 @@ const helpers = {
|
|
|
80
78
|
},
|
|
81
79
|
|
|
82
80
|
reset(soft) {
|
|
83
|
-
if (soft) loglevel('info');
|
|
84
|
-
else delete
|
|
81
|
+
if (soft) logger.loglevel('info');
|
|
82
|
+
else delete logger.constructor.instance;
|
|
85
83
|
|
|
86
84
|
helpers.stdout.length = 0;
|
|
87
85
|
helpers.stderr.length = 0;
|
|
@@ -94,7 +92,7 @@ const helpers = {
|
|
|
94
92
|
},
|
|
95
93
|
|
|
96
94
|
dump() {
|
|
97
|
-
let msgs = Array.from(
|
|
95
|
+
let msgs = Array.from(logger.instance.messages);
|
|
98
96
|
if (!msgs.length) return;
|
|
99
97
|
|
|
100
98
|
let log = m => process.env.__PERCY_BROWSERIFIED__ ? (
|
package/dist/browser.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { ANSI_COLORS, ANSI_REG } from './utils.js';
|
|
2
|
-
import PercyLogger from './logger.js';
|
|
3
|
-
export class PercyBrowserLogger extends PercyLogger {
|
|
4
|
-
write(level, message) {
|
|
5
|
-
let out = ['warn', 'error'].includes(level) ? level : 'log';
|
|
6
|
-
let colors = [];
|
|
7
|
-
message = message.replace(ANSI_REG, (_, ansi) => {
|
|
8
|
-
colors.push(`color:${ANSI_COLORS[ansi] || 'inherit'}`);
|
|
9
|
-
return '%c';
|
|
10
|
-
});
|
|
11
|
-
console[out](message, ...colors);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
progress() {
|
|
15
|
-
console.error('The log.progress() method is not supported in browsers');
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
export default PercyBrowserLogger;
|
package/dist/bundle.js
DELETED
|
@@ -1,445 +0,0 @@
|
|
|
1
|
-
(function() {
|
|
2
|
-
(function (exports) {
|
|
3
|
-
'use strict';
|
|
4
|
-
|
|
5
|
-
const process = (typeof globalThis !== "undefined" && globalThis.process) || {};
|
|
6
|
-
process.env = process.env || {};
|
|
7
|
-
process.env.__PERCY_BROWSERIFIED__ = true;
|
|
8
|
-
|
|
9
|
-
const {
|
|
10
|
-
assign,
|
|
11
|
-
entries
|
|
12
|
-
} = Object; // matches ansi escape sequences
|
|
13
|
-
|
|
14
|
-
const ANSI_REG = new RegExp('[\\u001B\\u009B][[\\]()#;?]*((?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)' + '|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))', 'g'); // color names by ansi escape code
|
|
15
|
-
|
|
16
|
-
const ANSI_COLORS = {
|
|
17
|
-
'91m': 'red',
|
|
18
|
-
'32m': 'green',
|
|
19
|
-
'93m': 'yellow',
|
|
20
|
-
'34m': 'blue',
|
|
21
|
-
'95m': 'magenta',
|
|
22
|
-
'90m': 'grey'
|
|
23
|
-
}; // colorize each line of a string using an ansi escape sequence
|
|
24
|
-
|
|
25
|
-
const LINE_REG = /^.*$/gm;
|
|
26
|
-
|
|
27
|
-
function colorize(code, str) {
|
|
28
|
-
return str.replace(LINE_REG, line => `\u001b[${code}${line}\u001b[39m`);
|
|
29
|
-
} // map ansi colors to bound colorize functions
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const colors = entries(ANSI_COLORS).reduce((colors, _ref) => {
|
|
33
|
-
let [code, name] = _ref;
|
|
34
|
-
return assign(colors, {
|
|
35
|
-
[name]: colorize.bind(null, code)
|
|
36
|
-
});
|
|
37
|
-
}, {});
|
|
38
|
-
|
|
39
|
-
function _defineProperty(obj, key, value) {
|
|
40
|
-
if (key in obj) {
|
|
41
|
-
Object.defineProperty(obj, key, {
|
|
42
|
-
value: value,
|
|
43
|
-
enumerable: true,
|
|
44
|
-
configurable: true,
|
|
45
|
-
writable: true
|
|
46
|
-
});
|
|
47
|
-
} else {
|
|
48
|
-
obj[key] = value;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return obj;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const URL_REGEXP = /\bhttps?:\/\/[^\s/$.?#].[^\s]*\b/i;
|
|
55
|
-
const LOG_LEVELS = {
|
|
56
|
-
debug: 0,
|
|
57
|
-
info: 1,
|
|
58
|
-
warn: 2,
|
|
59
|
-
error: 3
|
|
60
|
-
}; // A PercyLogger instance retains logs in-memory for quick lookups while also writing log
|
|
61
|
-
// messages to stdout and stderr depending on the log level and debug string.
|
|
62
|
-
|
|
63
|
-
class PercyLogger {
|
|
64
|
-
// default log level
|
|
65
|
-
// namespace regular expressions used to determine which debug logs to write
|
|
66
|
-
// in-memory store for logs and meta info
|
|
67
|
-
// track deprecations to limit noisy logging
|
|
68
|
-
// static vars can be overriden for testing
|
|
69
|
-
// Handles setting env var values and returns a singleton
|
|
70
|
-
constructor() {
|
|
71
|
-
_defineProperty(this, "level", 'info');
|
|
72
|
-
|
|
73
|
-
_defineProperty(this, "namespaces", {
|
|
74
|
-
include: [/^.*?$/],
|
|
75
|
-
exclude: []
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
_defineProperty(this, "messages", new Set());
|
|
79
|
-
|
|
80
|
-
_defineProperty(this, "deprecations", new Set());
|
|
81
|
-
|
|
82
|
-
let {
|
|
83
|
-
instance = this
|
|
84
|
-
} = this.constructor;
|
|
85
|
-
|
|
86
|
-
if (process.env.PERCY_DEBUG) {
|
|
87
|
-
instance.debug(process.env.PERCY_DEBUG);
|
|
88
|
-
} else if (process.env.PERCY_LOGLEVEL) {
|
|
89
|
-
instance.loglevel(process.env.PERCY_LOGLEVEL);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
this.constructor.instance = instance;
|
|
93
|
-
return instance;
|
|
94
|
-
} // Change log level at any time or return the current log level
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
loglevel(level) {
|
|
98
|
-
if (level) this.level = level;
|
|
99
|
-
return this.level;
|
|
100
|
-
} // Change namespaces by generating an array of namespace regular expressions from a
|
|
101
|
-
// comma separated debug string
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
debug(namespaces) {
|
|
105
|
-
if (this.namespaces.string === namespaces) return;
|
|
106
|
-
this.namespaces.string = namespaces;
|
|
107
|
-
namespaces = namespaces.split(/[\s,]+/).filter(Boolean);
|
|
108
|
-
if (!namespaces.length) return this.namespaces;
|
|
109
|
-
this.loglevel('debug');
|
|
110
|
-
this.namespaces = namespaces.reduce((namespaces, ns) => {
|
|
111
|
-
ns = ns.replace(/:?\*/g, m => m[0] === ':' ? ':?.*?' : '.*?');
|
|
112
|
-
|
|
113
|
-
if (ns[0] === '-') {
|
|
114
|
-
namespaces.exclude.push(new RegExp('^' + ns.substr(1) + '$'));
|
|
115
|
-
} else {
|
|
116
|
-
namespaces.include.push(new RegExp('^' + ns + '$'));
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
return namespaces;
|
|
120
|
-
}, {
|
|
121
|
-
string: namespaces,
|
|
122
|
-
include: [],
|
|
123
|
-
exclude: []
|
|
124
|
-
});
|
|
125
|
-
} // Creates a new log group and returns level specific functions for logging
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
group(name) {
|
|
129
|
-
return Object.keys(LOG_LEVELS).reduce((group, level) => Object.assign(group, {
|
|
130
|
-
[level]: this.log.bind(this, name, level)
|
|
131
|
-
}), {
|
|
132
|
-
deprecated: this.deprecated.bind(this, name),
|
|
133
|
-
shouldLog: this.shouldLog.bind(this, name),
|
|
134
|
-
progress: this.progress.bind(this, name),
|
|
135
|
-
format: this.format.bind(this, name),
|
|
136
|
-
loglevel: this.loglevel.bind(this),
|
|
137
|
-
stdout: this.constructor.stdout,
|
|
138
|
-
stderr: this.constructor.stderr
|
|
139
|
-
});
|
|
140
|
-
} // Query for a set of logs by filtering the in-memory store
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
query(filter) {
|
|
144
|
-
return Array.from(this.messages).filter(filter);
|
|
145
|
-
} // Formats messages before they are logged to stdio
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
format(debug, level, message, elapsed) {
|
|
149
|
-
let label = 'percy';
|
|
150
|
-
let suffix = '';
|
|
151
|
-
|
|
152
|
-
if (arguments.length === 1) {
|
|
153
|
-
// format(message)
|
|
154
|
-
[debug, message] = [null, debug];
|
|
155
|
-
} else if (arguments.length === 2) {
|
|
156
|
-
// format(debug, message)
|
|
157
|
-
[level, message] = [null, level];
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
if (this.level === 'debug') {
|
|
161
|
-
// include debug info in the label
|
|
162
|
-
if (debug) label += `:${debug}`; // include elapsed time since last log
|
|
163
|
-
|
|
164
|
-
if (elapsed != null) {
|
|
165
|
-
suffix = ' ' + colors.grey(`(${elapsed}ms)`);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
label = colors.magenta(label);
|
|
170
|
-
|
|
171
|
-
if (level === 'error') {
|
|
172
|
-
// red errors
|
|
173
|
-
message = colors.red(message);
|
|
174
|
-
} else if (level === 'warn') {
|
|
175
|
-
// yellow warnings
|
|
176
|
-
message = colors.yellow(message);
|
|
177
|
-
} else if (level === 'info' || level === 'debug') {
|
|
178
|
-
// blue info and debug URLs
|
|
179
|
-
message = message.replace(URL_REGEXP, colors.blue('$&'));
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
return `[${label}] ${message}${suffix}`;
|
|
183
|
-
} // Replaces the current line with a log message
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
progress(debug, message, persist) {
|
|
187
|
-
if (!this.shouldLog(debug, 'info')) return;
|
|
188
|
-
let {
|
|
189
|
-
stdout
|
|
190
|
-
} = this.constructor;
|
|
191
|
-
|
|
192
|
-
if (stdout.isTTY || !this._progress) {
|
|
193
|
-
message && (message = this.format(debug, message));
|
|
194
|
-
if (stdout.isTTY) stdout.cursorTo(0);else message && (message = message + '\n');
|
|
195
|
-
if (message) stdout.write(message);
|
|
196
|
-
if (stdout.isTTY) stdout.clearLine(1);
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
this._progress = !!message && {
|
|
200
|
-
message,
|
|
201
|
-
persist
|
|
202
|
-
};
|
|
203
|
-
} // Returns true or false if the level and debug group can write messages to stdio
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
shouldLog(debug, level) {
|
|
207
|
-
return LOG_LEVELS[level] != null && LOG_LEVELS[level] >= LOG_LEVELS[this.level] && !this.namespaces.exclude.some(ns => ns.test(debug)) && this.namespaces.include.some(ns => ns.test(debug));
|
|
208
|
-
} // Ensures that deprecation messages are not logged more than once
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
deprecated(debug, message, meta) {
|
|
212
|
-
if (this.deprecations.has(message)) return;
|
|
213
|
-
this.deprecations.add(message);
|
|
214
|
-
this.log(debug, 'warn', `Warning: ${message}`, meta);
|
|
215
|
-
} // Returns true if a socket is present and ready
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
get isRemote() {
|
|
219
|
-
var _this$socket;
|
|
220
|
-
|
|
221
|
-
return ((_this$socket = this.socket) === null || _this$socket === void 0 ? void 0 : _this$socket.readyState) === 1;
|
|
222
|
-
} // Generic log method accepts a debug group, log level, log message, and optional meta
|
|
223
|
-
// information to store with the message and other info
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
log(debug, level, message) {
|
|
227
|
-
let meta = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
228
|
-
// message might be an error object
|
|
229
|
-
let isError = typeof message !== 'string' && (level === 'error' || level === 'debug');
|
|
230
|
-
let error = isError && message; // if remote, send logs there
|
|
231
|
-
|
|
232
|
-
if (this.isRemote) {
|
|
233
|
-
// serialize error messages
|
|
234
|
-
message = isError && 'stack' in error ? {
|
|
235
|
-
message: error.message,
|
|
236
|
-
stack: error.stack
|
|
237
|
-
} : message;
|
|
238
|
-
return this.socket.send(JSON.stringify({
|
|
239
|
-
log: [debug, level, message, {
|
|
240
|
-
remote: true,
|
|
241
|
-
...meta
|
|
242
|
-
}]
|
|
243
|
-
}));
|
|
244
|
-
} // ensure the message is a string
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
message = isError && message.stack || message.message || message.toString(); // timestamp each log
|
|
248
|
-
|
|
249
|
-
let timestamp = Date.now();
|
|
250
|
-
let entry = {
|
|
251
|
-
debug,
|
|
252
|
-
level,
|
|
253
|
-
message,
|
|
254
|
-
meta,
|
|
255
|
-
timestamp
|
|
256
|
-
};
|
|
257
|
-
this.messages.add(entry); // maybe write the message to stdio
|
|
258
|
-
|
|
259
|
-
if (this.shouldLog(debug, level)) {
|
|
260
|
-
let elapsed = timestamp - (this.lastlog || timestamp);
|
|
261
|
-
if (isError && this.level !== 'debug') message = error.toString();
|
|
262
|
-
this.write(level, this.format(debug, error ? 'error' : level, message, elapsed));
|
|
263
|
-
this.lastlog = timestamp;
|
|
264
|
-
}
|
|
265
|
-
} // Writes a message to stdio based on the loglevel
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
write(level, message) {
|
|
269
|
-
var _this$_progress;
|
|
270
|
-
|
|
271
|
-
let {
|
|
272
|
-
stdout,
|
|
273
|
-
stderr
|
|
274
|
-
} = this.constructor;
|
|
275
|
-
let progress = stdout.isTTY && this._progress;
|
|
276
|
-
|
|
277
|
-
if (progress) {
|
|
278
|
-
stdout.cursorTo(0);
|
|
279
|
-
stdout.clearLine();
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
(level === 'info' ? stdout : stderr).write(message + '\n');
|
|
283
|
-
if (!((_this$_progress = this._progress) !== null && _this$_progress !== void 0 && _this$_progress.persist)) delete this._progress;else if (progress) stdout.write(progress.message);
|
|
284
|
-
} // Opens a socket logging connection
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
connect(socket) {
|
|
288
|
-
// send logging environment info
|
|
289
|
-
let PERCY_DEBUG = process.env.PERCY_DEBUG;
|
|
290
|
-
let PERCY_LOGLEVEL = process.env.PERCY_LOGLEVEL || this.loglevel();
|
|
291
|
-
socket.send(JSON.stringify({
|
|
292
|
-
env: {
|
|
293
|
-
PERCY_DEBUG,
|
|
294
|
-
PERCY_LOGLEVEL
|
|
295
|
-
}
|
|
296
|
-
})); // attach remote logging handler
|
|
297
|
-
|
|
298
|
-
socket.onmessage = _ref => {
|
|
299
|
-
let {
|
|
300
|
-
data
|
|
301
|
-
} = _ref;
|
|
302
|
-
let {
|
|
303
|
-
log,
|
|
304
|
-
logAll
|
|
305
|
-
} = JSON.parse(data);
|
|
306
|
-
if (logAll) logAll.forEach(e => this.messages.add(e));
|
|
307
|
-
if (log) this.log(...log);
|
|
308
|
-
}; // return a cleanup function
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
return () => {
|
|
312
|
-
socket.onmessage = null;
|
|
313
|
-
};
|
|
314
|
-
} // Connects to a remote logger
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
async remote(createSocket) {
|
|
318
|
-
let timeout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1000;
|
|
319
|
-
if (this.isRemote) return; // if not already connected, wait until the timeout
|
|
320
|
-
|
|
321
|
-
let err = await new Promise(resolve => {
|
|
322
|
-
let done = event => {
|
|
323
|
-
if (timeoutid == null) return;
|
|
324
|
-
timeoutid = clearTimeout(timeoutid);
|
|
325
|
-
if (this.socket) this.socket.onopen = this.socket.onerror = null;
|
|
326
|
-
resolve((event === null || event === void 0 ? void 0 : event.error) || (event === null || event === void 0 ? void 0 : event.type) === 'error' && 'Error: Socket connection failed');
|
|
327
|
-
};
|
|
328
|
-
|
|
329
|
-
let timeoutid = setTimeout(done, timeout, {
|
|
330
|
-
error: 'Error: Socket connection timed out'
|
|
331
|
-
});
|
|
332
|
-
Promise.resolve().then(async () => {
|
|
333
|
-
this.socket = await createSocket();
|
|
334
|
-
if (this.isRemote) return done();
|
|
335
|
-
this.socket.onopen = this.socket.onerror = done;
|
|
336
|
-
}).catch(error => done({
|
|
337
|
-
error
|
|
338
|
-
}));
|
|
339
|
-
}); // there was an error connecting, will fallback to normal logging
|
|
340
|
-
|
|
341
|
-
if (err) {
|
|
342
|
-
this.log('logger', 'debug', 'Unable to connect to remote logger');
|
|
343
|
-
this.log('logger', 'debug', err);
|
|
344
|
-
return;
|
|
345
|
-
} // send any messages already logged in this environment
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
if (this.messages.size) {
|
|
349
|
-
this.socket.send(JSON.stringify({
|
|
350
|
-
logAll: Array.from(this.messages).map(entry => ({ ...entry,
|
|
351
|
-
meta: {
|
|
352
|
-
remote: true,
|
|
353
|
-
...entry.meta
|
|
354
|
-
}
|
|
355
|
-
}))
|
|
356
|
-
}));
|
|
357
|
-
} // attach an incoming message handler
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
this.socket.onmessage = _ref2 => {
|
|
361
|
-
let {
|
|
362
|
-
data
|
|
363
|
-
} = _ref2;
|
|
364
|
-
let {
|
|
365
|
-
env
|
|
366
|
-
} = JSON.parse(data); // update local environment info
|
|
367
|
-
|
|
368
|
-
if (env) Object.assign(process.env, env);
|
|
369
|
-
}; // return a cleanup function
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
return () => {
|
|
373
|
-
this.socket.onmessage = null;
|
|
374
|
-
this.socket = null;
|
|
375
|
-
};
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
_defineProperty(PercyLogger, "stdout", process.stdout);
|
|
381
|
-
|
|
382
|
-
_defineProperty(PercyLogger, "stderr", process.stderr);
|
|
383
|
-
|
|
384
|
-
class PercyBrowserLogger extends PercyLogger {
|
|
385
|
-
write(level, message) {
|
|
386
|
-
let out = ['warn', 'error'].includes(level) ? level : 'log';
|
|
387
|
-
let colors = [];
|
|
388
|
-
message = message.replace(ANSI_REG, (_, ansi) => {
|
|
389
|
-
colors.push(`color:${ANSI_COLORS[ansi] || 'inherit'}`);
|
|
390
|
-
return '%c';
|
|
391
|
-
});
|
|
392
|
-
console[out](message, ...colors);
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
progress() {
|
|
396
|
-
console.error('The log.progress() method is not supported in browsers');
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
function logger(name) {
|
|
402
|
-
return new PercyBrowserLogger().group(name);
|
|
403
|
-
}
|
|
404
|
-
Object.assign(logger, {
|
|
405
|
-
format: function () {
|
|
406
|
-
return new PercyBrowserLogger().format(...arguments);
|
|
407
|
-
},
|
|
408
|
-
query: function () {
|
|
409
|
-
return new PercyBrowserLogger().query(...arguments);
|
|
410
|
-
},
|
|
411
|
-
connect: function () {
|
|
412
|
-
return new PercyBrowserLogger().connect(...arguments);
|
|
413
|
-
},
|
|
414
|
-
remote: function () {
|
|
415
|
-
return new PercyBrowserLogger().remote(...arguments);
|
|
416
|
-
},
|
|
417
|
-
loglevel: function () {
|
|
418
|
-
return new PercyBrowserLogger().loglevel(...arguments);
|
|
419
|
-
}
|
|
420
|
-
});
|
|
421
|
-
Object.defineProperties(logger, {
|
|
422
|
-
Logger: {
|
|
423
|
-
get: () => PercyBrowserLogger
|
|
424
|
-
},
|
|
425
|
-
stdout: {
|
|
426
|
-
get: () => PercyBrowserLogger.stdout
|
|
427
|
-
},
|
|
428
|
-
stderr: {
|
|
429
|
-
get: () => PercyBrowserLogger.stderr
|
|
430
|
-
}
|
|
431
|
-
});
|
|
432
|
-
|
|
433
|
-
exports["default"] = logger;
|
|
434
|
-
exports.logger = logger;
|
|
435
|
-
|
|
436
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
437
|
-
|
|
438
|
-
})(this.PercyLogger = this.PercyLogger || {});
|
|
439
|
-
}).call(window);
|
|
440
|
-
|
|
441
|
-
if (typeof define === "function" && define.amd) {
|
|
442
|
-
define([], () => window.PercyLogger);
|
|
443
|
-
} else if (typeof module === "object" && module.exports) {
|
|
444
|
-
module.exports = window.PercyLogger;
|
|
445
|
-
}
|
package/test/client.js
DELETED
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
(function() {
|
|
2
|
-
this.PercyLogger = this.PercyLogger || {};
|
|
3
|
-
this.PercyLogger.TestHelpers = (function (logger) {
|
|
4
|
-
'use strict';
|
|
5
|
-
|
|
6
|
-
const process = (typeof globalThis !== "undefined" && globalThis.process) || {};
|
|
7
|
-
process.env = process.env || {};
|
|
8
|
-
process.env.__PERCY_BROWSERIFIED__ = true;
|
|
9
|
-
|
|
10
|
-
globalThis.process = globalThis.process || process;
|
|
11
|
-
|
|
12
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
13
|
-
|
|
14
|
-
var logger__default = /*#__PURE__*/_interopDefaultLegacy(logger);
|
|
15
|
-
|
|
16
|
-
const {
|
|
17
|
-
assign,
|
|
18
|
-
entries
|
|
19
|
-
} = Object; // matches ansi escape sequences
|
|
20
|
-
|
|
21
|
-
const ANSI_REG = new RegExp('[\\u001B\\u009B][[\\]()#;?]*((?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)' + '|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))', 'g'); // color names by ansi escape code
|
|
22
|
-
|
|
23
|
-
const ANSI_COLORS = {
|
|
24
|
-
'91m': 'red',
|
|
25
|
-
'32m': 'green',
|
|
26
|
-
'93m': 'yellow',
|
|
27
|
-
'34m': 'blue',
|
|
28
|
-
'95m': 'magenta',
|
|
29
|
-
'90m': 'grey'
|
|
30
|
-
}; // colorize each line of a string using an ansi escape sequence
|
|
31
|
-
|
|
32
|
-
const LINE_REG = /^.*$/gm;
|
|
33
|
-
|
|
34
|
-
function colorize(code, str) {
|
|
35
|
-
return str.replace(LINE_REG, line => `\u001b[${code}${line}\u001b[39m`);
|
|
36
|
-
} // map ansi colors to bound colorize functions
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
entries(ANSI_COLORS).reduce((colors, _ref) => {
|
|
40
|
-
let [code, name] = _ref;
|
|
41
|
-
return assign(colors, {
|
|
42
|
-
[name]: colorize.bind(null, code)
|
|
43
|
-
});
|
|
44
|
-
}, {});
|
|
45
|
-
|
|
46
|
-
const ELAPSED_REG = /\s\S*?\(\d+ms\)\S*/;
|
|
47
|
-
const NEWLINE_REG = /\r\n/g;
|
|
48
|
-
const LASTLINE_REG = /\n$/;
|
|
49
|
-
|
|
50
|
-
function sanitizeLog(str) {
|
|
51
|
-
let {
|
|
52
|
-
ansi,
|
|
53
|
-
elapsed
|
|
54
|
-
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
55
|
-
// normalize line endings
|
|
56
|
-
str = str.replace(NEWLINE_REG, '\n'); // strip ansi colors
|
|
57
|
-
|
|
58
|
-
if (!ansi) str = str.replace(ANSI_REG, ''); // strip elapsed time
|
|
59
|
-
|
|
60
|
-
if (!elapsed) str = str.replace(ELAPSED_REG, ''); // strip trailing line endings
|
|
61
|
-
|
|
62
|
-
return str.replace(LASTLINE_REG, '');
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function spy(object, method, func) {
|
|
66
|
-
if (object[method].restore) object[method].restore();
|
|
67
|
-
let spy = Object.assign(function spy() {
|
|
68
|
-
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
69
|
-
args[_key] = arguments[_key];
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
spy.calls.push(args);
|
|
73
|
-
if (func) return func.apply(this, args);
|
|
74
|
-
}, {
|
|
75
|
-
restore: () => object[method] = spy.originalValue,
|
|
76
|
-
reset: () => (spy.calls.length = 0) || spy,
|
|
77
|
-
originalValue: object[method],
|
|
78
|
-
calls: []
|
|
79
|
-
});
|
|
80
|
-
object[method] = spy;
|
|
81
|
-
return spy;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const {
|
|
85
|
-
Logger,
|
|
86
|
-
loglevel
|
|
87
|
-
} = logger__default["default"];
|
|
88
|
-
const helpers = {
|
|
89
|
-
stdout: [],
|
|
90
|
-
stderr: [],
|
|
91
|
-
loglevel,
|
|
92
|
-
|
|
93
|
-
async mock() {
|
|
94
|
-
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
95
|
-
helpers.reset();
|
|
96
|
-
|
|
97
|
-
if (options.level) {
|
|
98
|
-
loglevel(options.level);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (process.env.__PERCY_BROWSERIFIED__) {
|
|
102
|
-
spy(Logger.prototype, 'write', function (lvl, msg) {
|
|
103
|
-
let stdio = lvl === 'info' ? 'stdout' : 'stderr';
|
|
104
|
-
helpers[stdio].push(sanitizeLog(msg, options));
|
|
105
|
-
return this.write.originalValue.call(this, lvl, msg);
|
|
106
|
-
});
|
|
107
|
-
spy(console, 'log');
|
|
108
|
-
spy(console, 'warn');
|
|
109
|
-
spy(console, 'error');
|
|
110
|
-
} else {
|
|
111
|
-
let {
|
|
112
|
-
Writable
|
|
113
|
-
} = await import('stream');
|
|
114
|
-
|
|
115
|
-
for (let stdio of ['stdout', 'stderr']) {
|
|
116
|
-
Logger[stdio] = Object.assign(new Writable(), {
|
|
117
|
-
columns: options.isTTY ? 100 : null,
|
|
118
|
-
isTTY: options.isTTY,
|
|
119
|
-
|
|
120
|
-
cursorTo() {},
|
|
121
|
-
|
|
122
|
-
clearLine() {},
|
|
123
|
-
|
|
124
|
-
_write(chunk, encoding, callback) {
|
|
125
|
-
helpers[stdio].push(sanitizeLog(chunk.toString(), options));
|
|
126
|
-
callback();
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
},
|
|
133
|
-
|
|
134
|
-
reset(soft) {
|
|
135
|
-
if (soft) loglevel('info');else delete Logger.instance;
|
|
136
|
-
helpers.stdout.length = 0;
|
|
137
|
-
helpers.stderr.length = 0;
|
|
138
|
-
|
|
139
|
-
if (console.log.reset) {
|
|
140
|
-
console.log.reset();
|
|
141
|
-
console.warn.reset();
|
|
142
|
-
console.error.reset();
|
|
143
|
-
}
|
|
144
|
-
},
|
|
145
|
-
|
|
146
|
-
dump() {
|
|
147
|
-
let msgs = Array.from(Logger.instance && Logger.instance.messages || []);
|
|
148
|
-
if (!msgs.length) return;
|
|
149
|
-
|
|
150
|
-
let log = m => process.env.__PERCY_BROWSERIFIED__ ? console.log.and ? console.log.and.originalFn(m) : console.log(m) : process.stderr.write(`${m}\n`);
|
|
151
|
-
|
|
152
|
-
logger__default["default"].loglevel('debug');
|
|
153
|
-
log(logger__default["default"].format('testing', 'warn', '--- DUMPING LOGS ---'));
|
|
154
|
-
msgs.reduce((last, _ref) => {
|
|
155
|
-
let {
|
|
156
|
-
debug,
|
|
157
|
-
level,
|
|
158
|
-
message,
|
|
159
|
-
timestamp
|
|
160
|
-
} = _ref;
|
|
161
|
-
log(logger__default["default"].format(debug, level, message, timestamp - last));
|
|
162
|
-
return timestamp;
|
|
163
|
-
}, msgs[0].timestamp);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
return helpers;
|
|
169
|
-
|
|
170
|
-
})(PercyLogger);
|
|
171
|
-
}).call(window);
|
|
172
|
-
|
|
173
|
-
if (typeof define === "function" && define.amd) {
|
|
174
|
-
define([], () => window.PercyLogger.TestHelpers);
|
|
175
|
-
} else if (typeof module === "object" && module.exports) {
|
|
176
|
-
module.exports = window.PercyLogger.TestHelpers;
|
|
177
|
-
}
|