|
| 1 | +# node-report |
| 2 | + |
| 3 | +Delivers a human-readable diagnostic summary, written to file |
| 4 | +or retrieved as a text. |
| 5 | + |
| 6 | +The report is intended for development, test and production |
| 7 | +use, to capture and preserve information for problem determination. |
| 8 | +It includes JavaScript and native stack traces, heap statistics, |
| 9 | +platform information and resource usage etc. With the report enabled, |
| 10 | +reports can be triggered on unhandled exceptions, fatal errors, signals. |
| 11 | + |
| 12 | +Many capabilities are available as APIs too, for being consumed in-process. |
| 13 | + |
| 14 | +Iin-built node-report function is supported in Node.js versions 11.0.0 onwards. |
| 15 | +For Node.js versions 8 and below, please use it's [npm counterpart][] instead. |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +```bash |
| 20 | +node --report-events fatalerror,signal,exception app.js |
| 21 | +``` |
| 22 | +A report will be triggered automatically on unhandled exceptions and fatal |
| 23 | +error events (for example out of memory errors), and can also be triggered |
| 24 | +by sending a USR2 signal to a Node.js process (not supported on Windows). |
| 25 | + |
| 26 | +A report can also be triggered via an API call from a JavaScript |
| 27 | +application. |
| 28 | + |
| 29 | +```js |
| 30 | +const util = require('util'); |
| 31 | +util.triggerReport(); |
| 32 | +``` |
| 33 | +The content of a report can also be returned as a JavaScript string via an |
| 34 | +API call from a JavaScript application. |
| 35 | + |
| 36 | +```js |
| 37 | +const util = require('util'); |
| 38 | +const report_str = util.getReport(); |
| 39 | +console.log(report_str); |
| 40 | +``` |
| 41 | + |
| 42 | +These APIs can be used without adding the automatic exception |
| 43 | +and fatal error hooks and the signal handler. |
| 44 | + |
| 45 | +Content of the report consists of a header section containing the event |
| 46 | +type, date, time, PID and Node version, sections containing JavaScript and |
| 47 | +native stack traces, a section containing V8 heap information, a section |
| 48 | +containing libuv handle information and an OS platform information section |
| 49 | +showing CPU and memory usage and system limits. An example report can be |
| 50 | +triggered using the Node.js REPL: |
| 51 | + |
| 52 | +```raw |
| 53 | +$ node |
| 54 | +> const util = require('util'); |
| 55 | +> util.triggerReport(); |
| 56 | +Writing Node.js report to file: node-report.20180820.091102.8480.001.txt |
| 57 | +Node.js report completed |
| 58 | +> |
| 59 | +``` |
| 60 | + |
| 61 | +When a report is triggered, start and end messages are issued to stderr |
| 62 | +and the filename of the report is returned to the caller. The default filename |
| 63 | +includes the date, time, PID and a sequence number. Alternatively, a filename |
| 64 | +can be specified as a parameter on the `triggerReport()` call. |
| 65 | + |
| 66 | +```js |
| 67 | +require('util').triggerReport('myReportName'); |
| 68 | +``` |
| 69 | + |
| 70 | +Both `triggerReport()` and `getReport()` can take an optional `Error` object |
| 71 | +as a parameter. If an `Error` object is provided, the message and stack trace |
| 72 | +from the object will be included in the report in the `JavaScript Exception |
| 73 | +Details` section. |
| 74 | +When using node-report to handle errors in a callback or an exception handler |
| 75 | +this allows the report to include the location of the original error as well |
| 76 | +as where it was handled. |
| 77 | +If both a filename and `Error` object are passed to `triggerReport()` the |
| 78 | +`Error` object should be the second parameter. |
| 79 | + |
| 80 | +```js |
| 81 | +try { |
| 82 | + process.chdir('/foo/foo'); |
| 83 | +} catch (err) { |
| 84 | + util.triggerReport(err); |
| 85 | +} |
| 86 | +// ... |
| 87 | +``` |
| 88 | + |
| 89 | +## Configuration |
| 90 | + |
| 91 | +Additional configuration is available using the following APIs: |
| 92 | + |
| 93 | +```js |
| 94 | +const util = require('util'); |
| 95 | +util.setEvents('exception+fatalerror+signal'); |
| 96 | +util.setSignal('SIGUSR2|SIGQUIT'); |
| 97 | +util.setFileName('stdout|stderr|<filename>'); |
| 98 | +util.setDirectory('<full path>'); |
| 99 | +util.setVerbose('yes|no'); |
| 100 | +``` |
| 101 | + |
| 102 | +Configuration on module initialization is also available via |
| 103 | +environment variables: |
| 104 | + |
| 105 | +```bash |
| 106 | +export NODEREPORT_EVENTS=exception+fatalerror+signal+apicall |
| 107 | +export NODEREPORT_SIGNAL=SIGUSR2|SIGQUIT |
| 108 | +export NODEREPORT_FILENAME=stdout|stderr|<filename> |
| 109 | +export NODEREPORT_DIRECTORY=<full path> |
| 110 | +export NODEREPORT_VERBOSE=yes|no |
| 111 | +``` |
| 112 | + |
| 113 | +Detailed API documentation can be found under [`util`][] section. |
| 114 | + |
| 115 | +[npm counterpart]: https://www.npmjs.com/package/node-report |
| 116 | +[`util`]: util.html |
0 commit comments