@parcel/logger
Advanced tools
Comparing version 2.0.0-alpha.2.1 to 2.0.0-alpha.3
@@ -7,3 +7,3 @@ "use strict"; | ||
exports.patchConsole = patchConsole; | ||
exports.default = void 0; | ||
exports.PluginLogger = exports.default = void 0; | ||
@@ -14,2 +14,10 @@ var _events = require("@parcel/events"); | ||
var _diagnostic = require("@parcel/diagnostic"); | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
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; } return obj; } | ||
function _classPrivateFieldGet(receiver, privateMap) { var descriptor = privateMap.get(receiver); if (!descriptor) { throw new TypeError("attempted to get private field on non-instance"); } if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; } | ||
@@ -29,35 +37,48 @@ | ||
verbose(message) { | ||
verbose(diagnostic) { | ||
_classPrivateFieldGet(this, _logEmitter).emit({ | ||
type: 'log', | ||
level: 'verbose', | ||
message | ||
diagnostics: Array.isArray(diagnostic) ? diagnostic : [diagnostic] | ||
}); | ||
} | ||
info(message) { | ||
this.log(message); | ||
info(diagnostic) { | ||
this.log(diagnostic); | ||
} | ||
log(message) { | ||
log(diagnostic) { | ||
_classPrivateFieldGet(this, _logEmitter).emit({ | ||
type: 'log', | ||
level: 'info', | ||
message | ||
diagnostics: Array.isArray(diagnostic) ? diagnostic : [diagnostic] | ||
}); | ||
} | ||
warn(err) { | ||
warn(diagnostic) { | ||
_classPrivateFieldGet(this, _logEmitter).emit({ | ||
type: 'log', | ||
level: 'warn', | ||
message: err | ||
diagnostics: Array.isArray(diagnostic) ? diagnostic : [diagnostic] | ||
}); | ||
} | ||
error(err) { | ||
error(input, realOrigin) { | ||
// $FlowFixMe origin is undefined on PluginInputDiagnostic | ||
let diagnostic = (0, _diagnostic.anyToDiagnostic)(input); | ||
if (typeof realOrigin === 'string') { | ||
diagnostic = Array.isArray(diagnostic) ? diagnostic.map(d => { | ||
return _objectSpread({}, d, { | ||
origin: realOrigin | ||
}); | ||
}) : _objectSpread({}, diagnostic, { | ||
origin: realOrigin | ||
}); | ||
} | ||
_classPrivateFieldGet(this, _logEmitter).emit({ | ||
type: 'log', | ||
level: 'error', | ||
message: err | ||
diagnostics: Array.isArray(diagnostic) ? diagnostic : [diagnostic] | ||
}); | ||
@@ -81,2 +102,48 @@ } | ||
exports.default = _default; | ||
class PluginLogger { | ||
constructor(opts) { | ||
_defineProperty(this, "origin", void 0); | ||
this.origin = opts.origin; | ||
} | ||
updateOrigin(diagnostic) { | ||
return Array.isArray(diagnostic) ? diagnostic.map(d => { | ||
return _objectSpread({}, d, { | ||
origin: this.origin | ||
}); | ||
}) : _objectSpread({}, diagnostic, { | ||
origin: this.origin | ||
}); | ||
} | ||
verbose(diagnostic) { | ||
logger.verbose(this.updateOrigin(diagnostic)); | ||
} | ||
info(diagnostic) { | ||
logger.info(this.updateOrigin(diagnostic)); | ||
} | ||
log(diagnostic) { | ||
logger.log(this.updateOrigin(diagnostic)); | ||
} | ||
warn(diagnostic) { | ||
logger.warn(this.updateOrigin(diagnostic)); | ||
} | ||
error(input) { | ||
// $FlowFixMe it should work, don't really wanna mess with the types of logger.error though... | ||
logger.error(input, this.origin); | ||
} | ||
progress(message) { | ||
logger.progress(message); | ||
} | ||
} | ||
exports.PluginLogger = PluginLogger; | ||
let consolePatched = false; // Patch `console` APIs within workers to forward their messages to the Logger | ||
@@ -96,3 +163,3 @@ // at the appropriate levels. | ||
console.log = console.info = (...messages) => { | ||
logger.info(joinLogMessages(messages)); | ||
logger.info(messagesToDiagnostic(messages)); | ||
}; // $FlowFixMe | ||
@@ -103,3 +170,3 @@ | ||
// TODO: dedicated debug level? | ||
logger.verbose(joinLogMessages(messages)); | ||
logger.verbose(messagesToDiagnostic(messages)); | ||
}; // $FlowFixMe | ||
@@ -109,3 +176,3 @@ | ||
console.warn = (...messages) => { | ||
logger.warn(joinLogMessages(messages)); | ||
logger.warn(messagesToDiagnostic(messages)); | ||
}; // $FlowFixMe | ||
@@ -115,3 +182,3 @@ | ||
console.error = (...messages) => { | ||
logger.error(joinLogMessages(messages)); | ||
logger.error(messagesToDiagnostic(messages)); | ||
}; | ||
@@ -124,4 +191,16 @@ /* eslint-enable no-console */ | ||
function messagesToDiagnostic(messages) { | ||
if (messages.length === 1 && messages[0] instanceof Error) { | ||
let error = messages[0]; | ||
return (0, _diagnostic.errorToDiagnostic)(error); | ||
} else { | ||
return { | ||
message: joinLogMessages(messages), | ||
origin: 'console' | ||
}; | ||
} | ||
} | ||
function joinLogMessages(messages) { | ||
return messages.map(m => typeof m === 'string' ? m : (0, _util.inspect)(m)).join(' '); | ||
} |
{ | ||
"name": "@parcel/logger", | ||
"version": "2.0.0-alpha.2.1", | ||
"version": "2.0.0-alpha.3", | ||
"description": "Blazing fast, zero configuration web application bundler", | ||
"main": "lib/Logger.js", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": { | ||
@@ -11,12 +13,12 @@ "type": "git", | ||
}, | ||
"main": "lib/Logger.js", | ||
"source": "src/Logger.js", | ||
"engines": { | ||
"node": ">= 10.0.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
"@parcel/events": "^2.0.0-alpha.2.1" | ||
"@parcel/diagnostic": "^2.0.0-alpha.3", | ||
"@parcel/events": "^2.0.0-alpha.3" | ||
}, | ||
"gitHead": "2c01341d81c41a70cb33acb51fff621353b3e494" | ||
} | ||
"gitHead": "d4b611ac4495787fa31dac40dda15eedb6e31492" | ||
} |
// @flow strict-local | ||
import type {IDisposable, LogEvent} from '@parcel/types'; | ||
import type {Diagnostic, Diagnostifiable} from '@parcel/diagnostic'; | ||
import {ValueEmitter} from '@parcel/events'; | ||
import {inspect} from 'util'; | ||
import {errorToDiagnostic, anyToDiagnostic} from '@parcel/diagnostic'; | ||
export type PluginInputDiagnostic = {| | ||
...Diagnostic, | ||
origin?: string | ||
|}; | ||
class Logger { | ||
@@ -15,35 +22,48 @@ #logEmitter = new ValueEmitter<LogEvent>(); | ||
verbose(message: string): void { | ||
verbose(diagnostic: Diagnostic | Array<Diagnostic>): void { | ||
this.#logEmitter.emit({ | ||
type: 'log', | ||
level: 'verbose', | ||
message | ||
diagnostics: Array.isArray(diagnostic) ? diagnostic : [diagnostic] | ||
}); | ||
} | ||
info(message: string): void { | ||
this.log(message); | ||
info(diagnostic: Diagnostic | Array<Diagnostic>): void { | ||
this.log(diagnostic); | ||
} | ||
log(message: string): void { | ||
log(diagnostic: Diagnostic | Array<Diagnostic>): void { | ||
this.#logEmitter.emit({ | ||
type: 'log', | ||
level: 'info', | ||
message | ||
diagnostics: Array.isArray(diagnostic) ? diagnostic : [diagnostic] | ||
}); | ||
} | ||
warn(err: Error | string): void { | ||
warn(diagnostic: Diagnostic | Array<Diagnostic>): void { | ||
this.#logEmitter.emit({ | ||
type: 'log', | ||
level: 'warn', | ||
message: err | ||
diagnostics: Array.isArray(diagnostic) ? diagnostic : [diagnostic] | ||
}); | ||
} | ||
error(err: Error | string): void { | ||
error(input: Diagnostifiable, realOrigin?: string): void { | ||
// $FlowFixMe origin is undefined on PluginInputDiagnostic | ||
let diagnostic = anyToDiagnostic(input); | ||
if (typeof realOrigin === 'string') { | ||
diagnostic = Array.isArray(diagnostic) | ||
? diagnostic.map(d => { | ||
return {...d, origin: realOrigin}; | ||
}) | ||
: { | ||
...diagnostic, | ||
origin: realOrigin | ||
}; | ||
} | ||
this.#logEmitter.emit({ | ||
type: 'log', | ||
level: 'error', | ||
message: err | ||
diagnostics: Array.isArray(diagnostic) ? diagnostic : [diagnostic] | ||
}); | ||
@@ -64,2 +84,56 @@ } | ||
export type PluginLoggerOpts = {| | ||
origin: string | ||
|}; | ||
export class PluginLogger { | ||
origin: string; | ||
constructor(opts: PluginLoggerOpts) { | ||
this.origin = opts.origin; | ||
} | ||
updateOrigin( | ||
diagnostic: PluginInputDiagnostic | Array<PluginInputDiagnostic> | ||
): Diagnostic | Array<Diagnostic> { | ||
return Array.isArray(diagnostic) | ||
? diagnostic.map(d => { | ||
return {...d, origin: this.origin}; | ||
}) | ||
: {...diagnostic, origin: this.origin}; | ||
} | ||
verbose( | ||
diagnostic: PluginInputDiagnostic | Array<PluginInputDiagnostic> | ||
): void { | ||
logger.verbose(this.updateOrigin(diagnostic)); | ||
} | ||
info(diagnostic: PluginInputDiagnostic | Array<PluginInputDiagnostic>): void { | ||
logger.info(this.updateOrigin(diagnostic)); | ||
} | ||
log(diagnostic: PluginInputDiagnostic | Array<PluginInputDiagnostic>): void { | ||
logger.log(this.updateOrigin(diagnostic)); | ||
} | ||
warn(diagnostic: PluginInputDiagnostic | Array<PluginInputDiagnostic>): void { | ||
logger.warn(this.updateOrigin(diagnostic)); | ||
} | ||
error( | ||
input: | ||
| Diagnostifiable | ||
| PluginInputDiagnostic | ||
| Array<PluginInputDiagnostic> | ||
): void { | ||
// $FlowFixMe it should work, don't really wanna mess with the types of logger.error though... | ||
logger.error(input, this.origin); | ||
} | ||
progress(message: string): void { | ||
logger.progress(message); | ||
} | ||
} | ||
let consolePatched = false; | ||
@@ -79,3 +153,3 @@ | ||
console.log = console.info = (...messages: Array<mixed>) => { | ||
logger.info(joinLogMessages(messages)); | ||
logger.info(messagesToDiagnostic(messages)); | ||
}; | ||
@@ -86,3 +160,3 @@ | ||
// TODO: dedicated debug level? | ||
logger.verbose(joinLogMessages(messages)); | ||
logger.verbose(messagesToDiagnostic(messages)); | ||
}; | ||
@@ -92,3 +166,3 @@ | ||
console.warn = (...messages: Array<mixed>) => { | ||
logger.warn(joinLogMessages(messages)); | ||
logger.warn(messagesToDiagnostic(messages)); | ||
}; | ||
@@ -98,3 +172,3 @@ | ||
console.error = (...messages: Array<mixed>) => { | ||
logger.error(joinLogMessages(messages)); | ||
logger.error(messagesToDiagnostic(messages)); | ||
}; | ||
@@ -106,4 +180,19 @@ | ||
function messagesToDiagnostic( | ||
messages: Array<mixed> | ||
): Diagnostic | Array<Diagnostic> { | ||
if (messages.length === 1 && messages[0] instanceof Error) { | ||
let error: Error = messages[0]; | ||
return errorToDiagnostic(error); | ||
} else { | ||
return { | ||
message: joinLogMessages(messages), | ||
origin: 'console' | ||
}; | ||
} | ||
} | ||
function joinLogMessages(messages: Array<mixed>): string { | ||
return messages.map(m => (typeof m === 'string' ? m : inspect(m))).join(' '); | ||
} |
@@ -19,15 +19,43 @@ // @flow | ||
it('emits log messages with info level', () => { | ||
Logger.log('hello'); | ||
assert(onLog.calledWith({level: 'info', message: 'hello', type: 'log'})); | ||
it('emits log diagnostics with info level', () => { | ||
let diagnostic = { | ||
message: 'hello', | ||
origin: 'logger' | ||
}; | ||
Logger.log(diagnostic); | ||
assert( | ||
onLog.calledWith({ | ||
level: 'info', | ||
diagnostics: [diagnostic], | ||
type: 'log' | ||
}) | ||
); | ||
}); | ||
it('emits warn messages with warn level', () => { | ||
Logger.warn('zomg'); | ||
assert(onLog.calledWith({level: 'warn', message: 'zomg', type: 'log'})); | ||
it('emits warn diagnostic with warn level', () => { | ||
let diagnostic = { | ||
message: 'zomg', | ||
origin: 'logger' | ||
}; | ||
Logger.warn(diagnostic); | ||
assert( | ||
onLog.calledWith({level: 'warn', diagnostics: [diagnostic], type: 'log'}) | ||
); | ||
}); | ||
it('emits error messages with error level', () => { | ||
Logger.error('oh noes'); | ||
assert(onLog.calledWith({level: 'error', message: 'oh noes', type: 'log'})); | ||
let diagnostic = { | ||
message: 'oh noes', | ||
origin: 'logger' | ||
}; | ||
Logger.error(diagnostic); | ||
assert( | ||
onLog.calledWith({level: 'error', diagnostics: [diagnostic], type: 'log'}) | ||
); | ||
}); | ||
@@ -34,0 +62,0 @@ |
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
373453
368
2
7
+ Added@lezer/common@1.2.1(transitive)
+ Added@lezer/lr@1.4.2(transitive)
+ Added@mischnic/json-sourcemap@0.1.1(transitive)
+ Added@parcel/diagnostic@2.12.0(transitive)
+ Addedjson5@2.2.3(transitive)
+ Addednullthrows@1.1.1(transitive)