@percy/logger
Advanced tools
Comparing version 1.0.0-beta.29 to 1.0.0-beta.30
{ | ||
"name": "@percy/logger", | ||
"version": "1.0.0-beta.29", | ||
"version": "1.0.0-beta.30", | ||
"license": "MIT", | ||
"main": "index.js", | ||
"main": "dist/index.js", | ||
"files": [ | ||
"index.js", | ||
"dist", | ||
"test/helper.js" | ||
], | ||
"scripts": { | ||
"build": "babel --root-mode upward src --out-dir dist", | ||
"lint": "eslint --ignore-path ../../.gitignore .", | ||
@@ -21,10 +22,3 @@ "test": "cross-env NODE_ENV=test mocha", | ||
}, | ||
"dependencies": { | ||
"colors": "^1.4.0", | ||
"winston": "^3.3.3" | ||
}, | ||
"devDependencies": { | ||
"strip-ansi": "^6.0.0" | ||
}, | ||
"gitHead": "d8ece55981a5b9ef52abda65783c2aa874ac87e6" | ||
"gitHead": "584a401d8cb349a90d0eea964c6ac2a3682591f1" | ||
} |
# @percy/logger | ||
Common [winston](https://github.com/winstonjs/winston) logger used throughout the Percy CLI. | ||
Common logger used throughout the Percy CLI and SDKs. | ||
@@ -8,4 +8,6 @@ ## Usage | ||
``` js | ||
import log from '@percy/logger' | ||
import logger from '@percy/logger' | ||
const log = logger('foobar') | ||
log.info('info message') | ||
@@ -15,46 +17,73 @@ log.error('error message') | ||
log.debug('debug message') | ||
log.deprecated('deprecation message') | ||
``` | ||
### `#loglevel([level][, flags])` | ||
### `logger([debug])` | ||
Sets or retrieves the log level of the console transport. If the second argument is provided, | ||
`level` is treated as a fallback when all logging flags are `false`. When no arguments are provided, | ||
the method will return the current log level of the console transport. | ||
Creates a group of logging functions that will be associated with the provided `debug` label. When | ||
debug logging is enabled, this label is printed with the `[percy:*]` label and can be filtered via | ||
the `PERCY_DEBUG` environment variable. | ||
``` js | ||
log.loglevel('info', { verbose: true }) | ||
log.loglevel() === 'debug' | ||
PERCY_DEBUG="one:*,*:a,-*:b" | ||
log.loglevel('info', { quiet: true }) | ||
log.loglevel() === 'warn' | ||
logger.loglevel('debug') | ||
log.loglevel('info', { silent: true }) | ||
log.loglevel() === 'silent' | ||
logger('one').debug('test') | ||
logger('one:a').debug('test') | ||
logger('one:b').debug('test') | ||
logger('one:c').debug('test') | ||
logger('two').debug('test') | ||
logger('two:a').debug('test') | ||
log.loglevel('info') | ||
log.loglevel() === 'info' | ||
// only logs from the matching debug string are printed | ||
//=> [percy:one] test | ||
//=> [percy:one:a] test | ||
//=> [percy:one:c] test | ||
//=> [percy:two:a] test | ||
``` | ||
### `#error(errorOrMessage)` | ||
### `logger.loglevel([level][, flags])` | ||
Patched `#error()` method that handles `Error` instance's and similar error objects. When | ||
`#loglevel()` is equal to `debug`, the `Error` instance's stack trace is logged. | ||
Sets or retrieves the log level of the shared logger. If the second argument is provided, `level` is | ||
treated as a fallback when all logging flags are `false`. When no arguments are provided, the method | ||
will return the current log level of the shared logger. | ||
``` js | ||
log.loglevel('debug') | ||
log.error(new Error('example')) | ||
// [percy] Error: example | ||
// at example:2:10 | ||
// ... | ||
logger.loglevel('info', { verbose: true }) | ||
logger.loglevel() === 'debug' | ||
logger.loglevel('info', { quiet: true }) | ||
logger.loglevel() === 'warn' | ||
logger.loglevel('info', { silent: true }) | ||
logget.loglevel() === 'silent' | ||
logger.loglevel('info') | ||
logger.loglevel() === 'info' | ||
``` | ||
### `#query(options)` | ||
### `logger.format(message, debug[, level])` | ||
Patched `#query()` method that is promisified and allows a `filter` function option. | ||
Returns a formatted `message` depending on the provided level and logger's own log level. When | ||
debugging, the `debug` label is added to the prepended `[percy:*]` label. | ||
``` js | ||
let logs = await log.query({ | ||
filter: log => true | ||
// ...other query options (see winston docs) | ||
logger.format('foobar', 'test') | ||
//=> [percy] foobar | ||
logger.loglevel('debug') | ||
logger.format('foobar', 'test', warn') | ||
//=> [percy:test] foobar (yellow for warnings) | ||
``` | ||
### `logger.query(filter)` | ||
Returns an array of logs matching the provided filter function. | ||
``` js | ||
let logs = logger.query(log => { | ||
return log.level === 'debug' && | ||
log.message.match(/foobar/) | ||
}) | ||
``` |
@@ -1,52 +0,53 @@ | ||
const stripAnsi = require('strip-ansi'); | ||
const logger = require('@percy/logger'); | ||
const { Writable } = require('stream'); | ||
const og = { | ||
out: process.stdout.write, | ||
err: process.stderr.write | ||
}; | ||
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=><~]))' | ||
].join('|'), 'g'); | ||
function format(chunk, { ansi = false } = {}) { | ||
// strip ansi and normalize line endings | ||
return (ansi ? chunk : stripAnsi(chunk)).replace('\r\n', '\n'); | ||
} | ||
class TestIO extends Writable { | ||
data = []; | ||
function tryFinally(fn, cb) { | ||
let done = (r, e) => { | ||
if ((cb(), e)) throw e; | ||
return r; | ||
}; | ||
constructor({ ansi } = {}) { | ||
super(); | ||
this.ansi = ansi; | ||
} | ||
let r, e; | ||
try { r = fn(); } catch (err) { e = err; } | ||
if (r && typeof r.then === 'function') { | ||
return r.then(done, e => done(null, e)); | ||
} else { | ||
return done(r, e); | ||
_write(chunk, encoding, callback) { | ||
// strip ansi and normalize line endings | ||
chunk = chunk.toString().replace('\r\n', '\n'); | ||
if (!this.ansi) chunk = chunk.replace(ANSI_REG, ''); | ||
this.data.push(chunk); | ||
callback(); | ||
} | ||
} | ||
const stdio = { | ||
1: [], | ||
2: [], | ||
logger.mock = function mock(options) { | ||
delete logger.instance; | ||
logger(); | ||
capture(fn, options) { | ||
stdio.flush(); | ||
process.stdout.write = chunk => stdio[1].push(format(chunk, options)); | ||
process.stderr.write = chunk => stdio[2].push(format(chunk, options)); | ||
return fn ? tryFinally(fn, stdio.restore) : null; | ||
}, | ||
logger.instance.stdout = new TestIO(options); | ||
logger.instance.stderr = new TestIO(options); | ||
logger.stdout = logger.instance.stdout.data; | ||
logger.stderr = logger.instance.stderr.data; | ||
}; | ||
restore() { | ||
process.stdout.write = og.out; | ||
process.stderr.write = og.err; | ||
}, | ||
logger.clear = function clear() { | ||
logger.stdout.length = 0; | ||
logger.stderr.length = 0; | ||
}; | ||
flush() { | ||
let output = [null, stdio[1], stdio[2]]; | ||
stdio[1] = []; stdio[2] = []; | ||
return output; | ||
} | ||
logger.dump = function dump() { | ||
logger.loglevel('debug'); | ||
process.stderr.write( | ||
logger.format('--- DUMPING LOGS ---', 'testing', 'warn') + '\n' | ||
); | ||
logger.instance.messages.forEach(({ debug, level, message }) => { | ||
process.stderr.write(logger.format(message, debug, level) + '\n'); | ||
}); | ||
}; | ||
module.exports = stdio; | ||
module.exports = logger; |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
11480
0
0
7
197
88
5
1
- Removedcolors@^1.4.0
- Removedwinston@^3.3.3
- Removed@colors/colors@1.6.0(transitive)
- Removed@dabh/diagnostics@2.0.3(transitive)
- Removed@types/triple-beam@1.3.5(transitive)
- Removedasync@3.2.6(transitive)
- Removedcolor@3.2.1(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removedcolor-string@1.9.1(transitive)
- Removedcolors@1.4.0(transitive)
- Removedcolorspace@1.1.4(transitive)
- Removedenabled@2.0.0(transitive)
- Removedfecha@4.2.3(transitive)
- Removedfn.name@1.1.0(transitive)
- Removedinherits@2.0.4(transitive)
- Removedis-arrayish@0.3.2(transitive)
- Removedis-stream@2.0.1(transitive)
- Removedkuler@2.0.0(transitive)
- Removedlogform@2.7.0(transitive)
- Removedms@2.1.3(transitive)
- Removedone-time@1.0.0(transitive)
- Removedreadable-stream@3.6.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsafe-stable-stringify@2.5.0(transitive)
- Removedsimple-swizzle@0.2.2(transitive)
- Removedstack-trace@0.0.10(transitive)
- Removedstring_decoder@1.3.0(transitive)
- Removedtext-hex@1.0.0(transitive)
- Removedtriple-beam@1.4.1(transitive)
- Removedutil-deprecate@1.0.2(transitive)
- Removedwinston@3.17.0(transitive)
- Removedwinston-transport@4.9.0(transitive)