-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WRLGS-11: Generic function for timestamp in stderr
- Loading branch information
1 parent
60cc71b
commit 3bbd78b
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* 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=`new Date().toISOString()`] Date to print | ||
*/ | ||
function printErrorWithTimestamp( | ||
err, origin, date = new Date().toISOString() | ||
) { | ||
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 you can use `printErrorWithTimestamp` in your own uncaughtException listener. | ||
* | ||
* @param {string} [date=`new Date().toISOString()`] Date to print | ||
* @param {*} [exitCode=1] On uncaughtException, if not null, `process.exit` will | ||
* be called with this value | ||
* @param {string} [date=`new Date().toISOString()`] | ||
* @param {*} [exitCode=1] | ||
*/ | ||
function catchAndTimestampUncaughtException( | ||
date = new Date().toISOString(), exitCode = 1 | ||
) { | ||
process.on('uncaughtException', function timestampUncaughtException (err, origin) { | ||
printErrorWithTimestamp(err, origin, date); | ||
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} [date=`new Date().toISOString()`] Date to print | ||
*/ | ||
function catchAndTimestampWarning(date = new Date().toISOString()) { | ||
process.traceProcessWarnings = true; | ||
// must be executed first, before the default `onWarning` | ||
process.prependListener('warning', function timestampWarning (warning) { | ||
if (warning.detail) | ||
warning.detail += `\nAbove Warning Date: ${date}` | ||
else | ||
warning.detail = `Above Warning Date: ${date}` | ||
}); | ||
} | ||
|
||
/** | ||
* 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 you can use `printErrorWithTimestamp` in your own uncaughtException listener. | ||
* | ||
* @example | ||
* const { catchAndTimestampStderr } = require('werelogs'); | ||
* // first instruction in your index.js or entrypoint | ||
* catchAndTimestampStderr(); | ||
* | ||
* @param {string} [date=`new Date().toISOString()`] Date to print | ||
* @param {*} [exitCode=1] On uncaughtException, if not null, `process.exit` will | ||
* be called with this value | ||
*/ | ||
function catchAndTimestampStderr( | ||
date = new Date().toISOString(), exitCode = 1 | ||
) { | ||
catchAndTimestampUncaughtException(date, exitCode); | ||
catchAndTimestampWarning(date); | ||
} | ||
|
||
module.exports = { | ||
printErrorWithTimestamp, | ||
catchAndTimestampUncaughtException, | ||
catchAndTimestampWarning, | ||
catchAndTimestampStderr | ||
} |