New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@monodeploy/logging

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@monodeploy/logging - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

11

lib/index.d.ts

@@ -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;

71

lib/index.js

@@ -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": {

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