@percy/logger
Advanced tools
Comparing version 1.0.0-beta.49 to 1.0.0-beta.50
(function() { | ||
(function (exports) { | ||
'use strict'; | ||
'use strict'; | ||
const process = (typeof globalThis !== "undefined" && globalThis.process) || {}; | ||
process.env = process.env || {}; | ||
process.env.__PERCY_BROWSERIFIED__ = true; | ||
const process = (typeof globalThis !== "undefined" && globalThis.process) || {}; | ||
process.env = process.env || {}; | ||
process.env.__PERCY_BROWSERIFIED__ = true; | ||
const { | ||
assign, | ||
entries | ||
} = Object; // matches ansi escape sequences | ||
function getAugmentedNamespace(n) { | ||
if (n.__esModule) return n; | ||
var a = Object.defineProperty({}, '__esModule', {value: true}); | ||
Object.keys(n).forEach(function (k) { | ||
var d = Object.getOwnPropertyDescriptor(n, k); | ||
Object.defineProperty(a, k, d.get ? d : { | ||
enumerable: true, | ||
get: function () { | ||
return n[k]; | ||
} | ||
}); | ||
}); | ||
return a; | ||
} | ||
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 | ||
const { | ||
assign, | ||
entries | ||
} = Object; // matches ansi escape sequences | ||
const ANSI_COLORS = { | ||
'31m': 'red', | ||
'33m': 'yellow', | ||
'34m': 'blue', | ||
'35m': 'magenta', | ||
'90m': 'grey' | ||
}; // colorize each line of a string using an ansi escape sequence | ||
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 | ||
const LINE_REG = /^.*$/gm; | ||
const ANSI_COLORS = { | ||
'31m': 'red', | ||
'33m': 'yellow', | ||
'34m': 'blue', | ||
'35m': 'magenta', | ||
'90m': 'grey' | ||
}; // colorize each line of a string using an ansi escape sequence | ||
function colorize(code, str) { | ||
return str.replace(LINE_REG, line => `\u001b[${code}${line}\u001b[39m`); | ||
} // map ansi colors to bound colorize functions | ||
const LINE_REG = /^.*$/gm; | ||
function colorize(code, str) { | ||
return str.replace(LINE_REG, line => `\u001b[${code}${line}\u001b[39m`); | ||
} // map ansi colors to bound colorize functions | ||
const colors = entries(ANSI_COLORS).reduce((colors, [code, name]) => { | ||
return assign(colors, { | ||
[name]: colorize.bind(null, code) | ||
}); | ||
}, {}); | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
const colors = entries(ANSI_COLORS).reduce((colors, [code, name]) => { | ||
return assign(colors, { | ||
[name]: colorize.bind(null, code) | ||
}); | ||
}, {}); | ||
return obj; | ||
} | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
const URL_REGEXP = /\bhttps?:\/\/[^\s/$.?#].[^\s]*\b/i; | ||
const LOG_LEVELS = { | ||
debug: 0, | ||
info: 1, | ||
warn: 2, | ||
error: 3 | ||
}; // A PercyLogger instance retains logs in-memory for quick lookups while also writing log | ||
// messages to stdout and stderr depending on the log level and debug string. | ||
return obj; | ||
} | ||
class PercyLogger { | ||
// default log level | ||
// namespace regular expressions used to determine which debug logs to write | ||
// in-memory store for logs and meta info | ||
// track deprecations to limit noisy logging | ||
// static vars can be overriden for testing | ||
// Handles setting env var values and returns a singleton | ||
constructor() { | ||
_defineProperty(this, "level", 'info'); | ||
const URL_REGEXP = /\bhttps?:\/\/[^\s/$.?#].[^\s]*\b/i; | ||
const LOG_LEVELS = { | ||
debug: 0, | ||
info: 1, | ||
warn: 2, | ||
error: 3 | ||
}; // A PercyLogger instance retains logs in-memory for quick lookups while also writing log | ||
// messages to stdout and stderr depending on the log level and debug string. | ||
_defineProperty(this, "namespaces", { | ||
include: [/^.*?$/], | ||
exclude: [] | ||
}); | ||
class PercyLogger { | ||
// default log level | ||
// namespace regular expressions used to determine which debug logs to write | ||
// in-memory store for logs and meta info | ||
// track deprecations to limit noisy logging | ||
// static vars can be overriden for testing | ||
// Handles setting env var values and returns a singleton | ||
constructor() { | ||
_defineProperty(this, "level", 'info'); | ||
_defineProperty(this, "messages", new Set()); | ||
_defineProperty(this, "namespaces", { | ||
include: [/^.*?$/], | ||
exclude: [] | ||
}); | ||
_defineProperty(this, "deprecations", new Set()); | ||
_defineProperty(this, "messages", new Set()); | ||
let { | ||
instance = this | ||
} = this.constructor; | ||
_defineProperty(this, "deprecations", new Set()); | ||
if (process.env.PERCY_DEBUG) { | ||
instance.debug(process.env.PERCY_DEBUG); | ||
} else if (process.env.PERCY_LOGLEVEL) { | ||
instance.loglevel(process.env.PERCY_LOGLEVEL); | ||
} | ||
let { | ||
instance = this | ||
} = this.constructor; | ||
this.constructor.instance = instance; | ||
return instance; | ||
} // Change log level at any time or return the current log level | ||
if (process.env.PERCY_DEBUG) { | ||
instance.debug(process.env.PERCY_DEBUG); | ||
} else if (process.env.PERCY_LOGLEVEL) { | ||
instance.loglevel(process.env.PERCY_LOGLEVEL); | ||
} | ||
this.constructor.instance = instance; | ||
return instance; | ||
} // Change log level at any time or return the current log level | ||
loglevel(level) { | ||
if (!level) return this.level; | ||
this.level = level; | ||
} // Change namespaces by generating an array of namespace regular expressions from a | ||
// comma separated debug string | ||
loglevel(level) { | ||
if (!level) return this.level; | ||
this.level = level; | ||
} // Change namespaces by generating an array of namespace regular expressions from a | ||
// comma separated debug string | ||
debug(namespaces) { | ||
if (this.namespaces.string === namespaces) return; | ||
this.namespaces.string = namespaces; | ||
namespaces = namespaces.split(/[\s,]+/).filter(Boolean); | ||
if (!namespaces.length) return this.namespaces; | ||
this.loglevel('debug'); | ||
this.namespaces = namespaces.reduce((namespaces, ns) => { | ||
ns = ns.replace(/:?\*/g, m => m[0] === ':' ? ':?.*?' : '.*?'); | ||
if (ns[0] === '-') { | ||
namespaces.exclude.push(new RegExp('^' + ns.substr(1) + '$')); | ||
} else { | ||
namespaces.include.push(new RegExp('^' + ns + '$')); | ||
} | ||
debug(namespaces) { | ||
if (this.namespaces.string === namespaces) return; | ||
this.namespaces.string = namespaces; | ||
namespaces = namespaces.split(/[\s,]+/).filter(Boolean); | ||
if (!namespaces.length) return this.namespaces; | ||
this.loglevel('debug'); | ||
this.namespaces = namespaces.reduce((namespaces, ns) => { | ||
ns = ns.replace(/:?\*/g, m => m[0] === ':' ? ':?.*?' : '.*?'); | ||
return namespaces; | ||
}, { | ||
string: namespaces, | ||
include: [], | ||
exclude: [] | ||
}); | ||
} // Creates a new log group and returns level specific functions for logging | ||
if (ns[0] === '-') { | ||
namespaces.exclude.push(new RegExp('^' + ns.substr(1) + '$')); | ||
} else { | ||
namespaces.include.push(new RegExp('^' + ns + '$')); | ||
} | ||
return namespaces; | ||
}, { | ||
string: namespaces, | ||
include: [], | ||
exclude: [] | ||
}); | ||
} // Creates a new log group and returns level specific functions for logging | ||
group(name) { | ||
return Object.keys(LOG_LEVELS).reduce((group, level) => Object.assign(group, { | ||
[level]: this.log.bind(this, name, level) | ||
}), { | ||
deprecated: this.deprecated.bind(this, name), | ||
shouldLog: this.shouldLog.bind(this, name) | ||
}); | ||
} // Query for a set of logs by filtering the in-memory store | ||
group(name) { | ||
return Object.keys(LOG_LEVELS).reduce((group, level) => Object.assign(group, { | ||
[level]: this.log.bind(this, name, level) | ||
}), { | ||
deprecated: this.deprecated.bind(this, name), | ||
shouldLog: this.shouldLog.bind(this, name) | ||
}); | ||
} // Query for a set of logs by filtering the in-memory store | ||
query(filter) { | ||
return Array.from(this.messages).filter(filter); | ||
} // Formats messages before they are logged to stdio | ||
query(filter) { | ||
return Array.from(this.messages).filter(filter); | ||
} // Formats messages before they are logged to stdio | ||
format(message, debug, level, elapsed) { | ||
let label = 'percy'; | ||
let suffix = ''; | ||
if (this.level === 'debug') { | ||
// include debug info in the label | ||
if (debug) label += `:${debug}`; // include elapsed time since last log | ||
format(message, debug, level, elapsed) { | ||
let label = 'percy'; | ||
let suffix = ''; | ||
if (elapsed != null) { | ||
suffix = ' ' + colors.grey(`(${elapsed}ms)`); | ||
} | ||
} | ||
if (this.level === 'debug') { | ||
// include debug info in the label | ||
if (debug) label += `:${debug}`; // include elapsed time since last log | ||
label = colors.magenta(label); | ||
if (elapsed != null) { | ||
suffix = ' ' + colors.grey(`(${elapsed}ms)`); | ||
} | ||
} | ||
if (level === 'error') { | ||
// red errors | ||
message = colors.red(message); | ||
} else if (level === 'warn') { | ||
// yellow warnings | ||
message = colors.yellow(message); | ||
} else if (level === 'info' || level === 'debug') { | ||
// blue info and debug URLs | ||
message = message.replace(URL_REGEXP, colors.blue('$&')); | ||
} | ||
label = colors.magenta(label); | ||
return `[${label}] ${message}${suffix}`; | ||
} // Returns true or false if the level and debug group can write messages to stdio | ||
if (level === 'error') { | ||
// red errors | ||
message = colors.red(message); | ||
} else if (level === 'warn') { | ||
// yellow warnings | ||
message = colors.yellow(message); | ||
} else if (level === 'info' || level === 'debug') { | ||
// blue info and debug URLs | ||
message = message.replace(URL_REGEXP, colors.blue('$&')); | ||
} | ||
return `[${label}] ${message}${suffix}`; | ||
} // Returns true or false if the level and debug group can write messages to stdio | ||
shouldLog(debug, level) { | ||
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)); | ||
} // Ensures that deprecation messages are not logged more than once | ||
shouldLog(debug, level) { | ||
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)); | ||
} // Ensures that deprecation messages are not logged more than once | ||
deprecated(debug, message, meta) { | ||
if (this.deprecations.has(message)) return; | ||
this.deprecations.add(message); | ||
this.log(debug, 'warn', `Warning: ${message}`, meta); | ||
} // Returns true if a socket is present and ready | ||
deprecated(debug, message, meta) { | ||
if (this.deprecations.has(message)) return; | ||
this.deprecations.add(message); | ||
this.log(debug, 'warn', `Warning: ${message}`, meta); | ||
} // Returns true if a socket is present and ready | ||
get isRemote() { | ||
var _this$socket; | ||
return ((_this$socket = this.socket) === null || _this$socket === void 0 ? void 0 : _this$socket.readyState) === 1; | ||
} // Generic log method accepts a debug group, log level, log message, and optional meta | ||
// information to store with the message and other info | ||
get isRemote() { | ||
var _this$socket; | ||
return ((_this$socket = this.socket) === null || _this$socket === void 0 ? void 0 : _this$socket.readyState) === 1; | ||
} // Generic log method accepts a debug group, log level, log message, and optional meta | ||
// information to store with the message and other info | ||
log(debug, level, message, meta = {}) { | ||
// message might be an error object | ||
let isError = typeof message !== 'string' && (level === 'error' || level === 'debug'); | ||
let error = isError && message; // if remote, send logs there | ||
if (this.isRemote) { | ||
// serialize error messages | ||
message = isError && 'stack' in error ? { | ||
message: error.message, | ||
stack: error.stack | ||
} : message; | ||
return this.socket.send(JSON.stringify({ | ||
log: [debug, level, message, { | ||
remote: true, | ||
...meta | ||
}] | ||
})); | ||
} // ensure the message is a string | ||
log(debug, level, message, meta = {}) { | ||
// message might be an error object | ||
let isError = typeof message !== 'string' && (level === 'error' || level === 'debug'); | ||
let error = isError && message; // if remote, send logs there | ||
if (this.isRemote) { | ||
// serialize error messages | ||
message = isError && 'stack' in error ? { | ||
message: error.message, | ||
stack: error.stack | ||
} : message; | ||
return this.socket.send(JSON.stringify({ | ||
log: [debug, level, message, { | ||
remote: true, | ||
...meta | ||
}] | ||
})); | ||
} // ensure the message is a string | ||
message = isError && message.stack || message.message || message.toString(); // timestamp each log | ||
let timestamp = Date.now(); | ||
let entry = { | ||
debug, | ||
level, | ||
message, | ||
meta, | ||
timestamp | ||
}; | ||
this.messages.add(entry); // maybe write the message to stdio | ||
message = isError && message.stack || message.message || message.toString(); // timestamp each log | ||
if (this.shouldLog(debug, level)) { | ||
let elapsed = timestamp - (this.lastlog || timestamp); | ||
if (isError && this.level !== 'debug') message = error.toString(); | ||
this.write(level, this.format(message, debug, error ? 'error' : level, elapsed)); | ||
this.lastlog = timestamp; | ||
} | ||
} // Writes a message to stdio based on the loglevel | ||
let timestamp = Date.now(); | ||
let entry = { | ||
debug, | ||
level, | ||
message, | ||
meta, | ||
timestamp | ||
}; | ||
this.messages.add(entry); // maybe write the message to stdio | ||
if (this.shouldLog(debug, level)) { | ||
let elapsed = timestamp - (this.lastlog || timestamp); | ||
if (isError && this.level !== 'debug') message = error.toString(); | ||
this.write(level, this.format(message, debug, error ? 'error' : level, elapsed)); | ||
this.lastlog = timestamp; | ||
} | ||
} // Writes a message to stdio based on the loglevel | ||
write(level, message) { | ||
let stdio = level === 'info' ? 'stdout' : 'stderr'; | ||
this.constructor[stdio].write(message + '\n'); | ||
} // Opens a socket logging connection | ||
write(level, message) { | ||
let stdio = level === 'info' ? 'stdout' : 'stderr'; | ||
this.constructor[stdio].write(message + '\n'); | ||
} // Opens a socket logging connection | ||
connect(socket) { | ||
// send logging environment info | ||
let PERCY_DEBUG = process.env.PERCY_DEBUG; | ||
let PERCY_LOGLEVEL = process.env.PERCY_LOGLEVEL || this.loglevel(); | ||
socket.send(JSON.stringify({ | ||
env: { | ||
PERCY_DEBUG, | ||
PERCY_LOGLEVEL | ||
} | ||
})); // attach remote logging handler | ||
socket.onmessage = ({ | ||
data | ||
}) => { | ||
let { | ||
log, | ||
logAll | ||
} = JSON.parse(data); | ||
if (logAll) logAll.forEach(e => this.messages.add(e)); | ||
if (log) this.log(...log); | ||
}; // return a cleanup function | ||
connect(socket) { | ||
// send logging environment info | ||
let PERCY_DEBUG = process.env.PERCY_DEBUG; | ||
let PERCY_LOGLEVEL = process.env.PERCY_LOGLEVEL || this.loglevel(); | ||
socket.send(JSON.stringify({ | ||
env: { | ||
PERCY_DEBUG, | ||
PERCY_LOGLEVEL | ||
} | ||
})); // attach remote logging handler | ||
socket.onmessage = ({ | ||
data | ||
}) => { | ||
let { | ||
log, | ||
logAll | ||
} = JSON.parse(data); | ||
if (logAll) logAll.forEach(e => this.messages.add(e)); | ||
if (log) this.log(...log); | ||
}; // return a cleanup function | ||
return () => { | ||
socket.onmessage = null; | ||
}; | ||
} // Connects to a remote logger | ||
return () => { | ||
socket.onmessage = null; | ||
}; | ||
} // Connects to a remote logger | ||
async remote(createSocket, timeout = 1000) { | ||
if (this.isRemote) return; // if not already connected, wait until the timeout | ||
let err = await new Promise(resolve => { | ||
let done = event => { | ||
if (timeoutid == null) return; | ||
timeoutid = clearTimeout(timeoutid); | ||
if (this.socket) this.socket.onopen = this.socket.onerror = null; | ||
resolve((event === null || event === void 0 ? void 0 : event.error) || (event === null || event === void 0 ? void 0 : event.type) === 'error' && 'Error: Socket connection failed'); | ||
}; | ||
async remote(createSocket, timeout = 1000) { | ||
if (this.isRemote) return; // if not already connected, wait until the timeout | ||
let timeoutid = setTimeout(done, timeout, { | ||
error: 'Error: Socket connection timed out' | ||
}); | ||
Promise.resolve().then(async () => { | ||
this.socket = await createSocket(); | ||
if (this.isRemote) return done(); | ||
this.socket.onopen = this.socket.onerror = done; | ||
}).catch(error => done({ | ||
error | ||
})); | ||
}); // there was an error connecting, will fallback to normal logging | ||
let err = await new Promise(resolve => { | ||
let done = event => { | ||
if (timeoutid == null) return; | ||
timeoutid = clearTimeout(timeoutid); | ||
if (this.socket) this.socket.onopen = this.socket.onerror = null; | ||
resolve((event === null || event === void 0 ? void 0 : event.error) || (event === null || event === void 0 ? void 0 : event.type) === 'error' && 'Error: Socket connection failed'); | ||
}; | ||
if (err) { | ||
this.log('logger', 'debug', 'Unable to connect to remote logger'); | ||
this.log('logger', 'debug', err); | ||
return; | ||
} // send any messages already logged in this environment | ||
let timeoutid = setTimeout(done, timeout, { | ||
error: 'Error: Socket connection timed out' | ||
}); | ||
Promise.resolve().then(async () => { | ||
this.socket = await createSocket(); | ||
if (this.isRemote) return done(); | ||
this.socket.onopen = this.socket.onerror = done; | ||
}).catch(error => done({ | ||
error | ||
})); | ||
}); // there was an error connecting, will fallback to normal logging | ||
if (err) { | ||
this.log('logger', 'debug', 'Unable to connect to remote logger'); | ||
this.log('logger', 'debug', err); | ||
return; | ||
} // send any messages already logged in this environment | ||
if (this.messages.size) { | ||
this.socket.send(JSON.stringify({ | ||
logAll: Array.from(this.messages).map(entry => ({ ...entry, | ||
meta: { | ||
remote: true, | ||
...entry.meta | ||
} | ||
})) | ||
})); | ||
} // attach an incoming message handler | ||
if (this.messages.size) { | ||
this.socket.send(JSON.stringify({ | ||
logAll: Array.from(this.messages).map(entry => ({ ...entry, | ||
meta: { | ||
remote: true, | ||
...entry.meta | ||
} | ||
})) | ||
})); | ||
} // attach an incoming message handler | ||
this.socket.onmessage = ({ | ||
data | ||
}) => { | ||
let { | ||
env | ||
} = JSON.parse(data); // update local environment info | ||
if (env) Object.assign(process.env, env); | ||
}; // return a cleanup function | ||
this.socket.onmessage = ({ | ||
data | ||
}) => { | ||
let { | ||
env | ||
} = JSON.parse(data); // update local environment info | ||
if (env) Object.assign(process.env, env); | ||
}; // return a cleanup function | ||
return () => { | ||
this.socket.onmessage = null; | ||
this.socket = null; | ||
}; | ||
} | ||
} | ||
return () => { | ||
this.socket.onmessage = null; | ||
this.socket = null; | ||
}; | ||
} | ||
_defineProperty(PercyLogger, "stdout", process.stdout); | ||
} | ||
_defineProperty(PercyLogger, "stderr", process.stderr); | ||
_defineProperty(PercyLogger, "stdout", process.stdout); | ||
var logger$1 = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
'default': PercyLogger | ||
}); | ||
_defineProperty(PercyLogger, "stderr", process.stderr); | ||
class BrowserLogger extends PercyLogger { | ||
write(level, message) { | ||
let out = ['warn', 'error'].includes(level) ? level : 'log'; | ||
let colors = []; | ||
message = message.replace(ANSI_REG, (_, ansi) => { | ||
colors.push(`color:${ANSI_COLORS[ansi] || 'inherit'}`); | ||
return '%c'; | ||
}); | ||
console[out](message, ...colors); | ||
} | ||
var logger$1 = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
'default': PercyLogger | ||
}); | ||
} | ||
class BrowserLogger extends PercyLogger { | ||
write(level, message) { | ||
let out = ['warn', 'error'].includes(level) ? level : 'log'; | ||
let colors = []; | ||
message = message.replace(ANSI_REG, (_, ansi) => { | ||
colors.push(`color:${ANSI_COLORS[ansi] || 'inherit'}`); | ||
return '%c'; | ||
}); | ||
console[out](message, ...colors); | ||
} | ||
var browser = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
'default': BrowserLogger | ||
}); | ||
} | ||
function getAugmentedNamespace(n) { | ||
if (n.__esModule) return n; | ||
var a = Object.defineProperty({}, '__esModule', {value: true}); | ||
Object.keys(n).forEach(function (k) { | ||
var d = Object.getOwnPropertyDescriptor(n, k); | ||
Object.defineProperty(a, k, d.get ? d : { | ||
enumerable: true, | ||
get: function () { | ||
return n[k]; | ||
} | ||
}); | ||
}); | ||
return a; | ||
} | ||
var browser = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
'default': BrowserLogger | ||
}); | ||
var require$$0 = /*@__PURE__*/getAugmentedNamespace(browser); | ||
var require$$0 = /*@__PURE__*/getAugmentedNamespace(browser); | ||
var require$$1 = /*@__PURE__*/getAugmentedNamespace(logger$1); | ||
var require$$1 = /*@__PURE__*/getAugmentedNamespace(logger$1); | ||
const { | ||
default: Logger | ||
} = process.env.__PERCY_BROWSERIFIED__ ? require$$0 : require$$1; | ||
const { | ||
default: Logger | ||
} = process.env.__PERCY_BROWSERIFIED__ ? require$$0 : require$$1; | ||
function logger(name) { | ||
return new Logger().group(name); | ||
} | ||
function logger(name) { | ||
return new Logger().group(name); | ||
} | ||
Object.assign(logger, { | ||
format: (...args) => new Logger().format(...args), | ||
query: (...args) => new Logger().query(...args), | ||
connect: (...args) => new Logger().connect(...args), | ||
remote: (...args) => new Logger().remote(...args), | ||
Object.assign(logger, { | ||
format: (...args) => new Logger().format(...args), | ||
query: (...args) => new Logger().query(...args), | ||
connect: (...args) => new Logger().connect(...args), | ||
remote: (...args) => new Logger().remote(...args), | ||
loglevel(level, flags = {}) { | ||
if (flags.verbose) level = 'debug';else if (flags.quiet) level = 'warn';else if (flags.silent) level = 'silent'; | ||
return new Logger().loglevel(level); | ||
} | ||
loglevel(level, flags = {}) { | ||
if (flags.verbose) level = 'debug';else if (flags.quiet) level = 'warn';else if (flags.silent) level = 'silent'; | ||
return new Logger().loglevel(level); | ||
} | ||
}); | ||
Object.defineProperties(logger, { | ||
Logger: { | ||
get: () => Logger | ||
}, | ||
stdout: { | ||
get: () => Logger.stdout | ||
}, | ||
stderr: { | ||
get: () => Logger.stderr | ||
} | ||
}); | ||
var src = logger; | ||
}); | ||
Object.defineProperties(logger, { | ||
Logger: { | ||
get: () => Logger | ||
}, | ||
stdout: { | ||
get: () => Logger.stdout | ||
}, | ||
stderr: { | ||
get: () => Logger.stderr | ||
} | ||
}); | ||
var src = logger; | ||
exports.default = src; | ||
exports.default = src; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
@@ -411,0 +411,0 @@ }(this.PercyLogger = this.PercyLogger || {})); |
{ | ||
"name": "@percy/logger", | ||
"version": "1.0.0-beta.49", | ||
"version": "1.0.0-beta.50", | ||
"license": "MIT", | ||
@@ -29,3 +29,3 @@ "main": "dist/index.js", | ||
}, | ||
"gitHead": "7fec5dc3d381622dc47c236ec7e81aeb6ec1c035" | ||
"gitHead": "06f92d0d9226ddae14f7af276abbfe0c77083603" | ||
} |
(function() { | ||
this.PercyLogger = this.PercyLogger || {}; | ||
this.PercyLogger.TestHelpers = (function (logger, require$$1) { | ||
'use strict'; | ||
this.PercyLogger.TestHelpers = (function (require$$0, require$$2) { | ||
'use strict'; | ||
const process = (typeof globalThis !== "undefined" && globalThis.process) || {}; | ||
process.env = process.env || {}; | ||
process.env.__PERCY_BROWSERIFIED__ = true; | ||
const process = (typeof globalThis !== "undefined" && globalThis.process) || {}; | ||
process.env = process.env || {}; | ||
process.env.__PERCY_BROWSERIFIED__ = true; | ||
globalThis.process = globalThis.process || process; | ||
globalThis.process = globalThis.process || process; | ||
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
var logger__default = /*#__PURE__*/_interopDefaultLegacy(logger); | ||
var require$$1__default = /*#__PURE__*/_interopDefaultLegacy(require$$1); | ||
var require$$0__default = /*#__PURE__*/_interopDefaultLegacy(require$$0); | ||
var require$$2__default = /*#__PURE__*/_interopDefaultLegacy(require$$2); | ||
const { | ||
assign, | ||
entries | ||
} = Object; // matches ansi escape sequences | ||
function getAugmentedNamespace(n) { | ||
if (n.__esModule) return n; | ||
var a = Object.defineProperty({}, '__esModule', {value: true}); | ||
Object.keys(n).forEach(function (k) { | ||
var d = Object.getOwnPropertyDescriptor(n, k); | ||
Object.defineProperty(a, k, d.get ? d : { | ||
enumerable: true, | ||
get: function () { | ||
return n[k]; | ||
} | ||
}); | ||
}); | ||
return a; | ||
} | ||
const ANSI_REG$1 = 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 | ||
const { | ||
assign, | ||
entries | ||
} = Object; // matches ansi escape sequences | ||
const ANSI_COLORS = { | ||
'31m': 'red', | ||
'33m': 'yellow', | ||
'34m': 'blue', | ||
'35m': 'magenta', | ||
'90m': 'grey' | ||
}; // colorize each line of a string using an ansi escape sequence | ||
const ANSI_REG$1 = 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 | ||
const LINE_REG = /^.*$/gm; | ||
const ANSI_COLORS = { | ||
'31m': 'red', | ||
'33m': 'yellow', | ||
'34m': 'blue', | ||
'35m': 'magenta', | ||
'90m': 'grey' | ||
}; // colorize each line of a string using an ansi escape sequence | ||
function colorize(code, str) { | ||
return str.replace(LINE_REG, line => `\u001b[${code}${line}\u001b[39m`); | ||
} // map ansi colors to bound colorize functions | ||
const LINE_REG = /^.*$/gm; | ||
function colorize(code, str) { | ||
return str.replace(LINE_REG, line => `\u001b[${code}${line}\u001b[39m`); | ||
} // map ansi colors to bound colorize functions | ||
const colors = entries(ANSI_COLORS).reduce((colors, [code, name]) => { | ||
return assign(colors, { | ||
[name]: colorize.bind(null, code) | ||
}); | ||
}, {}); | ||
var util = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
ANSI_REG: ANSI_REG$1, | ||
ANSI_COLORS: ANSI_COLORS, | ||
colors: colors | ||
}); | ||
const colors = entries(ANSI_COLORS).reduce((colors, [code, name]) => { | ||
return assign(colors, { | ||
[name]: colorize.bind(null, code) | ||
}); | ||
}, {}); | ||
function getAugmentedNamespace(n) { | ||
if (n.__esModule) return n; | ||
var a = Object.defineProperty({}, '__esModule', {value: true}); | ||
Object.keys(n).forEach(function (k) { | ||
var d = Object.getOwnPropertyDescriptor(n, k); | ||
Object.defineProperty(a, k, d.get ? d : { | ||
enumerable: true, | ||
get: function () { | ||
return n[k]; | ||
} | ||
}); | ||
}); | ||
return a; | ||
} | ||
var util = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
ANSI_REG: ANSI_REG$1, | ||
ANSI_COLORS: ANSI_COLORS, | ||
colors: colors | ||
}); | ||
var require$$0 = /*@__PURE__*/getAugmentedNamespace(util); | ||
var require$$1 = /*@__PURE__*/getAugmentedNamespace(util); | ||
const { | ||
ANSI_REG | ||
} = require$$0; | ||
const { | ||
Logger | ||
} = logger__default['default']; | ||
const ELAPSED_REG = /\s\S*?\(\d+ms\)\S*/; | ||
const NEWLINE_REG = /\r\n/g; | ||
const LASTLINE_REG = /\n$/; | ||
const logger = require$$0__default['default']; | ||
const { | ||
ANSI_REG | ||
} = require$$1; | ||
const { | ||
Logger | ||
} = logger; | ||
const ELAPSED_REG = /\s\S*?\(\d+ms\)\S*/; | ||
const NEWLINE_REG = /\r\n/g; | ||
const LASTLINE_REG = /\n$/; | ||
function sanitizeLog(str, { | ||
ansi, | ||
elapsed | ||
} = {}) { | ||
// normalize line endings | ||
str = str.replace(NEWLINE_REG, '\n'); // strip ansi colors | ||
function sanitizeLog(str, { | ||
ansi, | ||
elapsed | ||
} = {}) { | ||
// normalize line endings | ||
str = str.replace(NEWLINE_REG, '\n'); // strip ansi colors | ||
if (!ansi) str = str.replace(ANSI_REG, ''); // strip elapsed time | ||
if (!ansi) str = str.replace(ANSI_REG, ''); // strip elapsed time | ||
if (!elapsed) str = str.replace(ELAPSED_REG, ''); // strip trailing line endings | ||
if (!elapsed) str = str.replace(ELAPSED_REG, ''); // strip trailing line endings | ||
return str.replace(LASTLINE_REG, ''); | ||
} | ||
return str.replace(LASTLINE_REG, ''); | ||
} | ||
function TestIO(data, options) { | ||
if (!process.env.__PERCY_BROWSERIFIED__) { | ||
let { | ||
Writable | ||
} = require$$1__default['default']; | ||
return Object.assign(new Writable(), { | ||
_write(chunk, encoding, callback) { | ||
data.push(sanitizeLog(chunk.toString(), options)); | ||
callback(); | ||
} | ||
function TestIO(data, options) { | ||
if (!process.env.__PERCY_BROWSERIFIED__) { | ||
let { | ||
Writable | ||
} = require$$2__default['default']; | ||
return Object.assign(new Writable(), { | ||
_write(chunk, encoding, callback) { | ||
data.push(sanitizeLog(chunk.toString(), options)); | ||
callback(); | ||
} | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
function spy(object, method, func) { | ||
if (object[method].reset) { | ||
object[method].reset(); | ||
return object[method]; | ||
} | ||
function spy(object, method, func) { | ||
if (object[method].reset) { | ||
object[method].reset(); | ||
return object[method]; | ||
} | ||
let spy = Object.assign(function spy(...args) { | ||
spy.calls.push(args); | ||
if (func) return func.apply(this, args); | ||
}, { | ||
restore: () => object[method] = spy.originalValue, | ||
reset: () => spy.calls.length = 0, | ||
originalValue: object[method], | ||
calls: [] | ||
}); | ||
object[method] = spy; | ||
return spy; | ||
} | ||
let spy = Object.assign(function spy(...args) { | ||
spy.calls.push(args); | ||
if (func) return func.apply(this, args); | ||
}, { | ||
restore: () => object[method] = spy.originalValue, | ||
reset: () => spy.calls.length = 0, | ||
originalValue: object[method], | ||
calls: [] | ||
}); | ||
object[method] = spy; | ||
return spy; | ||
} | ||
const helpers = { | ||
constructor: Logger, | ||
loglevel: logger__default['default'].loglevel, | ||
stdout: [], | ||
stderr: [], | ||
const helpers = { | ||
constructor: Logger, | ||
loglevel: logger.loglevel, | ||
stdout: [], | ||
stderr: [], | ||
get messages() { | ||
return Logger.instance && Logger.instance.messages; | ||
}, | ||
get messages() { | ||
return Logger.instance && Logger.instance.messages; | ||
}, | ||
mock(options) { | ||
helpers.reset(); | ||
helpers.options = options; | ||
mock(options) { | ||
helpers.reset(); | ||
helpers.options = options; | ||
if (!process.env.__PERCY_BROWSERIFIED__) { | ||
Logger.stdout = TestIO(helpers.stdout, options); | ||
Logger.stderr = TestIO(helpers.stderr, options); | ||
} else { | ||
spy(Logger.prototype, 'write', function (lvl, msg) { | ||
let stdio = lvl === 'info' ? 'stdout' : 'stderr'; | ||
helpers[stdio].push(sanitizeLog(msg, helpers.options)); | ||
return this.write.originalValue.call(this, lvl, msg); | ||
}); | ||
spy(console, 'log'); | ||
spy(console, 'warn'); | ||
spy(console, 'error'); | ||
} | ||
}, | ||
if (!process.env.__PERCY_BROWSERIFIED__) { | ||
Logger.stdout = TestIO(helpers.stdout, options); | ||
Logger.stderr = TestIO(helpers.stderr, options); | ||
} else { | ||
spy(Logger.prototype, 'write', function (lvl, msg) { | ||
let stdio = lvl === 'info' ? 'stdout' : 'stderr'; | ||
helpers[stdio].push(sanitizeLog(msg, helpers.options)); | ||
return this.write.originalValue.call(this, lvl, msg); | ||
}); | ||
spy(console, 'log'); | ||
spy(console, 'warn'); | ||
spy(console, 'error'); | ||
} | ||
}, | ||
reset() { | ||
delete Logger.instance; | ||
helpers.stdout.length = 0; | ||
helpers.stderr.length = 0; | ||
reset() { | ||
delete Logger.instance; | ||
helpers.stdout.length = 0; | ||
helpers.stderr.length = 0; | ||
if (console.log.reset) { | ||
console.log.reset(); | ||
console.warn.reset(); | ||
console.error.reset(); | ||
} | ||
}, | ||
if (console.log.reset) { | ||
console.log.reset(); | ||
console.warn.reset(); | ||
console.error.reset(); | ||
} | ||
}, | ||
dump() { | ||
if (!helpers.messages || !helpers.messages.size) return; | ||
if (console.log.and) console.log.and.callThrough(); | ||
dump() { | ||
if (!helpers.messages || !helpers.messages.size) return; | ||
if (console.log.and) console.log.and.callThrough(); | ||
let write = m => process.env.__PERCY_BROWSERIFIED__ ? console.log(m) : process.stderr.write(`${m}\n`); | ||
let write = m => process.env.__PERCY_BROWSERIFIED__ ? console.log(m) : process.stderr.write(`${m}\n`); | ||
let logs = Array.from(helpers.messages); | ||
logger__default['default'].loglevel('debug'); | ||
write(logger__default['default'].format('--- DUMPING LOGS ---', 'testing', 'warn')); | ||
logs.reduce((lastlog, { | ||
debug, | ||
level, | ||
message, | ||
timestamp | ||
}) => { | ||
write(logger__default['default'].format(message, debug, level, timestamp - lastlog)); | ||
return timestamp; | ||
}, logs[0].timestamp); | ||
} | ||
let logs = Array.from(helpers.messages); | ||
logger.loglevel('debug'); | ||
write(logger.format('--- DUMPING LOGS ---', 'testing', 'warn')); | ||
logs.reduce((lastlog, { | ||
debug, | ||
level, | ||
message, | ||
timestamp | ||
}) => { | ||
write(logger.format(message, debug, level, timestamp - lastlog)); | ||
return timestamp; | ||
}, logs[0].timestamp); | ||
} | ||
}; | ||
var helpers_1 = helpers; | ||
}; | ||
var helpers_1 = helpers; | ||
return helpers_1; | ||
return helpers_1; | ||
@@ -193,0 +194,0 @@ }(PercyLogger, null)); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
892
36789