@monodeploy/logging
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -0,1 +1,3 @@ | ||
import { Writable } from 'stream'; | ||
import { Report, StreamReport } from '@yarnpkg/core'; | ||
export * from './invariants'; | ||
@@ -8,3 +10,6 @@ export declare const LOG_LEVELS: { | ||
}; | ||
declare type Logger = (...args: unknown[]) => void; | ||
declare type Logger = (message: string | Error, { report, extras }: { | ||
report?: Report | null; | ||
extras?: string; | ||
}) => void; | ||
declare const logger: { | ||
@@ -16,3 +21,7 @@ debug: Logger; | ||
setDryRun: (value: boolean) => void; | ||
createReportStream: ({ report, prefix, }: { | ||
report: StreamReport; | ||
prefix: string | null; | ||
}) => [Writable, Promise<boolean>]; | ||
}; | ||
export default logger; |
@@ -11,3 +11,3 @@ "use strict"; | ||
var _chalk = _interopRequireDefault(require("chalk")); | ||
var _core = require("@yarnpkg/core"); | ||
@@ -27,5 +27,2 @@ var _invariants = require("./invariants"); | ||
}); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const LOG_LEVELS = { | ||
@@ -38,8 +35,2 @@ DEBUG: 0, | ||
exports.LOG_LEVELS = LOG_LEVELS; | ||
const levelToColour = { | ||
[LOG_LEVELS.DEBUG]: _chalk.default.magenta, | ||
[LOG_LEVELS.INFO]: _chalk.default.reset, | ||
[LOG_LEVELS.WARNING]: _chalk.default.yellow, | ||
[LOG_LEVELS.ERROR]: _chalk.default.red | ||
}; | ||
const loggerOpts = { | ||
@@ -51,25 +42,32 @@ dryRun: false | ||
const envLogLevel = Number(process.env.MONODEPLOY_LOG_LEVEL); | ||
return isNaN(envLogLevel) ? LOG_LEVELS.WARNING : envLogLevel; | ||
return isNaN(envLogLevel) ? LOG_LEVELS.INFO : envLogLevel; | ||
}; | ||
const createLogger = level => (...args) => { | ||
const createLogger = level => (message, { | ||
report, | ||
extras | ||
}) => { | ||
if (getCurrentLogLevel() > level) return; | ||
const date = new Date(); | ||
const timestamp = `[${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}:${String(date.getSeconds()).padStart(2, '0')}.${String(date.getMilliseconds()).padStart(3, '0')}]`; | ||
const colour = levelToColour[level]; | ||
const line = [_chalk.default.yellow(timestamp)]; | ||
if (loggerOpts.dryRun && level === LOG_LEVELS.INFO) { | ||
line.push(_chalk.default.grey('[Dry Run]')); | ||
if (!report) { | ||
console.log(message); | ||
return; | ||
} | ||
line.push(colour(args[0])); | ||
const logFn = level >= LOG_LEVELS.WARNING ? console.error : console.log; | ||
logFn(line.join(' ')); | ||
if (message instanceof Error) { | ||
report.reportExceptionOnce(message); | ||
return; | ||
} | ||
if (args.length > 1) { | ||
for (const arg of args.slice(1)) { | ||
logFn(colour(arg)); | ||
} | ||
if (level === LOG_LEVELS.ERROR) { | ||
report.reportError(_core.MessageName.UNNAMED, message); | ||
} else if (level === LOG_LEVELS.WARNING) { | ||
report.reportWarning(_core.MessageName.UNNAMED, message); | ||
} else if (level === LOG_LEVELS.INFO || level === LOG_LEVELS.DEBUG) { | ||
report.reportInfo(_core.MessageName.UNNAMED, loggerOpts.dryRun ? `[Dry Run] ${message}` : message); | ||
} | ||
if (extras) { | ||
report.reportInfo(_core.MessageName.UNNAMED, extras); | ||
} | ||
}; | ||
@@ -81,2 +79,22 @@ | ||
const createReportStream = ({ | ||
report, | ||
prefix | ||
}) => { | ||
const streamReporter = report.createStreamReporter(prefix); | ||
const defaultStream = new _core.miscUtils.DefaultStream(); | ||
defaultStream.pipe(streamReporter, { | ||
end: false | ||
}); | ||
defaultStream.on('finish', () => { | ||
streamReporter.end(); | ||
}); | ||
const promise = new Promise(resolve => { | ||
streamReporter.on('finish', () => { | ||
resolve(defaultStream.active); | ||
}); | ||
}); | ||
return [defaultStream, promise]; | ||
}; | ||
const logger = { | ||
@@ -87,5 +105,6 @@ debug: createLogger(LOG_LEVELS.DEBUG), | ||
error: createLogger(LOG_LEVELS.ERROR), | ||
setDryRun | ||
setDryRun, | ||
createReportStream | ||
}; | ||
var _default = logger; | ||
exports.default = _default; |
"use strict"; | ||
var _core = require("@yarnpkg/core"); | ||
var _ = _interopRequireWildcard(require(".")); | ||
@@ -9,8 +11,32 @@ | ||
class CollectReport extends _core.ThrowReport { | ||
reportInfo(name, text) { | ||
console.log(text); | ||
} | ||
reportWarning(name, text) { | ||
console.warn(text); | ||
} | ||
reportError(name, text) { | ||
console.error(text); | ||
} | ||
reportExceptionOnce(error) { | ||
console.error(error); | ||
} | ||
} | ||
describe('Logging', () => { | ||
const origLogLevel = process.env.MONODEPLOY_LOG_LEVEL; | ||
let report; | ||
beforeAll(() => { | ||
jest.spyOn(console, 'log').mockImplementation(); | ||
jest.spyOn(console, 'warn').mockImplementation(); | ||
jest.spyOn(console, 'error').mockImplementation(); | ||
}); | ||
beforeEach(() => { | ||
report = new CollectReport(); | ||
}); | ||
afterEach(() => { | ||
@@ -28,68 +54,102 @@ jest.resetAllMocks(); | ||
it('respects log level', () => { | ||
process.env.MONODEPLOY_LOG_LEVEL = _.LOG_LEVELS.DEBUG; | ||
process.env.MONODEPLOY_LOG_LEVEL = String(_.LOG_LEVELS.DEBUG); | ||
_.default.debug('m1'); | ||
_.default.debug('m1', { | ||
report | ||
}); | ||
_.default.info('m2'); | ||
_.default.info('m2', { | ||
report | ||
}); | ||
_.default.warning('m3'); | ||
_.default.warning('m3', { | ||
report | ||
}); | ||
_.default.error('m4'); | ||
_.default.error('m4', { | ||
report | ||
}); | ||
expect(console.log).toBeCalledWith(expect.stringContaining('m1')); | ||
expect(console.log).toBeCalledWith(expect.stringContaining('m2')); | ||
expect(console.error).toBeCalledWith(expect.stringContaining('m3')); | ||
expect(console.warn).toBeCalledWith(expect.stringContaining('m3')); | ||
expect(console.error).toBeCalledWith(expect.stringContaining('m4')); | ||
jest.resetAllMocks(); | ||
process.env.MONODEPLOY_LOG_LEVEL = _.LOG_LEVELS.INFO; | ||
process.env.MONODEPLOY_LOG_LEVEL = String(_.LOG_LEVELS.INFO); | ||
_.default.debug('m1'); | ||
_.default.debug('m1', { | ||
report | ||
}); | ||
_.default.info('m2'); | ||
_.default.info('m2', { | ||
report | ||
}); | ||
_.default.warning('m3'); | ||
_.default.warning('m3', { | ||
report | ||
}); | ||
_.default.error('m4'); | ||
_.default.error('m4', { | ||
report | ||
}); | ||
expect(console.log).not.toBeCalledWith(expect.stringContaining('m1')); | ||
expect(console.log).toBeCalledWith(expect.stringContaining('m2')); | ||
expect(console.error).toBeCalledWith(expect.stringContaining('m3')); | ||
expect(console.warn).toBeCalledWith(expect.stringContaining('m3')); | ||
expect(console.error).toBeCalledWith(expect.stringContaining('m4')); | ||
jest.resetAllMocks(); | ||
process.env.MONODEPLOY_LOG_LEVEL = _.LOG_LEVELS.WARNING; | ||
process.env.MONODEPLOY_LOG_LEVEL = String(_.LOG_LEVELS.WARNING); | ||
_.default.debug('m1'); | ||
_.default.debug('m1', { | ||
report | ||
}); | ||
_.default.info('m2'); | ||
_.default.info('m2', { | ||
report | ||
}); | ||
_.default.warning('m3'); | ||
_.default.warning('m3', { | ||
report | ||
}); | ||
_.default.error('m4'); | ||
_.default.error('m4', { | ||
report | ||
}); | ||
expect(console.log).not.toBeCalledWith(expect.stringContaining('m1')); | ||
expect(console.log).not.toBeCalledWith(expect.stringContaining('m2')); | ||
expect(console.error).toBeCalledWith(expect.stringContaining('m3')); | ||
expect(console.warn).toBeCalledWith(expect.stringContaining('m3')); | ||
expect(console.error).toBeCalledWith(expect.stringContaining('m4')); | ||
jest.resetAllMocks(); | ||
process.env.MONODEPLOY_LOG_LEVEL = _.LOG_LEVELS.ERROR; | ||
process.env.MONODEPLOY_LOG_LEVEL = String(_.LOG_LEVELS.ERROR); | ||
_.default.debug('m1'); | ||
_.default.debug('m1', { | ||
report | ||
}); | ||
_.default.info('m2'); | ||
_.default.info('m2', { | ||
report | ||
}); | ||
_.default.warning('m3'); | ||
_.default.warning('m3', { | ||
report | ||
}); | ||
_.default.error('m4'); | ||
_.default.error('m4', { | ||
report | ||
}); | ||
expect(console.log).not.toBeCalledWith(expect.stringContaining('m1')); | ||
expect(console.log).not.toBeCalledWith(expect.stringContaining('m2')); | ||
expect(console.error).not.toBeCalledWith(expect.stringContaining('m3')); | ||
expect(console.warn).not.toBeCalledWith(expect.stringContaining('m3')); | ||
expect(console.error).toBeCalledWith(expect.stringContaining('m4')); | ||
}); | ||
it('prints dry run prefix when dry run configured', () => { | ||
process.env.MONODEPLOY_LOG_LEVEL = _.LOG_LEVELS.INFO; | ||
process.env.MONODEPLOY_LOG_LEVEL = String(_.LOG_LEVELS.INFO); | ||
_.default.setDryRun(true); | ||
_.default.info('m1'); | ||
_.default.info('m1', { | ||
report | ||
}); | ||
@@ -102,3 +162,5 @@ expect(console.log).toBeCalledWith(expect.stringContaining('m1')); | ||
_.default.info('m1'); | ||
_.default.info('m1', { | ||
report | ||
}); | ||
@@ -108,43 +170,61 @@ expect(console.log).toBeCalledWith(expect.stringContaining('m1')); | ||
}); | ||
it('prints all args', () => { | ||
process.env.MONODEPLOY_LOG_LEVEL = _.LOG_LEVELS.INFO; | ||
_.default.info('m1', 'm2', 'm3'); | ||
expect(console.log).toBeCalledWith(expect.stringContaining('m1')); | ||
expect(console.log).toBeCalledWith(expect.stringContaining('m2')); | ||
expect(console.log).toBeCalledWith(expect.stringContaining('m3')); | ||
}); | ||
it('defaults to log level warning', () => { | ||
it('defaults to log level info', () => { | ||
delete process.env.MONODEPLOY_LOG_LEVEL; | ||
_.default.debug('m1'); | ||
_.default.debug('m1', { | ||
report | ||
}); | ||
_.default.info('m2'); | ||
_.default.info('m2', { | ||
report | ||
}); | ||
_.default.warning('m3'); | ||
_.default.warning('m3', { | ||
report | ||
}); | ||
_.default.error('m4'); | ||
_.default.error('m4', { | ||
report | ||
}); | ||
expect(console.log).not.toBeCalledWith(expect.stringContaining('m1')); | ||
expect(console.log).not.toBeCalledWith(expect.stringContaining('m2')); | ||
expect(console.error).toBeCalledWith(expect.stringContaining('m3')); | ||
expect(console.log).toBeCalledWith(expect.stringContaining('m2')); | ||
expect(console.warn).toBeCalledWith(expect.stringContaining('m3')); | ||
expect(console.error).toBeCalledWith(expect.stringContaining('m4')); | ||
}); | ||
it('falls back to log level warning', () => { | ||
it('falls back to log level info', () => { | ||
process.env.MONODEPLOY_LOG_LEVEL = 'not-a-number'; | ||
_.default.debug('m1'); | ||
_.default.debug('m1', { | ||
report | ||
}); | ||
_.default.info('m2'); | ||
_.default.info('m2', { | ||
report | ||
}); | ||
_.default.warning('m3'); | ||
_.default.warning('m3', { | ||
report | ||
}); | ||
_.default.error('m4'); | ||
_.default.error('m4', { | ||
report | ||
}); | ||
expect(console.log).not.toBeCalledWith(expect.stringContaining('m1')); | ||
expect(console.log).not.toBeCalledWith(expect.stringContaining('m2')); | ||
expect(console.error).toBeCalledWith(expect.stringContaining('m3')); | ||
expect(console.log).toBeCalledWith(expect.stringContaining('m2')); | ||
expect(console.warn).toBeCalledWith(expect.stringContaining('m3')); | ||
expect(console.error).toBeCalledWith(expect.stringContaining('m4')); | ||
}); | ||
it('prints extras', () => { | ||
process.env.MONODEPLOY_LOG_LEVEL = String(_.LOG_LEVELS.INFO); | ||
_.default.info('m1', { | ||
report, | ||
extras: 'extra info' | ||
}); | ||
expect(console.log).toBeCalledWith(expect.stringContaining('m1')); | ||
expect(console.log).toBeCalledWith(expect.stringContaining('extra info')); | ||
}); | ||
}); |
{ | ||
"name": "@monodeploy/logging", | ||
"version": "0.0.5", | ||
"version": "0.1.1", | ||
"repository": "git@github.com:tophat/monodeploy.git", | ||
@@ -24,3 +24,3 @@ "author": "Top Hat Monocle Corp. <opensource@tophat.com>", | ||
"dependencies": { | ||
"chalk": "^4.1.0" | ||
"@yarnpkg/core": "^2.4.0" | ||
}, | ||
@@ -27,0 +27,0 @@ "devDependencies": { |
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
13063
347
0
+ Added@yarnpkg/core@^2.4.0
+ Added@arcanis/slice-ansi@1.1.1(transitive)
+ Added@nodelib/fs.scandir@2.1.5(transitive)
+ Added@nodelib/fs.stat@2.0.5(transitive)
+ Added@nodelib/fs.walk@1.2.8(transitive)
+ Added@sindresorhus/is@4.6.0(transitive)
+ Added@szmarczak/http-timer@4.0.6(transitive)
+ Added@types/cacheable-request@6.0.3(transitive)
+ Added@types/emscripten@1.40.0(transitive)
+ Added@types/http-cache-semantics@4.0.4(transitive)
+ Added@types/keyv@3.1.4(transitive)
+ Added@types/node@13.13.52(transitive)
+ Added@types/responselike@1.0.3(transitive)
+ Added@types/semver@7.5.8(transitive)
+ Added@types/treeify@1.0.3(transitive)
+ Added@yarnpkg/core@2.4.0(transitive)
+ Added@yarnpkg/fslib@2.10.4(transitive)
+ Added@yarnpkg/json-proxy@2.1.1(transitive)
+ Added@yarnpkg/libzip@2.3.0(transitive)
+ Added@yarnpkg/parsers@2.6.0(transitive)
+ Added@yarnpkg/pnp@2.3.2(transitive)
+ Added@yarnpkg/shell@2.4.1(transitive)
+ Addedany-promise@1.3.0(transitive)
+ Addedargparse@1.0.10(transitive)
+ Addedarray-union@2.1.0(transitive)
+ Addedasap@2.0.6(transitive)
+ Addedbase64-js@1.5.1(transitive)
+ Addedbinjumper@0.1.4(transitive)
+ Addedbl@4.1.0(transitive)
+ Addedbraces@3.0.3(transitive)
+ Addedbuffer@5.7.1(transitive)
+ Addedcacheable-lookup@5.0.4(transitive)
+ Addedcacheable-request@7.0.4(transitive)
+ Addedcall-bind@1.0.8(transitive)
+ Addedcall-bind-apply-helpers@1.0.2(transitive)
+ Addedcall-bound@1.0.3(transitive)
+ Addedcamelcase@5.3.1(transitive)
+ Addedchalk@3.0.0(transitive)
+ Addedci-info@2.0.0(transitive)
+ Addedclipanion@2.6.2(transitive)
+ Addedclone-response@1.0.3(transitive)
+ Addedcross-spawn@7.0.3(transitive)
+ Addeddecompress-response@6.0.0(transitive)
+ Addeddefer-to-connect@2.0.1(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddefine-properties@1.2.1(transitive)
+ Addeddiff@4.0.2(transitive)
+ Addeddir-glob@3.0.1(transitive)
+ Addeddunder-proto@1.0.1(transitive)
+ Addedend-of-stream@1.1.01.4.4(transitive)
+ Addedes-define-property@1.0.1(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedes-object-atoms@1.1.1(transitive)
+ Addedesprima@4.0.1(transitive)
+ Addedfast-glob@3.3.3(transitive)
+ Addedfastq@1.19.0(transitive)
+ Addedfill-range@7.1.1(transitive)
+ Addedfs-constants@1.0.0(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.3.0(transitive)
+ Addedget-proto@1.0.1(transitive)
+ Addedget-stream@5.2.0(transitive)
+ Addedglob-parent@5.1.2(transitive)
+ Addedglobby@11.1.0(transitive)
+ Addedgopd@1.2.0(transitive)
+ Addedgot@11.8.6(transitive)
+ Addedgrapheme-splitter@1.0.4(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-symbols@1.1.0(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhttp-cache-semantics@4.1.1(transitive)
+ Addedhttp2-wrapper@1.0.3(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addedignore@5.3.2(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedis@3.3.0(transitive)
+ Addedis-callable@1.2.7(transitive)
+ Addedis-extglob@2.1.1(transitive)
+ Addedis-glob@4.0.3(transitive)
+ Addedis-number@7.0.0(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedjs-yaml@3.14.1(transitive)
+ Addedjson-buffer@3.0.1(transitive)
+ Addedjson-file-plus@3.3.2(transitive)
+ Addedkeyv@4.5.4(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedlowercase-keys@2.0.0(transitive)
+ Addedmath-intrinsics@1.1.0(transitive)
+ Addedmerge2@1.4.1(transitive)
+ Addedmicromatch@4.0.8(transitive)
+ Addedmimic-response@1.0.13.1.0(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedmkdirp@0.5.6(transitive)
+ Addednode.extend@2.0.3(transitive)
+ Addednormalize-url@6.1.0(transitive)
+ Addedobject-keys@1.1.1(transitive)
+ Addedobject.assign@4.1.7(transitive)
+ Addedonce@1.3.31.4.0(transitive)
+ Addedp-cancelable@2.1.1(transitive)
+ Addedp-limit@2.3.0(transitive)
+ Addedp-try@2.2.0(transitive)
+ Addedpath-key@3.1.1(transitive)
+ Addedpath-type@4.0.0(transitive)
+ Addedpicomatch@2.3.1(transitive)
+ Addedpluralize@7.0.0(transitive)
+ Addedpretty-bytes@5.6.0(transitive)
+ Addedpromise@8.3.0(transitive)
+ Addedpromise-deferred@2.0.4(transitive)
+ Addedpromiseback@2.0.3(transitive)
+ Addedpump@3.0.2(transitive)
+ Addedqueue-microtask@1.2.3(transitive)
+ Addedquick-lru@5.1.1(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedresolve-alpn@1.2.1(transitive)
+ Addedresponselike@2.0.1(transitive)
+ Addedreusify@1.0.4(transitive)
+ Addedrun-parallel@1.2.0(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsemver@7.7.1(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedshebang-command@2.0.0(transitive)
+ Addedshebang-regex@3.0.0(transitive)
+ Addedslash@3.0.0(transitive)
+ Addedsprintf-js@1.0.3(transitive)
+ Addedstream-buffers@3.0.3(transitive)
+ Addedstream-to-array@2.3.0(transitive)
+ Addedstream-to-promise@2.2.0(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addedtar-stream@2.2.0(transitive)
+ Addedto-regex-range@5.0.1(transitive)
+ Addedtreeify@1.1.0(transitive)
+ Addedtslib@1.14.1(transitive)
+ Addedtunnel@0.0.6(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedwhich@2.0.2(transitive)
+ Addedwrappy@1.0.2(transitive)
- Removedchalk@^4.1.0
- Removedchalk@4.1.2(transitive)