Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@percy/logger

Package Overview
Dependencies
Maintainers
6
Versions
240
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@percy/logger - npm Package Compare versions

Comparing version 1.0.0-beta.29 to 1.0.0-beta.30

dist/colors.js

16

package.json
{
"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;
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc