@percy/logger
Advanced tools
Comparing version 1.12.0 to 1.13.0
@@ -9,21 +9,27 @@ import { colors } from './utils.js'; | ||
error: 3 | ||
}; // A PercyLogger instance retains logs in-memory for quick lookups while also writing log | ||
}; | ||
// 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. | ||
export class PercyLogger { | ||
// default log level | ||
level = 'info'; // namespace regular expressions used to determine which debug logs to write | ||
level = 'info'; | ||
// namespace regular expressions used to determine which debug logs to write | ||
namespaces = { | ||
include: [/^.*?$/], | ||
exclude: [] | ||
}; // in-memory store for logs and meta info | ||
}; | ||
messages = new Set(); // track deprecations to limit noisy logging | ||
// in-memory store for logs and meta info | ||
messages = new Set(); | ||
deprecations = new Set(); // static vars can be overriden for testing | ||
// track deprecations to limit noisy logging | ||
deprecations = new Set(); | ||
// static vars can be overriden for testing | ||
static stdout = process.stdout; | ||
static stderr = process.stderr; // Handles setting env var values and returns a singleton | ||
static stderr = process.stderr; | ||
// Handles setting env var values and returns a singleton | ||
constructor() { | ||
@@ -33,3 +39,2 @@ let { | ||
} = this.constructor; | ||
if (process.env.PERCY_DEBUG) { | ||
@@ -40,15 +45,14 @@ instance.debug(process.env.PERCY_DEBUG); | ||
} | ||
this.constructor.instance = instance; | ||
return instance; | ||
} // Change log level at any time or return the current log level | ||
} | ||
// Change log level at any time or return the current log level | ||
loglevel(level) { | ||
if (level) this.level = level; | ||
return this.level; | ||
} // Change namespaces by generating an array of namespace regular expressions from a | ||
} | ||
// Change namespaces by generating an array of namespace regular expressions from a | ||
// comma separated debug string | ||
debug(namespaces) { | ||
@@ -62,3 +66,2 @@ if (this.namespaces.string === namespaces) return; | ||
ns = ns.replace(/:?\*/g, m => m[0] === ':' ? ':?.*?' : '.*?'); | ||
if (ns[0] === '-') { | ||
@@ -69,3 +72,2 @@ namespaces.exclude.push(new RegExp('^' + ns.substr(1) + '$')); | ||
} | ||
return namespaces; | ||
@@ -77,5 +79,5 @@ }, { | ||
}); | ||
} // Creates a new log group and returns level specific functions for logging | ||
} | ||
// Creates a new log group and returns level specific functions for logging | ||
group(name) { | ||
@@ -93,18 +95,16 @@ return Object.keys(LOG_LEVELS).reduce((group, level) => Object.assign(group, { | ||
}); | ||
} // Query for a set of logs by filtering the in-memory store | ||
} | ||
// 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 | ||
} | ||
// Formats messages before they are logged to stdio | ||
format(debug, level, message, elapsed) { | ||
let color = (n, m) => this.isTTY ? colors[n](m) : m; | ||
let begin, | ||
end, | ||
suffix = ''; | ||
end, | ||
suffix = ''; | ||
let label = 'percy'; | ||
if (arguments.length === 1) { | ||
@@ -116,18 +116,19 @@ // format(message) | ||
[level, message] = [null, level]; | ||
} // do not format leading or trailing newlines | ||
} | ||
// do not format leading or trailing newlines | ||
[, begin, message, end] = message.match(LINE_PAD_REGEXP); | ||
[, begin, message, end] = message.match(LINE_PAD_REGEXP); // include debug information | ||
// include debug information | ||
if (this.level === 'debug') { | ||
if (debug) label += `:${debug}`; // include elapsed time since last log | ||
if (debug) label += `:${debug}`; | ||
// include elapsed time since last log | ||
if (elapsed != null) { | ||
suffix = ' ' + color('grey', `(${elapsed}ms)`); | ||
} | ||
} // add colors | ||
} | ||
// add colors | ||
label = color('magenta', label); | ||
if (level === 'error') { | ||
@@ -143,12 +144,11 @@ // red errors | ||
} | ||
return `${begin}[${label}] ${message}${suffix}${end}`; | ||
} // True if stdout is a TTY interface | ||
} | ||
// True if stdout is a TTY interface | ||
get isTTY() { | ||
return !!this.constructor.stdout.isTTY; | ||
} // Replaces the current line with a log message | ||
} | ||
// Replaces the current line with a log message | ||
progress(debug, message, persist) { | ||
@@ -159,3 +159,2 @@ if (!this.shouldLog(debug, 'info')) return; | ||
} = this.constructor; | ||
if (this.isTTY || !this._progress) { | ||
@@ -167,3 +166,2 @@ message && (message = this.format(debug, message)); | ||
} | ||
this._progress = !!message && { | ||
@@ -173,10 +171,10 @@ message, | ||
}; | ||
} // Returns true or false if the level and debug group can write messages to stdio | ||
} | ||
// 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 | ||
} | ||
// Ensures that deprecation messages are not logged more than once | ||
deprecated(debug, message, meta) { | ||
@@ -186,11 +184,12 @@ if (this.deprecations.has(message)) return; | ||
this.log(debug, 'warn', `Warning: ${message}`, meta); | ||
} // Generic log method accepts a debug group, log level, log message, and optional meta | ||
} | ||
// 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-like object | ||
let err = typeof message !== 'string' && (level === 'debug' || level === 'error'); | ||
err && (err = message.message ? Error.prototype.toString.call(message) : message.toString()); // save log entries | ||
err && (err = message.message ? Error.prototype.toString.call(message) : message.toString()); | ||
// save log entries | ||
let timestamp = Date.now(); | ||
@@ -206,8 +205,10 @@ message = err ? message.stack || err : message.toString(); | ||
}; | ||
this.messages.add(entry); // maybe write the message to stdio | ||
this.messages.add(entry); | ||
// maybe write the message to stdio | ||
if (this.shouldLog(debug, level)) { | ||
// unless the loglevel is debug, write shorter error messages | ||
if (err && this.level !== 'debug') message = err; | ||
this.write({ ...entry, | ||
this.write({ | ||
...entry, | ||
message | ||
@@ -217,5 +218,5 @@ }); | ||
} | ||
} // Writes a log entry to stdio based on the loglevel | ||
} | ||
// Writes a log entry to stdio based on the loglevel | ||
write({ | ||
@@ -229,3 +230,2 @@ debug, | ||
var _this$_progress; | ||
let elapsed = timestamp - (this.lastlog || timestamp); | ||
@@ -237,4 +237,5 @@ let msg = this.format(debug, error ? 'error' : level, message, elapsed); | ||
stderr | ||
} = this.constructor; // clear any logged progress | ||
} = this.constructor; | ||
// clear any logged progress | ||
if (progress) { | ||
@@ -244,8 +245,6 @@ stdout.cursorTo(0); | ||
} | ||
(level === 'info' ? stdout : stderr).write(msg + '\n'); | ||
if (!((_this$_progress = this._progress) !== null && _this$_progress !== void 0 && _this$_progress.persist)) delete this._progress;else if (progress) stdout.write(progress.message); | ||
} | ||
} | ||
export default PercyLogger; |
const { | ||
assign, | ||
entries | ||
} = Object; // matches ansi escape sequences | ||
} = Object; | ||
export 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 | ||
// matches ansi escape sequences | ||
export 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 | ||
export const ANSI_COLORS = { | ||
@@ -15,11 +17,11 @@ '91m': 'red', | ||
'90m': 'grey' | ||
}; // colorize each line of a string using an ansi escape sequence | ||
}; | ||
// colorize each line of a string using an ansi escape sequence | ||
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 | ||
} | ||
// map ansi colors to bound colorize functions | ||
export const colors = entries(ANSI_COLORS).reduce((colors, [code, name]) => { | ||
@@ -26,0 +28,0 @@ return assign(colors, { |
{ | ||
"name": "@percy/logger", | ||
"version": "1.12.0", | ||
"version": "1.13.0", | ||
"license": "MIT", | ||
@@ -34,3 +34,3 @@ "repository": { | ||
}, | ||
"gitHead": "4303b74df91f60e36065141289d2ef2277d1d6fc" | ||
"gitHead": "d2e812d14aa446fa580ffa75144a6280627b5a27" | ||
} |
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
15829
352