Compare commits
1 Commits
developmen
...
improvemen
Author | SHA1 | Date |
---|---|---|
Alexander Chan | 9210867983 |
|
@ -1,20 +1,27 @@
|
||||||
name: Tests
|
name: Tests
|
||||||
|
|
||||||
on:
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
push:
|
push:
|
||||||
branches-ignore:
|
branches:
|
||||||
- development/**
|
- 'feature/**'
|
||||||
- q/*/**
|
- 'documentation/**'
|
||||||
|
- 'improvement/**'
|
||||||
|
- 'bugfix/**'
|
||||||
|
- 'w/**'
|
||||||
|
- 'q/**'
|
||||||
|
- 'hotfix/**'
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
tests:
|
tests:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkokut
|
- name: Checkokut
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v2
|
||||||
- name: Install deps
|
- name: Install deps
|
||||||
run: sudo apt-get update -q
|
run: sudo apt-get update -q
|
||||||
- uses: actions/setup-node@v4
|
- uses: actions/setup-node@v2
|
||||||
with:
|
with:
|
||||||
node-version: '16'
|
node-version: '16'
|
||||||
- name: Install Yarn
|
- name: Install Yarn
|
||||||
|
|
|
@ -1,27 +1,12 @@
|
||||||
interface WerelogsConfigOptions {
|
interface config {}
|
||||||
level?: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal';
|
|
||||||
dump?: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal';
|
|
||||||
streams?: object[];
|
|
||||||
}
|
|
||||||
|
|
||||||
declare class WerelogsConfig {
|
interface LogDictionnary {
|
||||||
constructor(config?: WerelogsConfigOptions);
|
|
||||||
reset(): WerelogsConfig;
|
|
||||||
update(config: WerelogsConfig): WerelogsConfig;
|
|
||||||
logger: any;
|
|
||||||
level: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal';
|
|
||||||
dump: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal';
|
|
||||||
end: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal';
|
|
||||||
}
|
|
||||||
|
|
||||||
interface LogDictionary {
|
|
||||||
httpMethod?: string;
|
httpMethod?: string;
|
||||||
httpURL?: string;
|
httpURL?: string;
|
||||||
[field: string]: any;
|
[field: string]: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
declare module 'werelogs' {
|
declare class RequestLogger {
|
||||||
export class RequestLogger {
|
|
||||||
constructor(
|
constructor(
|
||||||
logger: any,
|
logger: any,
|
||||||
logLevel: string,
|
logLevel: string,
|
||||||
|
@ -31,35 +16,30 @@ declare module 'werelogs' {
|
||||||
);
|
);
|
||||||
getUids(): Array<string>;
|
getUids(): Array<string>;
|
||||||
getSerializedUids(): string;
|
getSerializedUids(): string;
|
||||||
addDefaultFields(fields: LogDictionary): LogDictionary;
|
addDefaultFields(fields: LogDictionnary): LogDictionnary;
|
||||||
trace(msg: string, data?: LogDictionary): void;
|
trace(msg: string, data?: LogDictionnary): void;
|
||||||
debug(msg: string, data?: LogDictionary): void;
|
debug(msg: string, data?: LogDictionnary): void;
|
||||||
info(msg: string, data?: LogDictionary): void;
|
info(msg: string, data?: LogDictionnary): void;
|
||||||
warn(msg: string, data?: LogDictionary): void;
|
warn(msg: string, data?: LogDictionnary): void;
|
||||||
error(msg: string, data?: LogDictionary): void;
|
error(msg: string, data?: LogDictionnary): void;
|
||||||
fatal(msg: string, data?: LogDictionary): void;
|
fatal(msg: string, data?: LogDictionnary): void;
|
||||||
end(msg: string, data?: LogDictionary): void;
|
end(msg: string, data?: LogDictionnary): void;
|
||||||
errorEnd(msg: string, data?:LogDictionary): void;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
declare module 'werelogs' {
|
||||||
export class Logger {
|
export class Logger {
|
||||||
name: string;
|
name: string;
|
||||||
constructor(name: string);
|
|
||||||
|
constructor(name: string, config?: config);
|
||||||
|
setLevel(levelName: string): void;
|
||||||
|
setDumpLevelThreshold(levelName: string): void;
|
||||||
newRequestLogger(uids?: string|Array<string>): RequestLogger;
|
newRequestLogger(uids?: string|Array<string>): RequestLogger;
|
||||||
newRequestLoggerFromSerializedUids(uids: string): RequestLogger;
|
newRequestLoggerFromSerializedUids(uids: string): RequestLogger;
|
||||||
trace(msg: string, data?: LogDictionary): void;
|
trace(msg: string, data?: LogDictionnary): void;
|
||||||
debug(msg: string, data?: LogDictionary): void;
|
debug(msg: string, data?: LogDictionnary): void;
|
||||||
info(msg: string, data?: LogDictionary): void;
|
info(msg: string, data?: LogDictionnary): void;
|
||||||
warn(msg: string, data?: LogDictionary): void;
|
warn(msg: string, data?: LogDictionnary): void;
|
||||||
error(msg: string, data?: LogDictionary): void;
|
error(msg: string, data?: LogDictionnary): void;
|
||||||
fatal(msg: string, data?: LogDictionary): void;
|
fatal(msg: string, data?: LogDictionnary): void;
|
||||||
}
|
|
||||||
|
|
||||||
export function configure(config: WerelogsConfigOptions): void;
|
|
||||||
|
|
||||||
export class API {
|
|
||||||
constructor(config: WerelogsConfigOptions);
|
|
||||||
reconfigure(config: WerelogsConfigOptions): void;
|
|
||||||
Logger: Logger;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
37
index.js
37
index.js
|
@ -1,5 +1,4 @@
|
||||||
const API = require('./lib/api.js');
|
const API = require('./lib/api.js');
|
||||||
const stderrUtils = require('./lib/stderrUtils');
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* For convenience purposes, we provide an already instanciated API; so that
|
* For convenience purposes, we provide an already instanciated API; so that
|
||||||
|
@ -12,40 +11,4 @@ module.exports = {
|
||||||
Logger: werelogs.Logger,
|
Logger: werelogs.Logger,
|
||||||
configure: werelogs.reconfigure.bind(werelogs),
|
configure: werelogs.reconfigure.bind(werelogs),
|
||||||
Werelogs: API,
|
Werelogs: API,
|
||||||
/**
|
|
||||||
* Timestamp logs going to stderr
|
|
||||||
*
|
|
||||||
* @example <caption>Simplest usage</caption>
|
|
||||||
* ```
|
|
||||||
* const { stderrUtils } = require('werelogs');
|
|
||||||
* stderrUtils.catchAndTimestampStderr();
|
|
||||||
* ```
|
|
||||||
*
|
|
||||||
* @example <caption>Manage process exit</caption>
|
|
||||||
* ```
|
|
||||||
* const { stderrUtils } = require('werelogs');
|
|
||||||
* // set exitCode to null to keep process running on uncaughtException
|
|
||||||
* stderrUtils.catchAndTimestampStderr(undefined, null);
|
|
||||||
* // application init
|
|
||||||
* process.on('uncaughtException', (err) => {
|
|
||||||
* // custom handling, close connections, files
|
|
||||||
* this.worker.kill(); // or process.exit(1);
|
|
||||||
* });
|
|
||||||
* // Note you could use prependListener to execute your callback first
|
|
||||||
* // and then let stderrUtils exit the process.
|
|
||||||
* ```
|
|
||||||
*
|
|
||||||
* @example <caption>Custom listener</caption>
|
|
||||||
* ```
|
|
||||||
* const { stderrUtils } = require('werelogs');
|
|
||||||
* stderrUtils.catchAndTimestampWarning();
|
|
||||||
* // application init
|
|
||||||
* process.on('uncaughtException', (err, origin) => {
|
|
||||||
* stderrUtils.printErrorWithTimestamp(err, origin);
|
|
||||||
* // close and stop everything
|
|
||||||
* process.exit(1);
|
|
||||||
* });
|
|
||||||
* ```
|
|
||||||
*/
|
|
||||||
stderrUtils,
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
|
|
||||||
// eslint-disable-line strict
|
// eslint-disable-line strict
|
||||||
|
|
||||||
|
const assert = require('assert');
|
||||||
|
|
||||||
const LogLevel = require('./LogLevel.js');
|
const LogLevel = require('./LogLevel.js');
|
||||||
const Utils = require('./Utils.js');
|
const Utils = require('./Utils.js');
|
||||||
|
|
||||||
|
@ -21,6 +23,9 @@ class EndLogger {
|
||||||
}
|
}
|
||||||
|
|
||||||
augmentedLog(level, msg, data) {
|
augmentedLog(level, msg, data) {
|
||||||
|
assert.strictEqual(this.logger.elapsedTime, null, 'The logger\'s'
|
||||||
|
+ 'end() wrapper should not be called more than'
|
||||||
|
+ ' once.');
|
||||||
// We can alter current instance, as it won't be usable after this
|
// We can alter current instance, as it won't be usable after this
|
||||||
// call.
|
// call.
|
||||||
this.fields = objectCopy(this.fields, data || {});
|
this.fields = objectCopy(this.fields, data || {});
|
||||||
|
@ -365,6 +370,8 @@ class RequestLogger {
|
||||||
if (msg === undefined && data === undefined) {
|
if (msg === undefined && data === undefined) {
|
||||||
return this.endLogger;
|
return this.endLogger;
|
||||||
}
|
}
|
||||||
|
assert.strictEqual(this.elapsedTime, null, 'The "end()" logging method '
|
||||||
|
+ 'should not be called more than once.');
|
||||||
return this.log(this.endLevel, msg, data, true);
|
return this.log(this.endLevel, msg, data, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -381,6 +388,8 @@ class RequestLogger {
|
||||||
* @returns {undefined}
|
* @returns {undefined}
|
||||||
*/
|
*/
|
||||||
errorEnd(msg, data) {
|
errorEnd(msg, data) {
|
||||||
|
assert.strictEqual(this.elapsedTime, null, 'The "end()" logging method '
|
||||||
|
+ 'should not be called more than once.');
|
||||||
return this.log('error', msg, data, true);
|
return this.log('error', msg, data, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -440,12 +449,6 @@ class RequestLogger {
|
||||||
// eslint-disable-next-line camelcase
|
// eslint-disable-next-line camelcase
|
||||||
fields.req_id = serializeUids(this.uids);
|
fields.req_id = serializeUids(this.uids);
|
||||||
if (endFlag) {
|
if (endFlag) {
|
||||||
if (this.elapsedTime !== null) {
|
|
||||||
// reset elapsedTime to avoid an infinite recursion
|
|
||||||
// while logging the error
|
|
||||||
this.elapsedTime = null;
|
|
||||||
this.error('RequestLogger.end() has been called more than once');
|
|
||||||
}
|
|
||||||
this.elapsedTime = process.hrtime(this.startTime);
|
this.elapsedTime = process.hrtime(this.startTime);
|
||||||
// eslint-disable-next-line camelcase
|
// eslint-disable-next-line camelcase
|
||||||
fields.elapsed_ms = this.elapsedTime[0] * 1000
|
fields.elapsed_ms = this.elapsedTime[0] * 1000
|
||||||
|
|
|
@ -3,15 +3,6 @@
|
||||||
|
|
||||||
const os = require('os');
|
const os = require('os');
|
||||||
const safeJSONStringify = require('safe-json-stringify');
|
const safeJSONStringify = require('safe-json-stringify');
|
||||||
const fastJSONStringify = require('fast-safe-stringify')
|
|
||||||
|
|
||||||
function errorStackReplacer(key, value) {
|
|
||||||
if (value instanceof Error) {
|
|
||||||
return value.stack;
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This function safely stringifies JSON. If an exception occcurs (due to
|
* This function safely stringifies JSON. If an exception occcurs (due to
|
||||||
* circular references, exceptions thrown from object getters etc.), the module
|
* circular references, exceptions thrown from object getters etc.), the module
|
||||||
|
@ -23,13 +14,12 @@ function errorStackReplacer(key, value) {
|
||||||
function safeStringify(obj) {
|
function safeStringify(obj) {
|
||||||
let str;
|
let str;
|
||||||
try {
|
try {
|
||||||
// Try to stringify the object (fast version)
|
str = JSON.stringify(obj);
|
||||||
str = fastJSONStringify(obj, errorStackReplacer);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// fallback to remove circular object references or other exceptions
|
// fallback to remove circular object references or other exceptions
|
||||||
// eslint-disable-next-line no-param-reassign
|
// eslint-disable-next-line no-param-reassign
|
||||||
obj.unsafeJSON = true;
|
obj.unsafeJSON = true;
|
||||||
return safeJSONStringify(obj, errorStackReplacer);
|
return safeJSONStringify(obj);
|
||||||
}
|
}
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
@ -79,9 +69,8 @@ class SimpleLogger {
|
||||||
logFields.hostname = this.hostname;
|
logFields.hostname = this.hostname;
|
||||||
logFields.pid = process.pid;
|
logFields.pid = process.pid;
|
||||||
|
|
||||||
const safeString = safeStringify(logFields);
|
|
||||||
this.streams.forEach(s => s.stream
|
this.streams.forEach(s => s.stream
|
||||||
.write(`${safeString}\n`));
|
.write(`${safeStringify(logFields)}\n`));
|
||||||
}
|
}
|
||||||
|
|
||||||
info(fields, message) {
|
info(fields, message) {
|
||||||
|
|
|
@ -1,106 +0,0 @@
|
||||||
/**
|
|
||||||
* @returns {string} a timestamp in ISO format YYYY-MM-DDThh:mm:ss.sssZ
|
|
||||||
*/
|
|
||||||
const defaultTimestamp = () => new Date().toISOString();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Prints on stderr a timestamp, the origin and the error
|
|
||||||
*
|
|
||||||
* If no other instructions are needed on uncaughtException,
|
|
||||||
* consider using `catchAndTimestampStderr` directly.
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* process.on('uncaughtException', (err, origin) => {
|
|
||||||
* printErrorWithTimestamp(err, origin);
|
|
||||||
* // server.close();
|
|
||||||
* // file.close();
|
|
||||||
* process.nextTick(() => process.exit(1));
|
|
||||||
* });
|
|
||||||
* // Don't forget to timestamp warning
|
|
||||||
* catchAndTimestampWarning();
|
|
||||||
* @param {Error} err see process event uncaughtException
|
|
||||||
* @param {uncaughtException|unhandledRejection} origin see process event
|
|
||||||
* @param {string} [date=`defaultTimestamp()`] Date to print
|
|
||||||
* @returns {boolean} see process.stderr.write
|
|
||||||
*/
|
|
||||||
function printErrorWithTimestamp(
|
|
||||||
err, origin, date = defaultTimestamp(),
|
|
||||||
) {
|
|
||||||
return process.stderr.write(`${date}: ${origin}:\n${err.stack}\n`);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Prefer using `catchAndTimestampStderr` instead of this function.
|
|
||||||
*
|
|
||||||
* Adds listener for uncaughtException to print with timestamp.
|
|
||||||
*
|
|
||||||
* If you want to manage the end of the process, you can set exitCode to null.
|
|
||||||
* Or use `printErrorWithTimestamp` in your own uncaughtException listener.
|
|
||||||
*
|
|
||||||
* @param {Function} [dateFct=`defaultTimestamp`] Fct returning a formatted date
|
|
||||||
* @param {*} [exitCode=1] On uncaughtException, if not null, `process.exit`
|
|
||||||
* will be called with this value
|
|
||||||
* @returns {undefined}
|
|
||||||
*/
|
|
||||||
function catchAndTimestampUncaughtException(
|
|
||||||
dateFct = defaultTimestamp, exitCode = 1,
|
|
||||||
) {
|
|
||||||
process.on('uncaughtException', (err, origin) => {
|
|
||||||
printErrorWithTimestamp(err, origin, dateFct());
|
|
||||||
if (exitCode !== null) {
|
|
||||||
process.nextTick(() => process.exit(exitCode));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Forces the use of `--trace-warnings` and adds a date in warning.detail
|
|
||||||
* The warning will be printed by the default `onWarning`
|
|
||||||
*
|
|
||||||
* @param {string} [dateFct=`defaultTimestamp`] Fct returning a formatted date
|
|
||||||
* @returns {undefined}
|
|
||||||
*/
|
|
||||||
function catchAndTimestampWarning(dateFct = defaultTimestamp) {
|
|
||||||
process.traceProcessWarnings = true;
|
|
||||||
// must be executed first, before the default `onWarning`
|
|
||||||
process.prependListener('warning', warning => {
|
|
||||||
if (warning.detail) {
|
|
||||||
// eslint-disable-next-line no-param-reassign
|
|
||||||
warning.detail += `\nAbove Warning Date: ${dateFct()}`;
|
|
||||||
} else {
|
|
||||||
// eslint-disable-next-line no-param-reassign
|
|
||||||
warning.detail = `Above Warning Date: ${dateFct()}`;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds listener for uncaughtException and warning to print them with timestamp.
|
|
||||||
*
|
|
||||||
* If you want to manage the end of the process, you can set exitCode to null.
|
|
||||||
* Or use `printErrorWithTimestamp` in your own uncaughtException listener.
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* const { stderrUtils } = require('werelogs');
|
|
||||||
* // first instruction in your index.js or entrypoint
|
|
||||||
* stderrUtils.catchAndTimestampStderr();
|
|
||||||
*
|
|
||||||
* @param {Function} [dateFct=`defaultTimestamp`] Fct returning a formatted date
|
|
||||||
* @param {*} [exitCode=1] On uncaughtException, if not null, `process.exit`
|
|
||||||
* will be called with this value
|
|
||||||
* @returns {undefined}
|
|
||||||
*/
|
|
||||||
function catchAndTimestampStderr(
|
|
||||||
dateFct = defaultTimestamp, exitCode = 1,
|
|
||||||
) {
|
|
||||||
catchAndTimestampUncaughtException(dateFct, exitCode);
|
|
||||||
catchAndTimestampWarning(dateFct);
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
defaultTimestamp,
|
|
||||||
printErrorWithTimestamp,
|
|
||||||
catchAndTimestampUncaughtException,
|
|
||||||
catchAndTimestampWarning,
|
|
||||||
catchAndTimestampStderr,
|
|
||||||
};
|
|
|
@ -3,7 +3,7 @@
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=10"
|
"node": ">=10"
|
||||||
},
|
},
|
||||||
"version": "8.1.5",
|
"version": "7.4.1",
|
||||||
"description": "An efficient raw JSON logging library aimed at micro-services architectures.",
|
"description": "An efficient raw JSON logging library aimed at micro-services architectures.",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
@ -33,20 +33,19 @@
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/scality/werelogs#readme",
|
"homepage": "https://github.com/scality/werelogs#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"fast-safe-stringify": "^2.1.1",
|
|
||||||
"safe-json-stringify": "^1.2.0"
|
"safe-json-stringify": "^1.2.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"eslint": "^7.32.0",
|
"eslint": "^7.32.0",
|
||||||
"eslint-config-airbnb": "^18.2.1",
|
"eslint-config-airbnb": "^18.2.1",
|
||||||
"eslint-config-scality": "git+https://git.yourcmc.ru/vitalif/zenko-eslint-config-scality.git",
|
"eslint-config-scality": "scality/Guidelines#71a059ad",
|
||||||
"eslint-plugin-import": "^2.22.1",
|
"eslint-plugin-import": "^2.22.1",
|
||||||
"eslint-plugin-jsx-a11y": "^6.4.1",
|
"eslint-plugin-jsx-a11y": "^6.4.1",
|
||||||
"eslint-plugin-react": "^7.26.0",
|
"eslint-plugin-react": "^7.26.0",
|
||||||
"eslint-plugin-react-hooks": "^4.2.0",
|
"eslint-plugin-react-hooks": "^4.2.0",
|
||||||
"jsdoc": "^3.4.3",
|
"jsdoc": "^3.4.3",
|
||||||
"markdownlint-cli": "^0.27.1",
|
"markdownlint-cli": "^0.27.1",
|
||||||
"mocha": ">=3.1.2",
|
"mocha": "^8.4.0",
|
||||||
"nyc": "^15.1.0"
|
"nyc": "^15.1.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -414,21 +414,6 @@ describe('RequestLogger', () => {
|
||||||
assert.strictEqual(dummyLogger.ops[0][1][0].endValue, 42);
|
assert.strictEqual(dummyLogger.ops[0][1][0].endValue, 42);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should log an error in addition to request logs when end() called more than once',
|
|
||||||
done => {
|
|
||||||
const dummyLogger = new DummyLogger();
|
|
||||||
const reqLogger = new RequestLogger(dummyLogger, 'trace', 'fatal');
|
|
||||||
reqLogger.end().info('after first call to end()');
|
|
||||||
reqLogger.end().debug('after second call to end()');
|
|
||||||
assert.strictEqual(dummyLogger.ops.length, 3);
|
|
||||||
assert.strictEqual(dummyLogger.ops[0][0], 'info');
|
|
||||||
assert.strictEqual(dummyLogger.ops[0][1][1], 'after first call to end()');
|
|
||||||
assert.strictEqual(dummyLogger.ops[1][0], 'error');
|
|
||||||
assert.strictEqual(dummyLogger.ops[2][0], 'debug');
|
|
||||||
assert.strictEqual(dummyLogger.ops[2][1][1], 'after second call to end()');
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Log History dumped when logging floor level reached', () => {
|
describe('Log History dumped when logging floor level reached', () => {
|
||||||
|
|
|
@ -1,17 +0,0 @@
|
||||||
#!/usr/bin/env node
|
|
||||||
// Convert string args into primitive value
|
|
||||||
const fromStr = (str, primitive) => (str === `${primitive}` ? primitive : str);
|
|
||||||
const date = fromStr(process.argv[2], undefined);
|
|
||||||
const exitCode = fromStr(fromStr(process.argv[3], null), undefined);
|
|
||||||
|
|
||||||
const { stderrUtils } = require('../../../../index');
|
|
||||||
|
|
||||||
stderrUtils.catchAndTimestampStderr(
|
|
||||||
date ? () => date : undefined,
|
|
||||||
exitCode,
|
|
||||||
);
|
|
||||||
|
|
||||||
process.emitWarning('TestWarningMessage');
|
|
||||||
// This will print warning after printing error before exit
|
|
||||||
throw new Error('TestingError');
|
|
||||||
|
|
|
@ -1,23 +0,0 @@
|
||||||
#!/usr/bin/env node
|
|
||||||
// Convert string args into primitive value
|
|
||||||
const fromStr = (str, primitive) => (str === `${primitive}` ? primitive : str);
|
|
||||||
const date = fromStr(process.argv[2], undefined);
|
|
||||||
const exitCode = fromStr(fromStr(process.argv[3], null), undefined);
|
|
||||||
const promise = fromStr(process.argv[4], true);
|
|
||||||
|
|
||||||
const { stderrUtils } = require('../../../../index');
|
|
||||||
|
|
||||||
stderrUtils.catchAndTimestampUncaughtException(
|
|
||||||
date ? () => date : undefined,
|
|
||||||
exitCode,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Executed if process does not exit, process is in undefined behavior (bad)
|
|
||||||
// eslint-disable-next-line no-console
|
|
||||||
setTimeout(() => console.log('EXECUTED AFTER UNCAUGHT EXCEPTION'), 1);
|
|
||||||
|
|
||||||
if (promise === true) {
|
|
||||||
Promise.reject();
|
|
||||||
} else {
|
|
||||||
throw new Error('TestingError');
|
|
||||||
}
|
|
|
@ -1,38 +0,0 @@
|
||||||
#!/usr/bin/env node
|
|
||||||
// Convert string args into primitive value
|
|
||||||
const fromStr = (str, primitive) => (str === `${primitive}` ? primitive : str);
|
|
||||||
const date = fromStr(process.argv[2], undefined);
|
|
||||||
const name = fromStr(process.argv[3], undefined);
|
|
||||||
const code = fromStr(process.argv[4], undefined);
|
|
||||||
const detail = fromStr(process.argv[5], undefined);
|
|
||||||
|
|
||||||
const { stderrUtils } = require('../../../../index');
|
|
||||||
|
|
||||||
stderrUtils.catchAndTimestampWarning(
|
|
||||||
date ? () => date : undefined,
|
|
||||||
);
|
|
||||||
|
|
||||||
const warning = new Error('TestWarningMessage');
|
|
||||||
|
|
||||||
if (name) warning.name = name;
|
|
||||||
if (code) warning.code = code;
|
|
||||||
if (detail) warning.detail = detail;
|
|
||||||
|
|
||||||
process.emitWarning(warning);
|
|
||||||
|
|
||||||
/*
|
|
||||||
Examples:
|
|
||||||
|
|
||||||
(node:203831) Error: TestWarningMessage
|
|
||||||
at Object.<anonymous> (catchWarning.js:15:17)
|
|
||||||
...
|
|
||||||
at node:internal/main/run_main_module:22:47
|
|
||||||
Above Warning Date: 2024-06-26T16:32:55.505Z
|
|
||||||
|
|
||||||
(node:205151) [TEST01] CUSTOM: TestWarningMessage
|
|
||||||
at Object.<anonymous> (catchWarning.js:15:17)
|
|
||||||
...
|
|
||||||
at node:internal/main/run_main_module:22:47
|
|
||||||
Some additional detail
|
|
||||||
Above Warning Date: Tue, 31 Dec 2024 10:20:30 GMT
|
|
||||||
*/
|
|
|
@ -1,309 +0,0 @@
|
||||||
const assert = require('assert');
|
|
||||||
const { execFile } = require('child_process');
|
|
||||||
|
|
||||||
const stderrUtils = require('../../lib/stderrUtils');
|
|
||||||
|
|
||||||
/** Simple regex for ISO YYYY-MM-DDThh:mm:ss.sssZ */
|
|
||||||
// eslint-disable-next-line max-len
|
|
||||||
const defaultDateRegex = /\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+(?:[+-][0-2]\d:[0-5]\d|Z)/;
|
|
||||||
|
|
||||||
// eslint-disable-next-line valid-jsdoc
|
|
||||||
/** another format: Tue, 31 Dec 2024 10:20:30 GMT */
|
|
||||||
const customDate = () => new Date('2024-12-31T10:20:30.444Z').toUTCString();
|
|
||||||
|
|
||||||
describe('stderrUtils', () => {
|
|
||||||
const errStackRegex = /Error: TestingError\n(?:.*\sat\s.*\n)+/;
|
|
||||||
|
|
||||||
describe('defaultTimestamp', () => {
|
|
||||||
it('should match ISO format', () => {
|
|
||||||
assert.match(stderrUtils.defaultTimestamp(), defaultDateRegex);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('printErrorWithTimestamp', () => {
|
|
||||||
let stderrText;
|
|
||||||
const originalStderrWrite = process.stderr.write;
|
|
||||||
const mockedStderrWrite = text => { stderrText = text; return true; };
|
|
||||||
const err = new Error('TestingError');
|
|
||||||
const origin = 'uncaughtException';
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
stderrText = undefined;
|
|
||||||
process.stderr.write = mockedStderrWrite;
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(() => {
|
|
||||||
process.stderr.write = originalStderrWrite;
|
|
||||||
stderrText = undefined;
|
|
||||||
});
|
|
||||||
|
|
||||||
it(
|
|
||||||
'should write to stderr with current date, origin and stacktrace',
|
|
||||||
() => {
|
|
||||||
const written = stderrUtils
|
|
||||||
.printErrorWithTimestamp(err, origin);
|
|
||||||
|
|
||||||
assert.strictEqual(written, true);
|
|
||||||
const [firstLine, errStack] = stderrText.split(':\n');
|
|
||||||
const [errDate, errOrigin] = firstLine.split(': ');
|
|
||||||
|
|
||||||
assert.match(errDate, defaultDateRegex);
|
|
||||||
assert.strictEqual(errOrigin, origin);
|
|
||||||
assert.strictEqual(errStack, `${err.stack}\n`);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
it(
|
|
||||||
'should write to stderr with custom date, origin and stacktrace',
|
|
||||||
() => {
|
|
||||||
const written = stderrUtils
|
|
||||||
.printErrorWithTimestamp(err, origin, customDate());
|
|
||||||
|
|
||||||
assert.strictEqual(written, true);
|
|
||||||
const [firstLine, errStack] = stderrText.split(':\n');
|
|
||||||
const [errDate, errOrigin] = firstLine.split(': ');
|
|
||||||
|
|
||||||
assert.strictEqual(errDate, customDate());
|
|
||||||
assert.strictEqual(errOrigin, origin);
|
|
||||||
assert.strictEqual(errStack, `${err.stack}\n`);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
const execOptions = {
|
|
||||||
cwd: __dirname,
|
|
||||||
// Subprocess should always stop alone
|
|
||||||
// But just in case, kill subprocess after 500ms.
|
|
||||||
// Leave enough time for `nyc` that runs slower.
|
|
||||||
timeout: 500,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Execute in another process to notice the process exit
|
|
||||||
// Therefore, looks more like a functional test
|
|
||||||
const timeoutHint = (ms, retries) =>
|
|
||||||
`Test fixture process timed out after ${ms}ms with ${retries} retries.\n` +
|
|
||||||
'Due to nyc coverage first run slowing down process.\nIncrease execOptions.timeout to fix';
|
|
||||||
|
|
||||||
describe('catchAndTimestampUncaughtException', () => {
|
|
||||||
[
|
|
||||||
{ desc: 'with default date' },
|
|
||||||
{ desc: 'with custom date', date: customDate() },
|
|
||||||
{ desc: 'with custom exitCode 42', exitCode: 42 },
|
|
||||||
{ desc: 'without exit on uncaught exception', exitCode: null },
|
|
||||||
{ desc: 'for unhandled promise', promise: true },
|
|
||||||
].forEach(({
|
|
||||||
desc, date, exitCode, promise,
|
|
||||||
}) => describe(desc, () => {
|
|
||||||
/** for before all hook that doesn't support this.retries */
|
|
||||||
let retries = 4;
|
|
||||||
let err;
|
|
||||||
let stdout;
|
|
||||||
let stderr;
|
|
||||||
let errStack;
|
|
||||||
let errDate;
|
|
||||||
let errOrigin;
|
|
||||||
|
|
||||||
before('run process catchUncaughtException', function beforeAllHook(done) {
|
|
||||||
execFile(
|
|
||||||
'./fixtures/stderrUtils/catchUncaughtException.js',
|
|
||||||
[`${date}`, `${exitCode}`, `${promise}`],
|
|
||||||
execOptions,
|
|
||||||
(subErr, subStdout, subStderr) => {
|
|
||||||
if (subErr?.killed) {
|
|
||||||
retries--;
|
|
||||||
if (retries <= 0) {
|
|
||||||
assert.fail(timeoutHint(execOptions.timeout, retries));
|
|
||||||
}
|
|
||||||
execOptions.timeout *= 2;
|
|
||||||
return beforeAllHook(done);
|
|
||||||
}
|
|
||||||
err = subErr;
|
|
||||||
stdout = subStdout;
|
|
||||||
stderr = subStderr;
|
|
||||||
let firstLine;
|
|
||||||
[firstLine, errStack] = stderr.split(':\n');
|
|
||||||
[errDate, errOrigin] = firstLine.split(': ');
|
|
||||||
done();
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (exitCode === null) {
|
|
||||||
it('should not be an error (or timeout)',
|
|
||||||
() => assert.ifError(err));
|
|
||||||
it('should have stdout (printed after uncaught exception)',
|
|
||||||
() => assert.match(stdout,
|
|
||||||
/^.*EXECUTED AFTER UNCAUGHT EXCEPTION(?:.|\n)*$/));
|
|
||||||
} else {
|
|
||||||
it('should be an error',
|
|
||||||
() => assert.ok(err));
|
|
||||||
it(`should have exitCode ${exitCode || 1}`,
|
|
||||||
() => assert.strictEqual(err.code, exitCode || 1));
|
|
||||||
it('should have empty stdout',
|
|
||||||
() => assert.strictEqual(stdout, ''));
|
|
||||||
}
|
|
||||||
|
|
||||||
it('should have stderr',
|
|
||||||
() => assert.ok(stderr));
|
|
||||||
it('should have date in stderr first line',
|
|
||||||
() => (date
|
|
||||||
? assert.strictEqual(errDate, date)
|
|
||||||
: assert.match(errDate, defaultDateRegex)));
|
|
||||||
|
|
||||||
it('should have origin in stderr first line',
|
|
||||||
() => (promise === true
|
|
||||||
? assert.strictEqual(errOrigin, 'unhandledRejection')
|
|
||||||
: assert.strictEqual(errOrigin, 'uncaughtException')));
|
|
||||||
|
|
||||||
if (!promise) {
|
|
||||||
it('should have stack trace on stderr',
|
|
||||||
() => assert.match(errStack, errStackRegex));
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('catchAndTimestampWarning (also tests node onWarning)', () => {
|
|
||||||
[
|
|
||||||
{ desc: 'with default date' },
|
|
||||||
{ desc: 'with custom date', date: customDate() },
|
|
||||||
{ desc: 'with deprecation warning', name: 'DeprecationWarning' },
|
|
||||||
{
|
|
||||||
desc: 'with custom warning',
|
|
||||||
name: 'CUSTOM',
|
|
||||||
code: 'TEST01',
|
|
||||||
detail: 'Some additional detail',
|
|
||||||
},
|
|
||||||
].forEach(({
|
|
||||||
desc, date, name, code, detail,
|
|
||||||
}) => describe(desc, () => {
|
|
||||||
/** for before all hook that doesn't support this.retries */
|
|
||||||
let retries = 4;
|
|
||||||
let err;
|
|
||||||
let stdout;
|
|
||||||
let stderr;
|
|
||||||
|
|
||||||
before('run process catchWarning', function beforeAllHook(done) {
|
|
||||||
execFile(
|
|
||||||
'./fixtures/stderrUtils/catchWarning.js',
|
|
||||||
[`${date}`, `${name}`, `${code}`, `${detail}`],
|
|
||||||
execOptions,
|
|
||||||
(subErr, subStdout, subStderr) => {
|
|
||||||
if (subErr?.killed) {
|
|
||||||
retries--;
|
|
||||||
if (retries <= 0) {
|
|
||||||
assert.fail(timeoutHint(execOptions.timeout, retries));
|
|
||||||
}
|
|
||||||
execOptions.timeout *= 2;
|
|
||||||
return beforeAllHook(done);
|
|
||||||
}
|
|
||||||
err = subErr;
|
|
||||||
stdout = subStdout;
|
|
||||||
stderr = subStderr;
|
|
||||||
done();
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should not be an error (or timeout)',
|
|
||||||
() => assert.ifError(err));
|
|
||||||
it('should have empty stdout',
|
|
||||||
() => assert.strictEqual(stdout, ''));
|
|
||||||
it('should have stderr',
|
|
||||||
() => assert.ok(stderr));
|
|
||||||
it('should have message on stderr first line, then stack trace',
|
|
||||||
() => assert.match(stderr,
|
|
||||||
/^.*TestWarningMessage\n(?:\s+at\s.*\n)+/));
|
|
||||||
|
|
||||||
if (code) {
|
|
||||||
it('should have code on stderr first line',
|
|
||||||
() => assert.match(stderr, new RegExp(`^.*[${code}]`)));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (name) {
|
|
||||||
it('should have name on stderr first line',
|
|
||||||
() => assert.match(stderr, new RegExp(`^.*${name}:`)));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (detail) {
|
|
||||||
it('should have detail on stderr',
|
|
||||||
() => assert.match(stderr, new RegExp(`.*${detail}.*`)));
|
|
||||||
}
|
|
||||||
|
|
||||||
it(`should have ${date ? 'custom' : 'default'} date on stderr`,
|
|
||||||
() => assert.match(stderr, new RegExp(
|
|
||||||
`\nAbove Warning Date: ${
|
|
||||||
date || defaultDateRegex.source}\n`,
|
|
||||||
)));
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('catchAndTimestampStderr', () => {
|
|
||||||
[
|
|
||||||
{ desc: 'with default date' },
|
|
||||||
{ desc: 'with custom date', date: customDate() },
|
|
||||||
{ desc: 'with exit code', exitCode: 42 },
|
|
||||||
|
|
||||||
].forEach(({
|
|
||||||
desc, date, exitCode,
|
|
||||||
}) => describe(desc, () => {
|
|
||||||
/** for before all hook that doesn't support this.retries */
|
|
||||||
let retries = 4;
|
|
||||||
let err;
|
|
||||||
let stdout;
|
|
||||||
let stderr;
|
|
||||||
|
|
||||||
before('run process catchStderr', function beforeAllHook(done) {
|
|
||||||
execFile(
|
|
||||||
'./fixtures/stderrUtils/catchStderr.js',
|
|
||||||
[`${date}`, `${exitCode}`],
|
|
||||||
execOptions,
|
|
||||||
(subErr, subStdout, subStderr) => {
|
|
||||||
if (subErr?.killed) {
|
|
||||||
retries--;
|
|
||||||
if (retries <= 0) {
|
|
||||||
assert.fail(timeoutHint(execOptions.timeout, retries));
|
|
||||||
}
|
|
||||||
execOptions.timeout *= 2;
|
|
||||||
return beforeAllHook(done);
|
|
||||||
}
|
|
||||||
err = subErr;
|
|
||||||
stdout = subStdout;
|
|
||||||
stderr = subStderr;
|
|
||||||
done();
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should be an error',
|
|
||||||
() => assert.ok(err));
|
|
||||||
it(`should have exitCode ${exitCode || 1}`,
|
|
||||||
() => assert.strictEqual(err.code, exitCode || 1));
|
|
||||||
it('should have empty stdout',
|
|
||||||
() => assert.strictEqual(stdout, ''));
|
|
||||||
|
|
||||||
it('should have stderr',
|
|
||||||
() => assert.ok(stderr));
|
|
||||||
|
|
||||||
// 2024-06-26T15:04:55.364Z: uncaughtException:
|
|
||||||
// Error: TestingError
|
|
||||||
// at Object.<anonymous> (catchStderr.js:16:7)
|
|
||||||
// at node:internal/main/run_main_module:22:47
|
|
||||||
it('should have error date, origin and stacktrace in stderr',
|
|
||||||
() => assert.match(stderr,
|
|
||||||
new RegExp(`${date || defaultDateRegex.source
|
|
||||||
}: uncaughtException:\n${errStackRegex.source}`)));
|
|
||||||
|
|
||||||
// (node:171245) Warning: TestWarningMessage
|
|
||||||
// at Object.<anonymous> (catchStderr.js:14:9)
|
|
||||||
// at node:internal/main/run_main_module:22:47
|
|
||||||
// Above Warning Date: 2024-06-26T15:04:55.365Z
|
|
||||||
it('should have warning with stacktrace in stderr', () => {
|
|
||||||
const trace = 'Warning: TestWarningMessage\n(?:\\s+at\\s.*\n)+';
|
|
||||||
const detail = `(?:.|\n)*?(?<=\n)Above Warning Date: ${
|
|
||||||
date || defaultDateRegex.source}\n`;
|
|
||||||
assert.match(stderr,
|
|
||||||
new RegExp(`${trace}${detail}`));
|
|
||||||
});
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
});
|
|
Loading…
Reference in New Issue